Add CryptoCoder interface 78/97378/3
authorjhh <jorge.hernandez-herrero@att.com>
Mon, 21 Oct 2019 14:08:11 +0000 (09:08 -0500)
committerjhh <jorge.hernandez-herrero@att.com>
Mon, 21 Oct 2019 14:57:09 +0000 (09:57 -0500)
This is to allow multiple supporting implementations.

Issue-ID: POLICY-1945
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I42491e5671f561fe320f034bf8ffe03848dff43f
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
utils/src/main/java/org/onap/policy/common/utils/security/CryptoCoder.java [new file with mode: 0644]
utils/src/main/java/org/onap/policy/common/utils/security/CryptoUtils.java
utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java

diff --git a/utils/src/main/java/org/onap/policy/common/utils/security/CryptoCoder.java b/utils/src/main/java/org/onap/policy/common/utils/security/CryptoCoder.java
new file mode 100644 (file)
index 0000000..34d0034
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.common.utils.security;
+
+/**
+ * Crypto Coder.
+ */
+public interface CryptoCoder {
+
+    /**
+     * Encrypts a String.
+     */
+    String encrypt(String value);
+
+    /**
+     * Decrypts a String.
+     */
+    String decrypt(String value);
+}
index 69d257e..416c73a 100644 (file)
@@ -33,8 +33,8 @@ import org.slf4j.LoggerFactory;
 /**
  * AES Encryption Utilities.
  */
-public class CryptoUtils {
-    private static Logger logger = LoggerFactory.getLogger(CryptoUtils.class);
+public class CryptoUtils implements CryptoCoder {
+    private static final Logger logger = LoggerFactory.getLogger(CryptoUtils.class);
 
     /**
      * Definition of encryption algorithm.
@@ -90,13 +90,13 @@ public class CryptoUtils {
      *  The plain text string
      * @return The encrypted String
      */
+    @Override
     public String encrypt(String value) {
         return encryptValue(value, secretKeySpec);
     }
 
     /**
      * Encrypt a value based on the Policy Encryption Key.
-     *
      * @param value
      *  The plain text string
      * @param secretKey
@@ -140,6 +140,7 @@ public class CryptoUtils {
      *  The encrypted string that must be decrypted using the Policy Encryption Key
      * @return The String decrypted if string begin with 'enc:'
      */
+    @Override
     public String decrypt(String value) {
         return decryptValue(value, secretKeySpec);
     }
@@ -208,10 +209,8 @@ public class CryptoUtils {
      */
     private static SecretKeySpec readSecretKeySpec(String secretKey) {
         if (secretKey != null && !secretKey.isEmpty()) {
-            SecretKeySpec keySpec;
             try {
-                keySpec = getSecretKeySpec(secretKey);
-                return keySpec;
+                return getSecretKeySpec(secretKey);
             } catch (Exception e) {
                 logger.error("Invalid key - exception: ", e);
                 return null;
@@ -254,4 +253,4 @@ public class CryptoUtils {
             logger.info("Example: CryptoUtils dec     enc:112233 1234");
         }
     }
-}
\ No newline at end of file
+}
index a992415..ce9435d 100644 (file)
@@ -44,7 +44,7 @@ public class CryptoUtilsTest {
     @Test
     public void testEncrypt() throws GeneralSecurityException {
         logger.info("testEncrypt:");
-        CryptoUtils cryptoUtils = new CryptoUtils(SECRET_KEY);
+        CryptoCoder cryptoUtils = new CryptoUtils(SECRET_KEY);
         String encryptedValue = cryptoUtils.encrypt(PASS);
         logger.info(ENCRYPTED_MSG, PASS, encryptedValue);
         assertTrue(encryptedValue.startsWith("enc:"));
@@ -57,7 +57,7 @@ public class CryptoUtilsTest {
     @Test
     public void testDecrypt() throws GeneralSecurityException {
         logger.info("testDecrypt:");
-        CryptoUtils cryptoUtils = new CryptoUtils(SECRET_KEY);
+        CryptoCoder cryptoUtils = new CryptoUtils(SECRET_KEY);
         String decryptedValue = cryptoUtils.decrypt(ENCRYPTED_PASS);
         logger.info(DECRYPTED_MSG, ENCRYPTED_PASS, decryptedValue);
         assertEquals(PASS, decryptedValue);