2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.utils;
23 import java.security.GeneralSecurityException;
25 import java.util.concurrent.ConcurrentHashMap;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29 import org.onap.policy.common.utils.security.CryptoUtils;
31 public class PeCryptoUtils {
33 private static Logger logger = FlexLogger.getLogger(PeCryptoUtils.class);
34 private static final String PROP_AES_KEY = "org.onap.policy.encryption.aes.key";
35 private static CryptoUtils cryptoUtils = null;
36 private static String secretKey = System.getenv("AES_ENCRYPTION_KEY");
37 private static final Map<String, String> decryptCache = new ConcurrentHashMap<>();
38 private static final Map<String, String> encryptCache = new ConcurrentHashMap<>();
41 private PeCryptoUtils() {}
46 * @param theSecretKey the the secret key
48 public static synchronized void initAesKey(String theSecretKey) {
49 String secKey = theSecretKey;
50 if (cryptoUtils == null) {
51 if (StringUtils.isBlank(secKey)) {
52 secKey = System.getProperty(PROP_AES_KEY);
54 if (StringUtils.isBlank(secKey)) {
57 cryptoUtils = new CryptoUtils(secKey);
62 * Encrypt a value based on the Policy Encryption Key.
64 * @param value The plain text string
65 * @return The encrypted String
67 public static String encrypt(String value) {
69 if (cryptoUtils == null || StringUtils.isBlank(value)) {
73 return encryptCache.computeIfAbsent(value, k -> {
75 return cryptoUtils.encrypt(k);
76 } catch (GeneralSecurityException e) {
77 logger.error("Could not decrypt value - exception: ", e);
84 * Decrypt a value based on the Policy Encryption Key if string begin with 'enc:'.
86 * @param value The encrypted string that must be decrypted using the Policy Encryption Key
87 * @return The String decrypted if string begin with 'enc:'
89 public static String decrypt(String value) {
90 if (cryptoUtils == null || StringUtils.isBlank(value)) {
93 return decryptCache.computeIfAbsent(value, k -> {
95 return cryptoUtils.decrypt(k);
96 } catch (GeneralSecurityException e) {
97 logger.error("Could not decrypt value - exception: ", e);