1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.cadi;
\r
25 import java.nio.ByteBuffer;
\r
26 import java.security.MessageDigest;
\r
27 import java.security.NoSuchAlgorithmException;
\r
30 private static char hexDigit[] = "0123456789abcdef".toCharArray();
\r
32 /////////////////////////////////
\r
34 /////////////////////////////////
\r
36 * Encrypt MD5 from Byte Array to Byte Array
\r
39 * @throws NoSuchAlgorithmException
\r
41 public static byte[] encryptMD5 (byte[] input) throws NoSuchAlgorithmException {
\r
42 MessageDigest md = MessageDigest.getInstance("MD5");
\r
48 * Encrypt MD5 from Byte Array to Byte Array
\r
51 * @throws NoSuchAlgorithmException
\r
53 public static byte[] encryptMD5 (byte[] input, int offset, int length) throws NoSuchAlgorithmException {
\r
54 MessageDigest md = MessageDigest.getInstance("MD5");
\r
55 md.update(input,offset,length);
\r
62 * Convenience Function: Encrypt MD5 from String to String Hex representation
\r
66 * @throws NoSuchAlgorithmException
\r
68 public static String encryptMD5asStringHex(String input) throws NoSuchAlgorithmException {
\r
69 byte[] output = encryptMD5(input.getBytes());
\r
70 StringBuilder sb = new StringBuilder("0x");
\r
71 for (byte b : output) {
\r
72 sb.append(hexDigit[(b >> 4) & 0x0f]);
\r
73 sb.append(hexDigit[b & 0x0f]);
\r
75 return sb.toString();
\r
78 /////////////////////////////////
\r
80 /////////////////////////////////
\r
84 public static byte[] hashSHA256(byte[] input) throws NoSuchAlgorithmException {
\r
85 MessageDigest md = MessageDigest.getInstance("SHA-256");
\r
93 public static byte[] hashSHA256(byte[] input, int offset, int length) throws NoSuchAlgorithmException {
\r
94 MessageDigest md = MessageDigest.getInstance("SHA-256");
\r
95 md.update(input,offset,length);
\r
100 * Convenience Function: Hash from String to String Hex representation
\r
104 * @throws NoSuchAlgorithmException
\r
106 public static String hashSHA256asStringHex(String input) throws NoSuchAlgorithmException {
\r
107 byte[] output = hashSHA256(input.getBytes());
\r
108 StringBuilder sb = new StringBuilder("0x");
\r
109 for (byte b : output) {
\r
110 sb.append(hexDigit[(b >> 4) & 0x0f]);
\r
111 sb.append(hexDigit[b & 0x0f]);
\r
113 return sb.toString();
\r
117 * Convenience Function: Hash from String to String Hex representation
\r
121 * @throws NoSuchAlgorithmException
\r
123 public static String hashSHA256asStringHex(String input, int salt) throws NoSuchAlgorithmException {
\r
124 byte[] in = input.getBytes();
\r
125 ByteBuffer bb = ByteBuffer.allocate(Integer.SIZE + in.length);
\r
127 bb.put(input.getBytes());
\r
128 byte[] output = Hash.hashSHA256(bb.array());
\r
129 StringBuilder sb = new StringBuilder("0x");
\r
130 for (byte b : output) {
\r
131 sb.append(hexDigit[(b >> 4) & 0x0f]);
\r
132 sb.append(hexDigit[b & 0x0f]);
\r
134 return sb.toString();
\r
138 * Compare two byte arrays for equivalency
\r
143 public static boolean isEqual(byte ba1[], byte ba2[]) {
\r
144 if(ba1.length!=ba2.length)return false;
\r
145 for(int i = 0;i<ba1.length; ++i) {
\r
146 if(ba1[i]!=ba2[i])return false;
\r
151 public static int compareTo(byte[] a, byte[] b) {
\r
152 int end = Math.min(a.length, b.length);
\r
154 for(int i=0;compare == 0 && i<end;++i) {
\r
155 compare = a[i]-b[i];
\r
157 if(compare==0)compare=a.length-b.length;
\r
161 public static String toHex(byte[] ba) {
\r
162 StringBuilder sb = new StringBuilder("0x");
\r
163 for (byte b : ba) {
\r
164 sb.append(hexDigit[(b >> 4) & 0x0f]);
\r
165 sb.append(hexDigit[b & 0x0f]);
\r
167 return sb.toString();
\r
170 public static byte[] fromHex(String s) throws CadiException{
\r
171 if(!s.startsWith("0x")) {
\r
172 throw new CadiException("HexString must start with \"0x\"");
\r
174 boolean high = true;
\r
177 byte[] ba = new byte[(s.length()-2)/2];
\r
179 for(int i=2;i<s.length();++i) {
\r
181 if(c>=0x30 && c<=0x39) {
\r
183 } else if(c>=0x61 && c<=0x66) {
\r
184 b=(byte)(c-0x57); // account for "A"
\r
185 } else if(c>=0x41 && c<=0x46) {
\r
188 throw new CadiException("Invalid char '" + c + "' in HexString");
\r
192 ba[idx]=(byte)(b<<4);
\r