2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.cadi;
24 import java.nio.ByteBuffer;
25 import java.security.MessageDigest;
26 import java.security.NoSuchAlgorithmException;
35 private static char hexDigit[] = "0123456789abcdef".toCharArray();
37 /////////////////////////////////
39 /////////////////////////////////
41 * Encrypt MD5 from Byte Array to Byte Array
44 * @throws NoSuchAlgorithmException
46 public static byte[] hashMD5 (byte[] input) throws NoSuchAlgorithmException {
47 // Note: Protect against Multi-thread issues with new MessageDigest
48 MessageDigest md = MessageDigest.getInstance("MD5");
54 * Encrypt MD5 from Byte Array to Byte Array
57 * @throws NoSuchAlgorithmException
59 public static byte[] hashMD5 (byte[] input, int offset, int length) throws NoSuchAlgorithmException {
60 // Note: Protect against Multi-thread issues with new MessageDigest
61 MessageDigest md = MessageDigest.getInstance("MD5");
62 md.update(input,offset,length);
69 * Convenience Function: Encrypt MD5 from String to String Hex representation
73 * @throws NoSuchAlgorithmException
75 public static String hashMD5asStringHex(String input) throws NoSuchAlgorithmException {
76 byte[] output = hashMD5(input.getBytes());
77 StringBuilder sb = new StringBuilder("0x");
78 for (byte b : output) {
79 sb.append(hexDigit[(b >> 4) & 0x0f]);
80 sb.append(hexDigit[b & 0x0f]);
85 /////////////////////////////////
87 /////////////////////////////////
91 public static byte[] hashSHA256(byte[] input) throws NoSuchAlgorithmException {
92 // Note: Protect against Multi-thread issues with new MessageDigest
93 MessageDigest md = MessageDigest.getInstance("SHA-256");
101 public static byte[] hashSHA256(byte[] input, int offset, int length) throws NoSuchAlgorithmException {
102 // Note: Protect against Multi-thread issues with new MessageDigest
103 MessageDigest md = MessageDigest.getInstance("SHA-256");
104 md.update(input,offset,length);
109 * Convenience Function: Hash from String to String Hex representation
113 * @throws NoSuchAlgorithmException
115 public static String hashSHA256asStringHex(String input) throws NoSuchAlgorithmException {
116 return toHex(hashSHA256(input.getBytes()));
120 * Convenience Function: Hash from String to String Hex representation
124 * @throws NoSuchAlgorithmException
126 public static String hashSHA256asStringHex(String input, int salt) throws NoSuchAlgorithmException {
127 byte[] in = input.getBytes();
128 ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE + in.length);
130 bb.put(input.getBytes());
131 return toHex(Hash.hashSHA256(bb.array()));
135 * Compare two byte arrays for equivalency
140 public static boolean isEqual(byte ba1[], byte ba2[]) {
141 if (ba1.length!=ba2.length)return false;
142 for (int i = 0;i<ba1.length; ++i) {
143 if (ba1[i]!=ba2[i])return false;
148 public static int compareTo(byte[] a, byte[] b) {
149 int end = Math.min(a.length, b.length);
151 for (int i=0;compare == 0 && i<end;++i) {
154 if (compare==0)compare=a.length-b.length;
162 public static String toHexNo0x(byte[] ba) {
163 StringBuilder sb = new StringBuilder();
165 sb.append(hexDigit[(b >> 4) & 0x0f]);
166 sb.append(hexDigit[b & 0x0f]);
168 return sb.toString();
175 public static String toHex(byte[] ba) {
176 StringBuilder sb = new StringBuilder("0x");
178 sb.append(hexDigit[(b >> 4) & 0x0f]);
179 sb.append(hexDigit[b & 0x0f]);
181 return sb.toString();
184 public static String toHex(byte[] ba, int start, int length) {
185 StringBuilder sb = new StringBuilder("0x");
186 for (int i=start;i<length;++i) {
187 sb.append(hexDigit[(ba[i] >> 4) & 0x0f]);
188 sb.append(hexDigit[ba[i] & 0x0f]);
190 return sb.toString();
194 public static byte[] fromHex(String s) {
195 if(!s.startsWith("0x")) {
196 return fromHexNo0x(s);
201 int extra = s.length()%2; // odd requires extra
202 ba = new byte[(s.length()-2)/2 + extra];
203 boolean high = extra==0;
206 for (int i=2;i<s.length();++i) {
208 if (c>=0x30 && c<=0x39) {
210 } else if (c>=0x61 && c<=0x66) {
211 b=(byte)(c-0x57); // account for "A"
212 } else if (c>=0x41 && c<=0x46) {
219 ba[idx]=(byte)(b<<4);
230 * Does not expect to start with "0x"
231 * if Any Character doesn't match, it returns null;
236 public static byte[] fromHexNo0x(String s) {
240 int extra = s.length()%2; // odd requires extra byte to store
241 ba = new byte[(s.length())/2 + extra];
242 boolean high = extra==0;
245 for (int i=0;i<s.length();++i) {
247 if (c>=0x30 && c<=0x39) {
249 } else if (c>=0x61 && c<=0x66) {
250 b=(byte)(c-0x57); // account for "A"
251 } else if (c>=0x41 && c<=0x46) {
258 ba[idx]=(byte)(b<<4);