0fd0d9297d3d5b561b03f4b683795e088b45fcf8
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / bpmn / common / util / CryptoHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Modifications Copyright (c) 2019 Samsung
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.bpmn.common.util;
25 import java.io.IOException;
26 import java.security.GeneralSecurityException;
27 import java.util.Properties;
28 import org.onap.so.utils.CryptoUtils;
29
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 public class CryptoHandler implements ICryptoHandler {
34         private static final Logger logger = LoggerFactory.getLogger(CryptoHandler.class);
35         private static final String GENERAL_SECURITY_EXCEPTION_PREFIX = "GeneralSecurityException :";
36         private static final String MSO_KEY = "aa3871669d893c7fb8abbcda31b88b4f";
37         private static final String PROPERTY_KEY = "mso.AaiEncrypted.Pwd";
38
39     @Override
40         public String getMsoAaiPassword() {
41         Properties keyProp = new Properties ();
42                 try {
43                         keyProp.load (Thread.currentThread ().getContextClassLoader ().getResourceAsStream ("urn.properties"));
44                         return CryptoUtils.decrypt((String) keyProp.get(PROPERTY_KEY), MSO_KEY);
45                 } catch (GeneralSecurityException | IOException e) {
46                         logger.error(GENERAL_SECURITY_EXCEPTION_PREFIX + e.getMessage(), e);
47                         return null;
48                 }
49         }
50
51
52         @Override
53         public String encryptMsoPassword(String plainMsoPwd) {
54                 try {
55                         return CryptoUtils.encrypt(plainMsoPwd, MSO_KEY);
56                 } catch (GeneralSecurityException e) {
57                         logger.error(GENERAL_SECURITY_EXCEPTION_PREFIX + e.getMessage(), e);
58                         return null;
59                 }
60         }
61
62         @Override
63         public String decryptMsoPassword(String encryptedPwd) {
64                 try {
65                         return CryptoUtils.decrypt(encryptedPwd, MSO_KEY);
66                 } catch (GeneralSecurityException e) {
67                         logger.error(GENERAL_SECURITY_EXCEPTION_PREFIX + e.getMessage(), e);
68                         return null;
69                 }
70         }
71 }