Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hashing with salt #18

Open
kamatama41 opened this issue Apr 26, 2018 · 4 comments
Open

Hashing with salt #18

kamatama41 opened this issue Apr 26, 2018 · 4 comments

Comments

@kamatama41
Copy link
Owner

ref: https://blog.liclab.com/2017-08-19/extract-and-anonymyze-by-embulk/

ハッシュ化する際にsaltつけたい

@kamatama41
Copy link
Owner Author

kamatama41 commented Apr 28, 2018

How should it be implemented? 🤔

  1. Specify a fixed salt via salt property
filters:
  - type: hash
    columns:
    - { name: email, salt: fooooooooo }
  1. Specify a column to be used as salt via salt_column property
filters:
  - type: hash
    columns:
    - { name: email, salt_column: user_id }

@k24d
Copy link

k24d commented Oct 4, 2018

saltとは少し違いますが、HMAC方式のハッシュにも対応して頂けると嬉しいです!

参考: https://docs.oracle.com/javase/jp/7/api/javax/crypto/Mac.html

diff --git a/src/main/kotlin/org/embulk/filter/hash/HashFilterPlugin.kt b/src/main/kotlin/org/embulk/filter/hash/HashFilterPlugin.kt
index 82c3f30..3a4fff7 100644
--- a/src/main/kotlin/org/embulk/filter/hash/HashFilterPlugin.kt
+++ b/src/main/kotlin/org/embulk/filter/hash/HashFilterPlugin.kt
@@ -17,6 +17,8 @@ import org.embulk.spi.PageReader
 import org.embulk.spi.Schema
 import org.embulk.spi.type.Types
 import java.security.MessageDigest
+import javax.crypto.Mac
+import javax.crypto.spec.SecretKeySpec
 
 class HashFilterPlugin : FilterPlugin {
     interface PluginTask : Task {
@@ -28,6 +30,10 @@ class HashFilterPlugin : FilterPlugin {
         @get:Config("name")
         val name: String
 
+        @get:Config("key")
+        @get:ConfigDefault("null")
+        val key: Optional<String>
+
         @get:Config("algorithm")
         @get:ConfigDefault("\"SHA-256\"")
         val algorithm: Optional<String>
@@ -114,7 +120,7 @@ class HashFilterPlugin : FilterPlugin {
                 hashColumnMap[inputColumn.name]?.let { hashColumn ->
                     // Write hashed value if it's hash column.
                     val outputColumn = outputColumnMap[hashColumn.newName.or(inputColumn.name)]
-                    val hashedValue = generateHash(inputValue.toString(), hashColumn.algorithm.get())
+                    val hashedValue = generateHMAC(inputValue.toString(), hashColumn.key.get(), hashColumn.algorithm.get())
                     builder.setString(outputColumn, hashedValue)
                 } ?: run {
                     // Write the original data
@@ -128,6 +134,14 @@ class HashFilterPlugin : FilterPlugin {
                 return md.digest().joinToString("") { "%02x".format(it) }
             }
 
+            private fun generateHMAC(value: String, key: String, algorithm: String): String {
+                val sks = SecretKeySpec(key.toByteArray(Charsets.UTF_8), algorithm)
+                val mac = Mac.getInstance(algorithm)
+                mac.init(sks)
+                val bytes = mac.doFinal(value.toByteArray(Charsets.US_ASCII))
+                return bytes.joinToString("") { "%02x".format(it) }
+            }
+
             override fun finish() {
                 builder.finish()
             }

@kamatama41
Copy link
Owner Author

@k24d 遅くなりましたが、HMAC hashingに対応したv0.5.0をリリースしたのでよければ試してみてください 🙏

@k24d
Copy link

k24d commented Nov 19, 2018

ありがとうございます!期待した通りに動作することを確認しました。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants