1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
12 * * http://www.apache.org/licenses/LICENSE-2.0
\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
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cadi;
\r
26 import java.nio.ByteBuffer;
\r
27 import java.security.MessageDigest;
\r
28 import java.security.NoSuchAlgorithmException;
\r
31 private static char hexDigit[] = "0123456789abcdef".toCharArray();
\r
33 /////////////////////////////////
\r
35 /////////////////////////////////
\r
37 * Encrypt MD5 from Byte Array to Byte Array
\r
40 * @throws NoSuchAlgorithmException
\r
42 public static byte[] encryptMD5 (byte[] input) throws NoSuchAlgorithmException {
\r
43 MessageDigest md = MessageDigest.getInstance("MD5");
\r
49 * Encrypt MD5 from Byte Array to Byte Array
\r
52 * @throws NoSuchAlgorithmException
\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
63 * Convenience Function: Encrypt MD5 from String to String Hex representation
\r
67 * @throws NoSuchAlgorithmException
\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
76 return sb.toString();
\r
79 /////////////////////////////////
\r
81 /////////////////////////////////
\r
85 public static byte[] hashSHA256(byte[] input) throws NoSuchAlgorithmException {
\r
86 MessageDigest md = MessageDigest.getInstance("SHA-256");
\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
101 * Convenience Function: Hash from String to String Hex representation
\r
105 * @throws NoSuchAlgorithmException
\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
114 return sb.toString();
\r
118 * Convenience Function: Hash from String to String Hex representation
\r
122 * @throws NoSuchAlgorithmException
\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
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
135 return sb.toString();
\r
139 * Compare two byte arrays for equivalency
\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
152 public static int compareTo(byte[] a, byte[] b) {
\r
153 int end = Math.min(a.length, b.length);
\r
155 for(int i=0;compare == 0 && i<end;++i) {
\r
156 compare = a[i]-b[i];
\r
158 if(compare==0)compare=a.length-b.length;
\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
168 return sb.toString();
\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
175 boolean high = true;
\r
178 byte[] ba = new byte[(s.length()-2)/2];
\r
180 for(int i=2;i<s.length();++i) {
\r
182 if(c>=0x30 && c<=0x39) {
\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
189 throw new CadiException("Invalid char '" + c + "' in HexString");
\r
193 ba[idx]=(byte)(b<<4);
\r