From 1c9e4e6d0d30e937fd0137ba88c30251c653798f Mon Sep 17 00:00:00 2001 From: jhh Date: Mon, 21 Oct 2019 09:08:11 -0500 Subject: Add CryptoCoder interface This is to allow multiple supporting implementations. Issue-ID: POLICY-1945 Signed-off-by: jhh Change-Id: I42491e5671f561fe320f034bf8ffe03848dff43f Signed-off-by: jhh --- .../policy/common/utils/security/CryptoCoder.java | 37 ++++++++++++++++++++++ .../policy/common/utils/security/CryptoUtils.java | 13 ++++---- .../common/utils/security/CryptoUtilsTest.java | 4 +-- 3 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 utils/src/main/java/org/onap/policy/common/utils/security/CryptoCoder.java (limited to 'utils/src') 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 index 00000000..34d00343 --- /dev/null +++ b/utils/src/main/java/org/onap/policy/common/utils/security/CryptoCoder.java @@ -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); +} 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 69d257ec..416c73a6 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 @@ -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 +} diff --git a/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java b/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java index a9924152..ce9435d8 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/security/CryptoUtilsTest.java @@ -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); -- cgit 1.2.3-korg