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;
24 import java.util.concurrent.ConcurrentHashMap;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.policy.common.utils.security.CryptoUtils;
29 public class PeCryptoUtils {
31 private static final String PROP_AES_KEY = "org.onap.policy.encryption.aes.key";
32 private static CryptoUtils cryptoUtils = null;
33 private static String secretKey = System.getenv("AES_ENCRYPTION_KEY");
34 private static final Map<String, String> decryptCache = new ConcurrentHashMap<>();
35 private static final Map<String, String> encryptCache = new ConcurrentHashMap<>();
37 private PeCryptoUtils() {
43 * @param theSecretKey the the secret key
45 public static synchronized void initAesKey(String theSecretKey) {
46 String secKey = theSecretKey;
47 if (cryptoUtils == null) {
48 if (StringUtils.isBlank(secKey)) {
49 secKey = System.getProperty(PROP_AES_KEY);
51 if (StringUtils.isBlank(secKey)) {
54 cryptoUtils = new CryptoUtils(secKey);
59 * Encrypt a value based on the Policy Encryption Key.
61 * @param value The plain text string
62 * @return The encrypted String
64 public static String encrypt(String value) {
65 if (cryptoUtils == null || StringUtils.isBlank(value)) {
68 return encryptCache.computeIfAbsent(value, cryptoUtils::encrypt);
72 * Decrypt a value based on the Policy Encryption Key if string begin with 'enc:'.
74 * @param value The encrypted string that must be decrypted using the Policy Encryption Key
75 * @return The String decrypted if string begin with 'enc:'
77 public static String decrypt(String value) {
78 if (cryptoUtils == null || StringUtils.isBlank(value)) {
81 return decryptCache.computeIfAbsent(value, cryptoUtils::decrypt);