Update css file name in conf.py
[policy/engine.git] / PolicyEngineUtils / src / main / java / org / onap / policy / utils / PeCryptoUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.utils;
22
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.policy.common.utils.security.CryptoUtils;
28
29 public class PeCryptoUtils {
30
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<>();
36
37     private PeCryptoUtils() {
38     }
39
40     /**
41      * Inits the aes key.
42      *
43      * @param theSecretKey the the secret key
44      */
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);
50             }
51             if (StringUtils.isBlank(secKey)) {
52                 secKey = secretKey;
53             }
54             cryptoUtils = new CryptoUtils(secKey);
55         }
56     }
57
58     /**
59      * Encrypt a value based on the Policy Encryption Key.
60      *
61      * @param value The plain text string
62      * @return The encrypted String
63      */
64     public static String encrypt(String value) {
65         if (cryptoUtils == null || StringUtils.isBlank(value)) {
66             return value;
67         }
68         return encryptCache.computeIfAbsent(value, cryptoUtils::encrypt);
69     }
70
71     /**
72      * Decrypt a value based on the Policy Encryption Key if string begin with 'enc:'.
73      *
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:'
76      */
77     public static String decrypt(String value) {
78         if (cryptoUtils == null || StringUtils.isBlank(value)) {
79             return value;
80         }
81         return decryptCache.computeIfAbsent(value, cryptoUtils::decrypt);
82     }
83 }