Skip to content

Commit

Permalink
Code review fix
Browse files Browse the repository at this point in the history
  • Loading branch information
migesok committed Mar 30, 2020
1 parent fdacdbd commit 1b7a525
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -15,7 +15,7 @@ Add the following resolver

Add the library to your dependencies list

libraryDependencies += "com.evolutiongaming" %% "crypto" % "1.6"
libraryDependencies += "com.evolutiongaming" %% "crypto" % "2.0.0"

Create an application config file `environments/default.conf`:

Expand Down
20 changes: 12 additions & 8 deletions src/main/scala/com/evolutiongaming/crypto/Crypto.scala
Expand Up @@ -91,12 +91,16 @@ object Crypto {

def decrypt(value: String, privateKey: String): String = {
val privateKeyBytes = privateKey.getBytes(UTF_8)
require(privateKeyBytes.length <= AesKeyBytesMaxSize, throw new AesKeyTooLong)
if (privateKeyBytes.length > AesKeyBytesMaxSize) {
throw new AesKeyTooLong
}
val effectiveSecretKey = privateKeyBytes.take(AesKeyBytesMaxSize)
val skeySpec = new SecretKeySpec(effectiveSecretKey, CipherAlgorithm)
val cipher = Cipher.getInstance(CipherTransformation)
cipher.init(Cipher.DECRYPT_MODE, skeySpec)
new String(cipher.doFinal(Hex.decodeHex(value)))
val valueBytes = Hex.decodeHex(value)
val decryptedValueBytes = cipher.doFinal(valueBytes)
new String(decryptedValueBytes, UTF_8)
}
}

Expand All @@ -108,11 +112,12 @@ object Crypto {
private val CipherTransformation = "AES"

def decrypt(value: String, privateKey: String): String = {
val data = Base64.decodeBase64(value)
val valueBytes = Base64.decodeBase64(value)
val skeySpec = aesSKey128bitWithSha256(privateKey.getBytes(UTF_8))
val cipher = Cipher.getInstance(CipherTransformation)
cipher.init(Cipher.DECRYPT_MODE, skeySpec)
new String(cipher.doFinal(data), UTF_8)
val decryptedValueBytes = cipher.doFinal(valueBytes)
new String(decryptedValueBytes, UTF_8)
}
}

Expand All @@ -129,12 +134,11 @@ object Crypto {
val skeySpec = aesSKey128bitWithSha256(privateKey.getBytes(UTF_8))
val cipher = Cipher.getInstance(CipherTransformation)
val blockSize = cipher.getBlockSize
if (ivWithEncryptedData.length < blockSize) {
throw new IllegalArgumentException("invalid data size")
}
require(ivWithEncryptedData.length >= blockSize, "invalid data size")
val (iv, encryptedData) = ivWithEncryptedData.splitAt(blockSize)
cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(iv))
new String(cipher.doFinal(encryptedData), UTF_8)
val decryptedData = cipher.doFinal(encryptedData)
new String(decryptedData, UTF_8)
}
}

Expand Down
2 changes: 1 addition & 1 deletion version.sbt
@@ -1 +1 @@
version in ThisBuild := "1.6-SNAPSHOT"
version in ThisBuild := "2.0-SNAPSHOT"

0 comments on commit 1b7a525

Please sign in to comment.