Change the header to SO
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / bpmn / common / util / CryptoUtils.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.common.util;\r
22 \r
23 \r
24 import java.security.GeneralSecurityException;\r
25 import java.security.NoSuchAlgorithmException;\r
26 \r
27 import javax.crypto.Cipher;\r
28 import javax.crypto.spec.SecretKeySpec;\r
29 \r
30 //Need to add BPM error handler\r
31 \r
32 /**\r
33  * CryptoUtils adapted from RTTP client.\r
34  *\r
35  */\r
36 public class CryptoUtils {\r
37 \r
38     public static final String AES = "AES";\r
39 \r
40     /**\r
41     * encrypt a value and generate a keyfile\r
42      * if the keyfile is not found then a new one is created\r
43     * @throws GeneralSecurityException\r
44      * @throws IOException\r
45      */\r
46     public static String encrypt(String value, String keyString) throws GeneralSecurityException\r
47     {\r
48         SecretKeySpec sks = getSecretKeySpec(keyString);\r
49         Cipher cipher = Cipher.getInstance(CryptoUtils.AES);\r
50         cipher.init(Cipher.ENCRYPT_MODE, sks, cipher.getParameters());\r
51         byte[] encrypted = cipher.doFinal(value.getBytes());\r
52         return byteArrayToHexString(encrypted);\r
53     }\r
54 \r
55     /**\r
56     * decrypt a value\r
57      * @throws GeneralSecurityException\r
58      * @throws IOException\r
59      */\r
60     public static String decrypt(String message, String keyString) throws GeneralSecurityException\r
61     {\r
62         SecretKeySpec sks = getSecretKeySpec(keyString);\r
63         Cipher cipher = Cipher.getInstance(CryptoUtils.AES);\r
64         cipher.init(Cipher.DECRYPT_MODE, sks);\r
65         byte[] decrypted = cipher.doFinal(hexStringToByteArray(message));\r
66         return new String(decrypted);\r
67     }\r
68 \r
69     private static SecretKeySpec getSecretKeySpec(String keyString) throws NoSuchAlgorithmException\r
70     {\r
71         byte [] key = hexStringToByteArray(keyString);\r
72         SecretKeySpec sks = new SecretKeySpec(key, CryptoUtils.AES);\r
73         return sks;\r
74     }\r
75 \r
76 \r
77     private static String byteArrayToHexString(byte[] b){\r
78         StringBuffer sb = new StringBuffer(b.length * 2);\r
79         for (int i = 0; i < b.length; i++){\r
80             int v = b[i] & 0xff;\r
81             if (v < 16) {\r
82                 sb.append('0');\r
83             }\r
84             sb.append(Integer.toHexString(v));\r
85     }\r
86         return sb.toString().toUpperCase();\r
87     }\r
88 \r
89     private static byte[] hexStringToByteArray(String s) {\r
90         byte[] b = new byte[s.length() / 2];\r
91         for (int i = 0; i < b.length; i++){\r
92             int index = i * 2;\r
93             int v = Integer.parseInt(s.substring(index, index + 2), 16);\r
94             b[i] = (byte)v;\r
95         }\r
96         return b;\r
97     }\r
98 \r
99     /**\r
100      * Not Used...\r
101      *\r
102     * Call Rttp utility jar to encrypt pwd\r
103     * @param clearPassword\r
104     * @return\r
105     * @throws GeneralSecurityException\r
106      * @throws Exception\r
107     *\r
108     public static String encryptRttpPwd(String clearPassword) throws GeneralSecurityException {\r
109         try {\r
110             return RttpBasicAuth.encrypt(clearPassword);\r
111         } catch (Exception e) {\r
112             // wrap generic exception\r
113             throw new GeneralSecurityException(e);\r
114         }\r
115     }\r
116     */\r
117 }\r
118 \r