[AAF-21] Initial code import
[aaf/cadi.git] / core / src / main / java / com / att / cadi / Hash.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi;\r
25 \r
26 import java.nio.ByteBuffer;\r
27 import java.security.MessageDigest;\r
28 import java.security.NoSuchAlgorithmException;\r
29 \r
30 public class Hash {\r
31         private static char hexDigit[] = "0123456789abcdef".toCharArray();\r
32         \r
33 /////////////////////////////////\r
34 // MD5\r
35 /////////////////////////////////\r
36         /**\r
37          * Encrypt MD5 from Byte Array to Byte Array\r
38          * @param input\r
39          * @return\r
40          * @throws NoSuchAlgorithmException\r
41          */\r
42         public static byte[] encryptMD5 (byte[] input) throws NoSuchAlgorithmException {\r
43                 MessageDigest md = MessageDigest.getInstance("MD5");\r
44                 md.update(input); \r
45                 return md.digest();\r
46         }\r
47 \r
48         /**\r
49          * Encrypt MD5 from Byte Array to Byte Array\r
50          * @param input\r
51          * @return\r
52          * @throws NoSuchAlgorithmException\r
53          */\r
54         public static byte[] encryptMD5 (byte[] input, int offset, int length) throws NoSuchAlgorithmException {\r
55                 MessageDigest md = MessageDigest.getInstance("MD5");\r
56                 md.update(input,offset,length); \r
57                 return md.digest();\r
58         }\r
59 \r
60 \r
61 \r
62         /**\r
63          * Convenience Function: Encrypt MD5 from String to String Hex representation \r
64          * \r
65          * @param input\r
66          * @return\r
67          * @throws NoSuchAlgorithmException\r
68          */\r
69         public static String encryptMD5asStringHex(String input) throws NoSuchAlgorithmException {\r
70                 byte[] output = encryptMD5(input.getBytes());\r
71                 StringBuilder sb = new StringBuilder("0x");\r
72                  for (byte b : output) {\r
73                     sb.append(hexDigit[(b >> 4) & 0x0f]);\r
74                     sb.append(hexDigit[b & 0x0f]);\r
75                  }\r
76                  return sb.toString();\r
77         }\r
78 \r
79 /////////////////////////////////\r
80 // SHA256\r
81 /////////////////////////////////\r
82         /**\r
83          * SHA256 Hashing\r
84          */\r
85         public static byte[] hashSHA256(byte[] input) throws NoSuchAlgorithmException {\r
86                 MessageDigest md = MessageDigest.getInstance("SHA-256");\r
87                 md.update(input); \r
88                 return md.digest();\r
89         }\r
90 \r
91         /**\r
92          * SHA256 Hashing\r
93          */\r
94         public static byte[] hashSHA256(byte[] input, int offset, int length) throws NoSuchAlgorithmException {\r
95                 MessageDigest md = MessageDigest.getInstance("SHA-256");\r
96                 md.update(input,offset,length); \r
97                 return md.digest();\r
98         }\r
99         \r
100         /**\r
101          * Convenience Function: Hash from String to String Hex representation\r
102          * \r
103          * @param input\r
104          * @return\r
105          * @throws NoSuchAlgorithmException\r
106          */\r
107         public static String hashSHA256asStringHex(String input) throws NoSuchAlgorithmException {\r
108                 byte[] output = hashSHA256(input.getBytes());\r
109                 StringBuilder sb = new StringBuilder("0x");\r
110                  for (byte b : output) {\r
111                     sb.append(hexDigit[(b >> 4) & 0x0f]);\r
112                     sb.append(hexDigit[b & 0x0f]);\r
113                  }\r
114                  return sb.toString();\r
115         }\r
116 \r
117         /**\r
118          * Convenience Function: Hash from String to String Hex representation\r
119          * \r
120          * @param input\r
121          * @return\r
122          * @throws NoSuchAlgorithmException\r
123          */\r
124         public static String hashSHA256asStringHex(String input, int salt) throws NoSuchAlgorithmException {\r
125                 byte[] in = input.getBytes();\r
126                 ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE + in.length);\r
127                 bb.putInt(salt);\r
128                 bb.put(input.getBytes());\r
129                 byte[] output = Hash.hashSHA256(bb.array());\r
130                 StringBuilder sb = new StringBuilder("0x");\r
131                  for (byte b : output) {\r
132                     sb.append(hexDigit[(b >> 4) & 0x0f]);\r
133                     sb.append(hexDigit[b & 0x0f]);\r
134                  }\r
135                  return sb.toString();\r
136         }\r
137         \r
138         /**\r
139          * Compare two byte arrays for equivalency\r
140          * @param ba1\r
141          * @param ba2\r
142          * @return\r
143          */\r
144         public static boolean isEqual(byte ba1[], byte ba2[]) {\r
145                 if(ba1.length!=ba2.length)return false;\r
146                 for(int i = 0;i<ba1.length; ++i) {\r
147                         if(ba1[i]!=ba2[i])return false;\r
148                 }\r
149                 return true;\r
150         }\r
151 \r
152         public static int compareTo(byte[] a, byte[] b) {\r
153                 int end = Math.min(a.length, b.length);\r
154                 int compare = 0;\r
155                 for(int i=0;compare == 0 && i<end;++i) {\r
156                         compare = a[i]-b[i];\r
157                 }\r
158                 if(compare==0)compare=a.length-b.length;\r
159                 return compare;\r
160         }\r
161         \r
162         public static String toHex(byte[] ba) {\r
163                 StringBuilder sb = new StringBuilder("0x");\r
164                  for (byte b : ba) {\r
165                     sb.append(hexDigit[(b >> 4) & 0x0f]);\r
166                     sb.append(hexDigit[b & 0x0f]);\r
167                  }\r
168                  return sb.toString();\r
169         }\r
170         \r
171         public static byte[] fromHex(String s)  throws CadiException{\r
172                 if(!s.startsWith("0x")) {\r
173                         throw new CadiException("HexString must start with \"0x\"");\r
174                 }\r
175                 boolean high = true;\r
176                 int c;\r
177                 byte b;\r
178                 byte[] ba = new byte[(s.length()-2)/2];\r
179                 int idx;\r
180                 for(int i=2;i<s.length();++i) {\r
181                         c = s.charAt(i);\r
182                         if(c>=0x30 && c<=0x39) {\r
183                                 b=(byte)(c-0x30);\r
184                         } else if(c>=0x61 && c<=0x66) {\r
185                                 b=(byte)(c-0x57);  // account for "A"\r
186                         } else if(c>=0x41 && c<=0x46) {\r
187                                 b=(byte)(c-0x37);\r
188                         } else {\r
189                                 throw new CadiException("Invalid char '" + c + "' in HexString");\r
190                         }\r
191                         idx = (i-2)/2;\r
192                         if(high) {\r
193                                 ba[idx]=(byte)(b<<4);\r
194                                 high = false;\r
195                         } else {\r
196                                 ba[idx]|=b;\r
197                                 high = true;\r
198                         }\r
199                 }\r
200                 return ba;\r
201         }\r
202 \r
203 }\r