Created
June 5, 2015 05:17
-
-
Save rhwlo/afe4dd7cc7d52767033e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def deriveKey(masterKey: String, allowedOperations: String) = { | |
def bytesToString(bytes: Array[Byte]) = { | |
bytes.map({byte => Integer.toString(byte.toInt, 16)}).mkString | |
} | |
val masterKeyBytes: Array[Byte] = { | |
masterKey.map({ char => | |
Integer.parseInt(char.toString, 16).toByte | |
}).toArray | |
} | |
val cipher: Cipher = Cipher.getInstance("AES/CBC/PKCS5Padding") | |
val secretKey: SecretKeySpec = new SecretKeySpec(masterKeyBytes, "AES") | |
cipher.init(Cipher.ENCRYPT_MODE, secretKey) | |
val ivString = bytesToString(cipher.getIV) | |
val jsonDescription: String = s"""{"allowed_operations":"${allowedOperations}"}""" | |
val cipherText = bytesToString(cipher.doFinal(jsonDescription.getBytes("UTF-8"))) | |
ivString ++ cipherText | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment