Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit d81d4b0

Browse files
committed
Updated CryptoUtil. Added warning for people using ancient versions of java.
1 parent c72d65d commit d81d4b0

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/main/scala/org/codeoverflow/chatoverflow/configuration/CryptoUtil.scala

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.codeoverflow.chatoverflow.configuration
22

33
import java.nio.charset.StandardCharsets
4-
import java.security.{DigestException, MessageDigest, SecureRandom}
4+
import java.security.{DigestException, InvalidKeyException, MessageDigest, SecureRandom}
55
import java.util
66
import java.util.Base64
77

88
import javax.crypto.spec.{IvParameterSpec, PBEKeySpec, SecretKeySpec}
99
import javax.crypto.{Cipher, SecretKeyFactory}
10+
import org.codeoverflow.chatoverflow.WithLogger
1011

1112
/**
1213
* Provides methods to de- and encrypt using the AES-CBC algorithm.
@@ -15,7 +16,7 @@ import javax.crypto.{Cipher, SecretKeyFactory}
1516
* The SSL compliant / crypto-js compliant code is based on
1617
* https://stackoverflow.com/questions/41432896/cryptojs-aes-encryption-and-java-aes-decryption
1718
*/
18-
object CryptoUtil {
19+
object CryptoUtil extends WithLogger {
1920

2021
// Used for the run-unique auth key
2122
private val runSpecificRandom = generateIV
@@ -65,6 +66,9 @@ object CryptoUtil {
6566
Some(decrString.substring(5))
6667
}
6768
} catch {
69+
case e: InvalidKeyException => logger.error("Your environment does not work with AES256." +
70+
"Please update your java runtime version to at least: 1.8.0_161", e)
71+
None
6872
case _: Exception => None
6973
}
7074
}
@@ -143,6 +147,13 @@ object CryptoUtil {
143147
new String(Base64.getEncoder.encode(message), StandardCharsets.UTF_8)
144148
}
145149

150+
private def generateSalt: Array[Byte] = {
151+
val random = new SecureRandom()
152+
val salt = new Array[Byte](8)
153+
random.nextBytes(salt)
154+
salt
155+
}
156+
146157
/**
147158
* Encrypts the provided plaintext using AES.
148159
*
@@ -173,13 +184,6 @@ object CryptoUtil {
173184
ciphertext.toString
174185
}
175186

176-
private def generateSalt: Array[Byte] = {
177-
val random = new SecureRandom()
178-
val salt = new Array[Byte](8)
179-
random.nextBytes(salt)
180-
salt
181-
}
182-
183187
private def generateIV: Array[Byte] = {
184188
val random = new SecureRandom()
185189
val iv = new Array[Byte](16)

0 commit comments

Comments
 (0)