diff options
author | jhh <jorge.hernandez-herrero@att.com> | 2019-10-21 09:08:11 -0500 |
---|---|---|
committer | jhh <jorge.hernandez-herrero@att.com> | 2019-10-21 09:57:09 -0500 |
commit | 1c9e4e6d0d30e937fd0137ba88c30251c653798f (patch) | |
tree | 70b56732f7b44e7ebf53ae24613938b0fd8fc6d6 /utils/src | |
parent | 824468c9138700f046e35e1faaf6dc78eef5d98d (diff) |
Add CryptoCoder interface
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>
Diffstat (limited to 'utils/src')
3 files changed, 45 insertions, 9 deletions
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); |