2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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=========================================================
21 package org.openecomp.vid.encryption;
23 import java.lang.Character;
26 * The Class EncryptConvertor.
28 public class EncryptConvertor {
30 /** The Constant HEX_CHARS. */
31 private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
34 * toHexString(String) - convert a string into its hex equivalent.
39 public final static String toHexString(String buf) {
40 if (buf == null) return "";
41 return toHexString(buf.getBytes());
45 * toHexString(byte[]) - convert a byte-string into its hex equivalent.
50 public final static String toHexString(byte[] buf) {
52 if (buf == null) return "";
53 char[] chars = new char[2 * buf.length];
54 for (int i = 0; i < buf.length; ++i) {
55 chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
56 chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
58 return new String(chars);
61 // alternate implementation that's slightly slower
62 // protected static final byte[] Hexhars = {
63 // '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
65 // public static String encode(byte[] b) {
66 // StringBuilder s = new StringBuilder(2 * b.length);
67 // for (int i = 0; i < b.length; i++) {
68 // int v = b[i] & 0xff;
69 // s.append((char)Hexhars[v >> 4]);
70 // s.append((char)Hexhars[v & 0xf]);
72 // return s.toString();
76 * Convert a hex string to its equivalent value.
78 * @param hexString the hex string
80 * @throws Exception the exception
82 public final static String stringFromHex(String hexString) throws Exception
84 if (hexString == null) return "";
85 return stringFromHex(hexString.toCharArray());
91 * @param hexCharArray the hex char array
93 * @throws Exception the exception
95 public final static String stringFromHex(char[] hexCharArray)
97 if (hexCharArray == null) return "";
98 return new String(bytesFromHex(hexCharArray));
104 * @param hexString the hex string
106 * @throws Exception the exception
108 public final static byte[] bytesFromHex(String hexString) throws Exception
110 if (hexString == null) return new byte[0];
111 return bytesFromHex(hexString.toCharArray());
117 * @param hexCharArray the hex char array
119 * @throws Exception the exception
121 public final static byte[] bytesFromHex(char[] hexCharArray)
123 if (hexCharArray == null) return new byte[0];
124 int len = hexCharArray.length;
125 if ((len % 2) != 0) throw new Exception("Odd number of characters: '" + String.valueOf(hexCharArray) + "'");
126 byte [] txtInByte = new byte [len / 2];
128 for (int i = 0; i < len; i += 2) {
129 txtInByte[j++] = (byte)(((fromHexDigit(hexCharArray[i], i) << 4) | fromHexDigit(hexCharArray[i+1], i)) & 0xFF);
138 * @param index the index
140 * @throws Exception the exception
142 protected final static int fromHexDigit(char ch, int index) throws Exception
144 int digit = Character.digit(ch, 16);
145 if (digit == -1) throw new Exception("Illegal hex character '" + ch + "' at index " + index);
149 // UNIT TESTS (same junit, but we want to run from command line
152 * Check to hex string B.
155 * @param expected the expected
156 * @return true, if successful
158 public static boolean checkToHexStringB(String arg, String expected) {
159 String ret = toHexString(arg != null ? arg.getBytes() : null);
160 System.out.println("toHexString(" + arg + ")=> " + ret);
161 if (!ret.equals(expected)) System.out.println("\tWRONG, expected: " + expected);
162 return ret.equals(expected);
166 * Check to hex string.
169 * @param expected the expected
170 * @return true, if successful
172 public static boolean checkToHexString(String arg, String expected) {
173 String ret = toHexString(arg);
174 System.out.println("toHexString(" + arg + ")=> " + ret);
175 if (!ret.equals(expected)) System.out.println("\tWRONG, expected: " + expected);
176 return ret.equals(expected);
180 * Check from hex string.
183 * @param expected the expected
184 * @return true, if successful
186 public static boolean checkFromHexString(String arg, String expected) {
188 String ret = stringFromHex(arg);
189 System.out.println("fromHexString(" + arg + ")=> " + ret);
190 if (!ret.equals(expected)) System.out.println("\tWRONG, expected: " + expected);
191 return ret.equals(expected);
192 } catch (Exception e) {
193 System.out.println("Caught exception: " + e.toString());
199 * Check from hex string B.
202 * @param expected the expected
203 * @return true, if successful
205 public static boolean checkFromHexStringB(String arg, String expected) {
207 byte[] ret = bytesFromHex(arg);
208 String sret = new String(ret);
209 System.out.println("fromHexString(" + arg + ")=> " + sret);
210 if (!sret.equals(expected)) System.out.println("\tWRONG, expected: " + expected);
211 return sret.equals(expected);
212 } catch (Exception e) {
213 System.out.println("Caught exception: " + e.toString());
222 * @param args the arguments
224 public static void main(String[] args) {
225 // TODO Auto-generated method stub
226 int pass = 0, fail = 0;
227 if (checkToHexString("", "")) pass++; else fail++;
228 if (checkToHexString(null, "")) pass++; else fail++;
229 if (checkToHexString("0", "30")) pass++; else fail++;
230 if (checkToHexString("abc", "616263")) pass++; else fail++;
231 if (checkToHexString("!@#$%^&*()", "21402324255e262a2829")) pass++; else fail++;
232 if (checkToHexStringB("", "")) pass++; else fail++;
233 if (checkToHexStringB(null, "")) pass++; else fail++;
234 if (checkToHexStringB("0", "30")) pass++; else fail++;
235 if (checkToHexStringB("abc", "616263")) pass++; else fail++;
236 if (checkToHexStringB("!@#$%^&*()", "21402324255e262a2829")) pass++; else fail++;
237 if (checkFromHexString("", "")) pass++; else fail++;
238 if (checkFromHexString(null, "")) pass++; else fail++;
239 if (checkFromHexString("30", "0")) pass++; else fail++;
240 if (checkFromHexString("616263", "abc")) pass++; else fail++;
241 if (checkFromHexString("21402324255e262a2829", "!@#$%^&*()")) pass++; else fail++;
242 if (checkFromHexStringB("", "")) pass++; else fail++;
243 if (checkFromHexStringB(null, "")) pass++; else fail++;
244 if (checkFromHexStringB("30", "0")) pass++; else fail++;
245 if (checkFromHexStringB("616263", "abc")) pass++; else fail++;
246 if (checkFromHexStringB("21402324255e262a2829", "!@#$%^&*()")) pass++; else fail++;
247 System.out.println("Tests passed: " + Integer.toString(pass));
248 System.out.println("Tests failed: " + Integer.toString(fail));
249 System.out.println("=======");
250 System.out.println("abc toHex = " + toHexString("abc"));
251 System.out.println("123 toHex = " + toHexString("123"));
253 System.out.println("616263 FromHex = " + stringFromHex("616263"));
254 System.out.println("313233 FromHex = " + stringFromHex("313233"));
255 //System.out.println("current key FromHex = " + stringFromHex("57ajqe{kJjjarj}G#(3)ea7"));
256 } catch (Exception e) {
257 System.out.println("exception: " + e.toString());