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;
29 private static char hexDigit[] = "0123456789abcdef".toCharArray();
31 /////////////////////////////////
33 /////////////////////////////////
35 * Encrypt MD5 from Byte Array to Byte Array
38 * @throws NoSuchAlgorithmException
40 public static byte[] hashMD5 (byte[] input) throws NoSuchAlgorithmException {
41 // Note: Protect against Multi-thread issues with new MessageDigest
42 MessageDigest md = MessageDigest.getInstance("MD5");
48 * Encrypt MD5 from Byte Array to Byte Array
51 * @throws NoSuchAlgorithmException
53 public static byte[] hashMD5 (byte[] input, int offset, int length) throws NoSuchAlgorithmException {
54 // Note: Protect against Multi-thread issues with new MessageDigest
55 MessageDigest md = MessageDigest.getInstance("MD5");
56 md.update(input,offset,length);
63 * Convenience Function: Encrypt MD5 from String to String Hex representation
67 * @throws NoSuchAlgorithmException
69 public static String hashMD5asStringHex(String input) throws NoSuchAlgorithmException {
70 byte[] output = hashMD5(input.getBytes());
71 StringBuilder sb = new StringBuilder("0x");
72 for (byte b : output) {
73 sb.append(hexDigit[(b >> 4) & 0x0f]);
74 sb.append(hexDigit[b & 0x0f]);
79 /////////////////////////////////
81 /////////////////////////////////
85 public static byte[] hashSHA256(byte[] input) throws NoSuchAlgorithmException {
86 // Note: Protect against Multi-thread issues with new MessageDigest
87 MessageDigest md = MessageDigest.getInstance("SHA-256");
95 public static byte[] hashSHA256(byte[] input, int offset, int length) throws NoSuchAlgorithmException {
96 // Note: Protect against Multi-thread issues with new MessageDigest
97 MessageDigest md = MessageDigest.getInstance("SHA-256");
98 md.update(input,offset,length);
103 * Convenience Function: Hash from String to String Hex representation
107 * @throws NoSuchAlgorithmException
109 public static String hashSHA256asStringHex(String input) throws NoSuchAlgorithmException {
110 return toHex(hashSHA256(input.getBytes()));
114 * Convenience Function: Hash from String to String Hex representation
118 * @throws NoSuchAlgorithmException
120 public static String hashSHA256asStringHex(String input, int salt) throws NoSuchAlgorithmException {
121 byte[] in = input.getBytes();
122 ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE + in.length);
124 bb.put(input.getBytes());
125 return toHex(Hash.hashSHA256(bb.array()));
129 * Compare two byte arrays for equivalency
134 public static boolean isEqual(byte ba1[], byte ba2[]) {
135 if (ba1.length!=ba2.length)return false;
136 for (int i = 0;i<ba1.length; ++i) {
137 if (ba1[i]!=ba2[i])return false;
142 public static int compareTo(byte[] a, byte[] b) {
143 int end = Math.min(a.length, b.length);
145 for (int i=0;compare == 0 && i<end;++i) {
148 if (compare==0)compare=a.length-b.length;
152 public static String toHexNo0x(byte[] ba) {
153 StringBuilder sb = new StringBuilder();
155 sb.append(hexDigit[(b >> 4) & 0x0f]);
156 sb.append(hexDigit[b & 0x0f]);
158 return sb.toString();
161 public static String toHex(byte[] ba) {
162 StringBuilder sb = new StringBuilder("0x");
164 sb.append(hexDigit[(b >> 4) & 0x0f]);
165 sb.append(hexDigit[b & 0x0f]);
167 return sb.toString();
170 public static String toHex(byte[] ba, int start, int length) {
171 StringBuilder sb = new StringBuilder("0x");
172 for (int i=start;i<length;++i) {
173 sb.append(hexDigit[(ba[i] >> 4) & 0x0f]);
174 sb.append(hexDigit[ba[i] & 0x0f]);
176 return sb.toString();
180 public static byte[] fromHex(String s) throws CadiException{
181 if (!s.startsWith("0x")) {
182 throw new CadiException("HexString must start with \"0x\"");
187 byte[] ba = new byte[(s.length()-2)/2];
189 for (int i=2;i<s.length();++i) {
191 if (c>=0x30 && c<=0x39) {
193 } else if (c>=0x61 && c<=0x66) {
194 b=(byte)(c-0x57); // account for "A"
195 } else if (c>=0x41 && c<=0x46) {
198 throw new CadiException("Invalid char '" + c + "' in HexString");
202 ba[idx]=(byte)(b<<4);
213 * Does not expect to start with "0x"
214 * if Any Character doesn't match, it returns null;
219 public static byte[] fromHexNo0x(String s) {
225 if (s.length()%2==0) {
226 ba = new byte[s.length()/2];
230 ba = new byte[(s.length()/2)+1];
235 for (int i=start;i<s.length();++i) {
236 c = s.charAt((i-start));
237 if (c>=0x30 && c<=0x39) {
239 } else if (c>=0x61 && c<=0x66) {
240 b=(byte)(c-0x57); // account for "A"
241 } else if (c>=0x41 && c<=0x46) {
248 ba[idx]=(byte)(b<<4);