aboutsummaryrefslogtreecommitdiffstats
path: root/vid/src/main/java/org/openecomp/vid/encryption/EncryptConvertor.java
blob: 51a5ee2e34128aea787ae1ded3a15baf3d53a540 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.vid.encryption;

import java.lang.Character;

/**
 * The Class EncryptConvertor.
 */
public class EncryptConvertor {

  /** The Constant HEX_CHARS. */
  private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

  /**
   * toHexString(String) - convert a string into its hex equivalent.
   *
   * @param buf the buf
   * @return the string
   */
  public final static String toHexString(String buf) {
    if (buf == null) return "";
    return toHexString(buf.getBytes());
  }

  /**
   * toHexString(byte[]) - convert a byte-string into its hex equivalent.
   *
   * @param buf the buf
   * @return the string
   */
  public final static String toHexString(byte[] buf) {

    if (buf == null) return "";
    char[] chars = new char[2 * buf.length];
    for (int i = 0; i < buf.length; ++i) {
      chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
      chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
    }
    return new String(chars);
  }   

    // alternate implementation that's slightly slower
// protected static final byte[] Hexhars = {
//	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
// };
// public static String encode(byte[] b) {
//	StringBuilder s = new StringBuilder(2 * b.length);
//	for (int i = 0; i < b.length; i++) {
//	    int v = b[i] & 0xff;
//	    s.append((char)Hexhars[v >> 4]);
//	    s.append((char)Hexhars[v & 0xf]);
//	}
//	return s.toString();
// }

  /**
     * Convert a hex string to its equivalent value.
     *
     * @param hexString the hex string
     * @return the string
     * @throws Exception the exception
     */
  public final static String stringFromHex(String hexString) throws Exception
  {
    if (hexString == null) return "";
    return stringFromHex(hexString.toCharArray());
  }

  /**
   * String from hex.
   *
   * @param hexCharArray the hex char array
   * @return the string
   * @throws Exception the exception
   */
  public final static String stringFromHex(char[] hexCharArray)
  throws Exception {
    if (hexCharArray == null) return "";
    return new String(bytesFromHex(hexCharArray));
  }

  /**
   * Bytes from hex.
   *
   * @param hexString the hex string
   * @return the byte[]
   * @throws Exception the exception
   */
  public final static byte[] bytesFromHex(String hexString) throws Exception
  {
    if (hexString == null) return new byte[0];
    return bytesFromHex(hexString.toCharArray());
  }

  /**
   * Bytes from hex.
   *
   * @param hexCharArray the hex char array
   * @return the byte[]
   * @throws Exception the exception
   */
  public final static byte[] bytesFromHex(char[] hexCharArray)
  throws Exception {
    if (hexCharArray == null) return new byte[0];
    int len = hexCharArray.length;
    if ((len % 2) != 0) throw new Exception("Odd number of characters: '" + String.valueOf(hexCharArray) + "'");
    byte [] txtInByte = new byte [len / 2];
    int j = 0;
    for (int i = 0; i < len; i += 2) {
      txtInByte[j++] = (byte)(((fromHexDigit(hexCharArray[i], i) << 4) | fromHexDigit(hexCharArray[i+1], i)) & 0xFF);
    }
    return txtInByte;
  }

  /**
   * From hex digit.
   *
   * @param ch the ch
   * @param index the index
   * @return the int
   * @throws Exception the exception
   */
  protected final static int fromHexDigit(char ch, int index) throws Exception
  {
    int digit = Character.digit(ch, 16);
    if (digit == -1) throw new Exception("Illegal hex character '" + ch + "' at index " + index);
      return digit;
  }

  // UNIT TESTS (same junit, but we want to run from command line

  /**
   * Check to hex string B.
   *
   * @param arg the arg
   * @param expected the expected
   * @return true, if successful
   */
  public static boolean checkToHexStringB(String arg, String expected) {
	String ret = toHexString(arg != null ? arg.getBytes() : null);
	System.out.println("toHexString(" + arg + ")=> " + ret);
	if (!ret.equals(expected)) System.out.println("\tWRONG, expected: " + expected);
	return ret.equals(expected);
  }

  /**
   * Check to hex string.
   *
   * @param arg the arg
   * @param expected the expected
   * @return true, if successful
   */
  public static boolean checkToHexString(String arg, String expected) {
	String ret = toHexString(arg);
	System.out.println("toHexString(" + arg + ")=> " + ret);
	if (!ret.equals(expected)) System.out.println("\tWRONG, expected: " + expected);
	return ret.equals(expected);
  }

  /**
   * Check from hex string.
   *
   * @param arg the arg
   * @param expected the expected
   * @return true, if successful
   */
  public static boolean checkFromHexString(String arg, String expected) {
	try {
	    String ret = stringFromHex(arg);
	    System.out.println("fromHexString(" + arg + ")=> " + ret);
	    if (!ret.equals(expected)) System.out.println("\tWRONG, expected: " + expected);
	    return ret.equals(expected);
	} catch (Exception e) {
		System.out.println("Caught exception: " + e.toString());
	    return false;
	}
  }

  /**
   * Check from hex string B.
   *
   * @param arg the arg
   * @param expected the expected
   * @return true, if successful
   */
  public static boolean checkFromHexStringB(String arg, String expected) {
	try {
	    byte[] ret = bytesFromHex(arg);
	    String sret = new String(ret);
	    System.out.println("fromHexString(" + arg + ")=> " + sret);
	    if (!sret.equals(expected)) System.out.println("\tWRONG, expected: " + expected);
	    return sret.equals(expected);
	} catch (Exception e) {
		System.out.println("Caught exception: " + e.toString());
	    return false;
	}
  }


  /**
   * The main method.
   *
   * @param args the arguments
   */
  public static void main(String[] args) {
	// TODO Auto-generated method stub
    int pass = 0, fail = 0;
    if (checkToHexString("", "")) pass++; else fail++;
    if (checkToHexString(null, "")) pass++; else fail++;
    if (checkToHexString("0", "30")) pass++; else fail++;
    if (checkToHexString("abc", "616263")) pass++; else fail++;
    if (checkToHexString("!@#$%^&*()", "21402324255e262a2829")) pass++; else fail++;
    if (checkToHexStringB("", "")) pass++; else fail++;
    if (checkToHexStringB(null, "")) pass++; else fail++;
    if (checkToHexStringB("0", "30")) pass++; else fail++;
    if (checkToHexStringB("abc", "616263")) pass++; else fail++;
    if (checkToHexStringB("!@#$%^&*()", "21402324255e262a2829")) pass++; else fail++;
    if (checkFromHexString("", "")) pass++; else fail++;
    if (checkFromHexString(null, "")) pass++; else fail++;
    if (checkFromHexString("30", "0")) pass++; else fail++;
    if (checkFromHexString("616263", "abc")) pass++; else fail++;
    if (checkFromHexString("21402324255e262a2829", "!@#$%^&*()")) pass++; else fail++;
    if (checkFromHexStringB("", "")) pass++; else fail++;
    if (checkFromHexStringB(null, "")) pass++; else fail++;
    if (checkFromHexStringB("30", "0")) pass++; else fail++;
    if (checkFromHexStringB("616263", "abc")) pass++; else fail++;
    if (checkFromHexStringB("21402324255e262a2829", "!@#$%^&*()")) pass++; else fail++;
    System.out.println("Tests passed: " + Integer.toString(pass));
    System.out.println("Tests failed: " + Integer.toString(fail));
    System.out.println("=======");
    System.out.println("abc toHex = " + toHexString("abc"));
    System.out.println("123 toHex = " + toHexString("123"));
    try {
    System.out.println("616263 FromHex = " + stringFromHex("616263"));
    System.out.println("313233 FromHex = " + stringFromHex("313233"));
    //System.out.println("current key FromHex = " + stringFromHex("57ajqe{kJjjarj}G#(3)ea7"));
    } catch (Exception e) {
      System.out.println("exception: " + e.toString());
    }
  }

}