b8b668dd0bd1c185edb3475132311ffd7a4cc798
[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 \r
34 /**\r
35  * The Processing of a Password.  Password can be encrypted and decrypted.\r
36  * @author Vikram Singh\r
37  * @version $Id: PasswordProcessor.java,v 1.0 2016/12/14 10:16:52 EST\r
38  */\r
39 public class PasswordProcessor {\r
40 \r
41     private static final String SECRET_KEY_FACTORY_TYPE = "PBEWithMD5AndDES";\r
42     private static final String PASSWORD_ENCRYPTION_STRING =\r
43             (new DB()).getProperties().getProperty("org.onap.dmaap.datarouter.provserver.passwordencryption");\r
44     private static final char[] PASSWORD = PASSWORD_ENCRYPTION_STRING.toCharArray();\r
45     private static final byte[] SALT = {(byte) 0xde, (byte) 0x33, (byte) 0x10,\r
46         (byte) 0x12, (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,};\r
47 \r
48     private PasswordProcessor(){\r
49     }\r
50 \r
51     /**\r
52      * Encrypt password.\r
53      * @param property the Password\r
54      * @return Encrypted password.\r
55      */\r
56     public static String encrypt(String property) throws GeneralSecurityException {\r
57         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_TYPE);\r
58         SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));\r
59         Cipher pbeCipher = Cipher.getInstance(SECRET_KEY_FACTORY_TYPE);\r
60         pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 32));\r
61         return Base64.getEncoder().encodeToString(pbeCipher.doFinal(property.getBytes(StandardCharsets.UTF_8)));\r
62     }\r
63 \r
64     /**\r
65      * Decrypt password.\r
66      * @param property the Password\r
67      * @return Decrypt password.\r
68      */\r
69     public static String decrypt(String property) throws GeneralSecurityException {\r
70         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_TYPE);\r
71         SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));\r
72         Cipher pbeCipher = Cipher.getInstance(SECRET_KEY_FACTORY_TYPE);\r
73         pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 32));\r
74         return new String(pbeCipher.doFinal(Base64.getDecoder().decode(property)), StandardCharsets.UTF_8);\r
75     }\r
76 \r
77 }\r