Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / utils / PasswordProcessor.java
1 /**\r
2  * -\r
3  * ============LICENSE_START=======================================================\r
4  * Copyright (C) 2019 Nordix Foundation.\r
5  * ================================================================================\r
6  * Licensed under the Apache License, Version 2.0 (the "License");\r
7  * you may not use this file except in compliance with the License.\r
8  * You may obtain a copy of the License at\r
9  *\r
10  * <p>http://www.apache.org/licenses/LICENSE-2.0\r
11  *\r
12  * <p>* Unless required by applicable law or agreed to in writing, software\r
13  * distributed under the License is distributed on an "AS IS" BASIS,\r
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
15  * See the License for the specific language governing permissions and\r
16  * limitations under the License.\r
17  *\r
18  * <p>* SPDX-License-Identifier: Apache-2.0\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 \r
22 package org.onap.dmaap.datarouter.provisioning.utils;\r
23 \r
24 import java.nio.charset.StandardCharsets;\r
25 import java.security.GeneralSecurityException;\r
26 import java.util.Base64;\r
27 \r
28 import javax.crypto.Cipher;\r
29 import javax.crypto.SecretKey;\r
30 import javax.crypto.SecretKeyFactory;\r
31 import javax.crypto.spec.PBEKeySpec;\r
32 import javax.crypto.spec.PBEParameterSpec;\r
33 import org.onap.dmaap.datarouter.provisioning.ProvRunner;\r
34 \r
35 /**\r
36  * The Processing of a Password.  Password can be encrypted and decrypted.\r
37  * @author Vikram Singh\r
38  * @version $Id: PasswordProcessor.java,v 1.0 2016/12/14 10:16:52 EST\r
39  */\r
40 public class PasswordProcessor {\r
41 \r
42     private static final String SECRET_KEY_FACTORY_TYPE = "PBEWithMD5AndDES";\r
43     private static final String PASSWORD_ENCRYPTION_STRING =\r
44             ProvRunner.getProvProperties().getProperty("org.onap.dmaap.datarouter.provserver.passwordencryption");\r
45     private static final char[] PASSWORD = PASSWORD_ENCRYPTION_STRING.toCharArray();\r
46     private static final byte[] SALT = {(byte) 0xde, (byte) 0x33, (byte) 0x10,\r
47         (byte) 0x12, (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,};\r
48 \r
49     private PasswordProcessor(){\r
50     }\r
51 \r
52     /**\r
53      * Encrypt password.\r
54      * @param property the Password\r
55      * @return Encrypted password.\r
56      */\r
57     public static String encrypt(String property) throws GeneralSecurityException {\r
58         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_TYPE);\r
59         SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));\r
60         Cipher pbeCipher = Cipher.getInstance(SECRET_KEY_FACTORY_TYPE);\r
61         pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 32));\r
62         return Base64.getEncoder().encodeToString(pbeCipher.doFinal(property.getBytes(StandardCharsets.UTF_8)));\r
63     }\r
64 \r
65     /**\r
66      * Decrypt password.\r
67      * @param property the Password\r
68      * @return Decrypt password.\r
69      */\r
70     public static String decrypt(String property) throws GeneralSecurityException {\r
71         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_TYPE);\r
72         SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));\r
73         Cipher pbeCipher = Cipher.getInstance(SECRET_KEY_FACTORY_TYPE);\r
74         pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 32));\r
75         return new String(pbeCipher.doFinal(Base64.getDecoder().decode(property)), StandardCharsets.UTF_8);\r
76     }\r
77 \r
78 }\r