Skip to content

Instantly share code, notes, and snippets.

@rhwlo
Created June 5, 2015 05:17
Show Gist options
  • Save rhwlo/afe4dd7cc7d52767033e to your computer and use it in GitHub Desktop.
Save rhwlo/afe4dd7cc7d52767033e to your computer and use it in GitHub Desktop.
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