44142031f464a86a83981eb170e13413ea739d93
[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  * <p>\r
10  * http://www.apache.org/licenses/LICENSE-2.0\r
11  * <p>\r
12  * 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  * <p>\r
18  * 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 javax.crypto.Cipher;\r
25 import javax.crypto.SecretKey;\r
26 import javax.crypto.SecretKeyFactory;\r
27 import javax.crypto.spec.PBEKeySpec;\r
28 import javax.crypto.spec.PBEParameterSpec;\r
29 import java.nio.charset.StandardCharsets;\r
30 import java.security.GeneralSecurityException;\r
31 import java.util.Base64;\r
32 \r
33 /**\r
34  * The Processing of a Password.  Password can be encrypted and decrypted.\r
35  * @author Vikram Singh\r
36  * @version $Id: PasswordProcessor.java,v 1.0 2016/12/14 10:16:52 EST\r
37  */\r
38 public class PasswordProcessor {\r
39 \r
40     private PasswordProcessor(){}\r
41 \r
42     private static final String SECRET_KEY_FACTORY_TYPE = "PBEWithMD5AndDES";\r
43     private static final String PASSWORD_ENCRYPTION_STRING = (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, (byte) 0x12, (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12,};\r
46 \r
47     /**\r
48      * Encrypt password.\r
49      * @param property the Password\r
50      * @return Encrypted password.\r
51      */\r
52     public static String encrypt(String property) throws GeneralSecurityException {\r
53         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_TYPE);\r
54         SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));\r
55         Cipher pbeCipher = Cipher.getInstance(SECRET_KEY_FACTORY_TYPE);\r
56         pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 32));\r
57         return Base64.getEncoder().encodeToString(pbeCipher.doFinal(property.getBytes(StandardCharsets.UTF_8)));\r
58     }\r
59 \r
60     /**\r
61      * Decrypt password.\r
62      * @param property the Password\r
63      * @return Decrypt password.\r
64      */\r
65     public static String decrypt(String property) throws GeneralSecurityException {\r
66         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY_TYPE);\r
67         SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));\r
68         Cipher pbeCipher = Cipher.getInstance(SECRET_KEY_FACTORY_TYPE);\r
69         pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 32));\r
70         return new String(pbeCipher.doFinal(Base64.getDecoder().decode(property)), StandardCharsets.UTF_8);\r
71     }\r
72 \r
73 }\r