11package org .codeoverflow .chatoverflow .configuration
22
33import java .nio .charset .StandardCharsets
4- import java .security .{DigestException , MessageDigest , SecureRandom }
4+ import java .security .{DigestException , InvalidKeyException , MessageDigest , SecureRandom }
55import java .util
66import java .util .Base64
77
88import javax .crypto .spec .{IvParameterSpec , PBEKeySpec , SecretKeySpec }
99import 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