summaryrefslogtreecommitdiffstats
path: root/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2019-06-12 14:32:25 -0400
committerJim Hahn <jrh3@att.com>2019-06-12 17:36:59 -0400
commitea262e6da52fd4da0733f02998f87aebaf502ddb (patch)
treea7cded567521f0141f31d9f8c185eccdaf2a8979 /utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java
parente671e9fa5d09ec696cb24aeefaf10ddbc7c705d7 (diff)
Apply simple sonar fixes
Note: A number of these were identified, by SonarLint, in the Test classes, which are not typically scanned by Sonar. Removed unnecessary imports. Removed unneeded "throws Xxx". Replaced lambda with method references. Replaced duplicate strings with constants. Replaced try-fail-catch with assert-j methods to eliminate sonar complaints about duplicate failure messages. Added missing @Override annotations. Use map.computeIfAbsent() where appropriate. Also fixed some minor checkstyle issues. Removed unneeded "volatile" declarations. Replaced some if-else constructs with "?:" construct, per sonar. Replaced Object.wait() with CountDownLatch.await(); according to sonar (and javadocs), Object.wait() can return due to "spurious wakeups". Fixed issue whereby CryptoUtilsTest wouldn't run in my Eclipse. Change-Id: Ib6b71ed65662cfd6209400dac57ed69279bf29ec Issue-ID: POLICY-1791 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java')
-rw-r--r--utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java20
1 files changed, 9 insertions, 11 deletions
diff --git a/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java b/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java
index ebe0483f..579eed9f 100644
--- a/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java
+++ b/utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java
@@ -20,16 +20,13 @@
package org.onap.policy.common.utils.security;
-import com.google.common.base.Charsets;
-
+import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
-
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
-
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -65,7 +62,7 @@ public class CryptoUtils {
/**
* CryptoUtils - encryption tool constructor.
- * @param secretKey
+ * @param secretKeySpec
* AES supports 128, 192 or 256-bit long key size, it can be plain text or generated with key generator
*/
public CryptoUtils(SecretKeySpec secretKeySpec) {
@@ -80,7 +77,7 @@ public class CryptoUtils {
* Encrypt a value based on the Policy Encryption Key.
* Equivalent openssl command: echo -n "123456" | openssl aes-128-cbc -e -K PrivateHexkey
* -iv 16BytesIV | xxd -u -g100
- *
+ *
* <p>Final result is to put in properties file is: IV + Outcome of openssl command
*
* @param value
@@ -123,7 +120,7 @@ public class CryptoUtils {
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivspec);
return "enc:" + DatatypeConverter.printBase64Binary(
- ArrayUtils.addAll(iv, cipher.doFinal(value.getBytes(Charsets.UTF_8))));
+ ArrayUtils.addAll(iv, cipher.doFinal(value.getBytes(StandardCharsets.UTF_8))));
} catch (Exception e) {
logger.error("Could not encrypt value - exception: ", e);
return value;
@@ -181,7 +178,7 @@ public class CryptoUtils {
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivspec);
byte[] decrypted = cipher.doFinal(realData);
- return new String(decrypted, Charsets.UTF_8);
+ return new String(decrypted, StandardCharsets.UTF_8);
} catch (Exception e) {
logger.error("Could not decrypt value - exception: ", e);
}
@@ -224,6 +221,7 @@ public class CryptoUtils {
return null;
}
}
+
/**
* Check if string is encrypted by verify if string prefix with 'enc:'.
*
@@ -243,12 +241,12 @@ public class CryptoUtils {
if (args.length == 3) {
if ("enc".equals(args[0])) {
String encryptedValue = encrypt(args[1], args[2]);
- logger.info("original value: " + args[1] + " encrypted value: " + encryptedValue);
+ logger.info("original value: {} encrypted value: {}", args[1], encryptedValue);
} else if ("dec".equals(args[0])) {
String decryptedValue = decrypt(args[1], args[2]);
- logger.info("original value: " + args[1] + " decrypted value: " + decryptedValue);
+ logger.info("original value: {} decrypted value: {}", args[1], decryptedValue);
} else {
- logger.info("Unknown request: " + args[0]);
+ logger.info("Unknown request: {}", args[0]);
}
} else {
logger.info("Usage : CryptoUtils enc/dec password secretKey");