[VID-3] Setting docker image tag
[vid.git] / vid / src / main / java / org / openecomp / vid / encryption / EncryptConvertor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.vid.encryption;
22
23 import java.lang.Character;
24
25 /**
26  * The Class EncryptConvertor.
27  */
28 public class EncryptConvertor {
29
30   /** The Constant HEX_CHARS. */
31   private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
32
33   /**
34    * toHexString(String) - convert a string into its hex equivalent.
35    *
36    * @param buf the buf
37    * @return the string
38    */
39   public final static String toHexString(String buf) {
40     if (buf == null) return "";
41     return toHexString(buf.getBytes());
42   }
43
44   /**
45    * toHexString(byte[]) - convert a byte-string into its hex equivalent.
46    *
47    * @param buf the buf
48    * @return the string
49    */
50   public final static String toHexString(byte[] buf) {
51
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];
57     }
58     return new String(chars);
59   }   
60
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'
64 // };
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]);
71 //      }
72 //      return s.toString();
73 // }
74
75   /**
76      * Convert a hex string to its equivalent value.
77      *
78      * @param hexString the hex string
79      * @return the string
80      * @throws Exception the exception
81      */
82   public final static String stringFromHex(String hexString) throws Exception
83   {
84     if (hexString == null) return "";
85     return stringFromHex(hexString.toCharArray());
86   }
87
88   /**
89    * String from hex.
90    *
91    * @param hexCharArray the hex char array
92    * @return the string
93    * @throws Exception the exception
94    */
95   public final static String stringFromHex(char[] hexCharArray)
96   throws Exception {
97     if (hexCharArray == null) return "";
98     return new String(bytesFromHex(hexCharArray));
99   }
100
101   /**
102    * Bytes from hex.
103    *
104    * @param hexString the hex string
105    * @return the byte[]
106    * @throws Exception the exception
107    */
108   public final static byte[] bytesFromHex(String hexString) throws Exception
109   {
110     if (hexString == null) return new byte[0];
111     return bytesFromHex(hexString.toCharArray());
112   }
113
114   /**
115    * Bytes from hex.
116    *
117    * @param hexCharArray the hex char array
118    * @return the byte[]
119    * @throws Exception the exception
120    */
121   public final static byte[] bytesFromHex(char[] hexCharArray)
122   throws Exception {
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];
127     int j = 0;
128     for (int i = 0; i < len; i += 2) {
129       txtInByte[j++] = (byte)(((fromHexDigit(hexCharArray[i], i) << 4) | fromHexDigit(hexCharArray[i+1], i)) & 0xFF);
130     }
131     return txtInByte;
132   }
133
134   /**
135    * From hex digit.
136    *
137    * @param ch the ch
138    * @param index the index
139    * @return the int
140    * @throws Exception the exception
141    */
142   protected final static int fromHexDigit(char ch, int index) throws Exception
143   {
144     int digit = Character.digit(ch, 16);
145     if (digit == -1) throw new Exception("Illegal hex character '" + ch + "' at index " + index);
146       return digit;
147   }
148
149   // UNIT TESTS (same junit, but we want to run from command line
150
151   /**
152    * Check to hex string B.
153    *
154    * @param arg the arg
155    * @param expected the expected
156    * @return true, if successful
157    */
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);
163   }
164
165   /**
166    * Check to hex string.
167    *
168    * @param arg the arg
169    * @param expected the expected
170    * @return true, if successful
171    */
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);
177   }
178
179   /**
180    * Check from hex string.
181    *
182    * @param arg the arg
183    * @param expected the expected
184    * @return true, if successful
185    */
186   public static boolean checkFromHexString(String arg, String expected) {
187         try {
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());
194             return false;
195         }
196   }
197
198   /**
199    * Check from hex string B.
200    *
201    * @param arg the arg
202    * @param expected the expected
203    * @return true, if successful
204    */
205   public static boolean checkFromHexStringB(String arg, String expected) {
206         try {
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());
214             return false;
215         }
216   }
217
218
219   /**
220    * The main method.
221    *
222    * @param args the arguments
223    */
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"));
252     try {
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());
258     }
259   }
260
261 }