2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2023 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.ccsdk.apps.cadi;
24 import java.nio.ByteBuffer;
25 import java.security.MessageDigest;
26 import java.security.NoSuchAlgorithmException;
34 private static char hexDigit[] = "0123456789abcdef".toCharArray();
36 /////////////////////////////////
38 /////////////////////////////////
40 * Encrypt MD5 from Byte Array to Byte Array
43 * @throws NoSuchAlgorithmException
45 public static byte[] hashMD5 (byte[] input) throws NoSuchAlgorithmException {
46 // Note: Protect against Multi-thread issues with new MessageDigest
47 MessageDigest md = MessageDigest.getInstance("MD5");
53 * Encrypt MD5 from Byte Array to Byte Array
56 * @throws NoSuchAlgorithmException
58 public static byte[] hashMD5 (byte[] input, int offset, int length) throws NoSuchAlgorithmException {
59 // Note: Protect against Multi-thread issues with new MessageDigest
60 MessageDigest md = MessageDigest.getInstance("MD5");
61 md.update(input,offset,length);
68 * Convenience Function: Encrypt MD5 from String to String Hex representation
72 * @throws NoSuchAlgorithmException
74 public static String hashMD5asStringHex(String input) throws NoSuchAlgorithmException {
75 byte[] output = hashMD5(input.getBytes());
76 StringBuilder sb = new StringBuilder("0x");
77 for (byte b : output) {
78 sb.append(hexDigit[(b >> 4) & 0x0f]);
79 sb.append(hexDigit[b & 0x0f]);
84 /////////////////////////////////
86 /////////////////////////////////
90 public static byte[] hashSHA256(byte[] input) throws NoSuchAlgorithmException {
91 // Note: Protect against Multi-thread issues with new MessageDigest
92 MessageDigest md = MessageDigest.getInstance("SHA-256");
100 public static byte[] hashSHA256(byte[] input, int offset, int length) throws NoSuchAlgorithmException {
101 // Note: Protect against Multi-thread issues with new MessageDigest
102 MessageDigest md = MessageDigest.getInstance("SHA-256");
103 md.update(input,offset,length);
108 * Convenience Function: Hash from String to String Hex representation
112 * @throws NoSuchAlgorithmException
114 public static String hashSHA256asStringHex(String input) throws NoSuchAlgorithmException {
115 return toHex(hashSHA256(input.getBytes()));
119 * Convenience Function: Hash from String to String Hex representation
123 * @throws NoSuchAlgorithmException
125 public static String hashSHA256asStringHex(String input, int salt) throws NoSuchAlgorithmException {
126 byte[] in = input.getBytes();
127 ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE + in.length);
129 bb.put(input.getBytes());
130 return toHex(Hash.hashSHA256(bb.array()));
134 * Compare two byte arrays for equivalency
139 public static boolean isEqual(byte ba1[], byte ba2[]) {
140 if (ba1.length!=ba2.length)return false;
141 for (int i = 0;i<ba1.length; ++i) {
142 if (ba1[i]!=ba2[i])return false;
147 public static int compareTo(byte[] a, byte[] b) {
148 int end = Math.min(a.length, b.length);
150 for (int i=0;compare == 0 && i<end;++i) {
153 if (compare==0)compare=a.length-b.length;
161 public static String toHexNo0x(byte[] ba) {
162 StringBuilder sb = new StringBuilder();
164 sb.append(hexDigit[(b >> 4) & 0x0f]);
165 sb.append(hexDigit[b & 0x0f]);
167 return sb.toString();
174 public static String toHex(byte[] ba) {
175 StringBuilder sb = new StringBuilder("0x");
177 sb.append(hexDigit[(b >> 4) & 0x0f]);
178 sb.append(hexDigit[b & 0x0f]);
180 return sb.toString();
183 public static String toHex(byte[] ba, int start, int length) {
184 StringBuilder sb = new StringBuilder("0x");
185 for (int i=start;i<length;++i) {
186 sb.append(hexDigit[(ba[i] >> 4) & 0x0f]);
187 sb.append(hexDigit[ba[i] & 0x0f]);
189 return sb.toString();
193 public static byte[] fromHex(String s) {
194 if(!s.startsWith("0x")) {
195 return fromHexNo0x(s);
200 int extra = s.length()%2; // odd requires extra
201 ba = new byte[(s.length()-2)/2 + extra];
202 boolean high = extra==0;
205 for (int i=2;i<s.length();++i) {
207 if (c>=0x30 && c<=0x39) {
209 } else if (c>=0x61 && c<=0x66) {
210 b=(byte)(c-0x57); // account for "A"
211 } else if (c>=0x41 && c<=0x46) {
218 ba[idx]=(byte)(b<<4);
229 * Does not expect to start with "0x"
230 * if Any Character doesn't match, it returns null;
235 public static byte[] fromHexNo0x(String s) {
239 int extra = s.length()%2; // odd requires extra byte to store
240 ba = new byte[(s.length())/2 + extra];
241 boolean high = extra==0;
244 for (int i=0;i<s.length();++i) {
246 if (c>=0x30 && c<=0x39) {
248 } else if (c>=0x61 && c<=0x66) {
249 b=(byte)(c-0x57); // account for "A"
250 } else if (c>=0x41 && c<=0x46) {
257 ba[idx]=(byte)(b<<4);