Changing the license and trademark
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / util / EncryptConvertor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sparky.util;
24
25 /**
26  * The Class EncryptConvertor.
27  */
28 public class EncryptConvertor {
29
30   private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
31
32   /**
33    * toHexString(String) - convert a string into its hex equivalent.
34    *
35    * @param buf the buf
36    * @return the string
37    */
38   public static final String toHexString(String buf) {
39     if (buf == null) {
40       return "";
41     }
42     return toHexString(buf.getBytes());
43   }
44
45   /**
46    * toHexString(byte[]) - convert a byte-string into its hex equivalent.
47    *
48    * @param buf the buf
49    * @return the string
50    */
51   public static final String toHexString(byte[] buf) {
52
53     if (buf == null) {
54       return "";
55     }
56     char[] chars = new char[2 * buf.length];
57     for (int i = 0; i < buf.length; ++i) {
58       chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
59       chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
60     }
61     return new String(chars);
62   }
63
64   /**
65    * Convert a hex string to its equivalent value.
66    *
67    * @param hexString the hex string
68    * @return the string
69    * @throws Exception the exception
70    */
71   public static final String stringFromHex(String hexString) throws Exception {
72     if (hexString == null) {
73       return "";
74     }
75     return stringFromHex(hexString.toCharArray());
76   }
77
78   /**
79    * String from hex.
80    *
81    * @param hexCharArray the hex char array
82    * @return the string
83    * @throws Exception the exception
84    */
85   public static final String stringFromHex(char[] hexCharArray) throws Exception {
86     if (hexCharArray == null) {
87       return "";
88     }
89     return new String(bytesFromHex(hexCharArray));
90   }
91
92   /**
93    * Bytes from hex.
94    *
95    * @param hexString the hex string
96    * @return the byte[]
97    * @throws Exception the exception
98    */
99   public static final byte[] bytesFromHex(String hexString) throws Exception {
100     if (hexString == null) {
101       return new byte[0];
102     }
103     return bytesFromHex(hexString.toCharArray());
104   }
105
106   /**
107    * Bytes from hex.
108    *
109    * @param hexCharArray the hex char array
110    * @return the byte[]
111    * @throws Exception the exception
112    */
113   public static final byte[] bytesFromHex(char[] hexCharArray) throws Exception {
114     if (hexCharArray == null) {
115       return new byte[0];
116     }
117     int len = hexCharArray.length;
118     if ((len % 2) != 0) {
119       throw new Exception("Odd number of characters: '" + String.valueOf(hexCharArray) + "'");
120     }
121     byte[] txtInByte = new byte[len / 2];
122     int counter = 0;
123     for (int i = 0; i < len; i += 2) {
124       txtInByte[counter++] =
125           (byte) (((fromHexDigit(hexCharArray[i], i) << 4) | fromHexDigit(hexCharArray[i + 1], i))
126               & 0xFF);
127     }
128     return txtInByte;
129   }
130
131   /**
132    * From hex digit.
133    *
134    * @param ch the ch
135    * @param index the index
136    * @return the int
137    * @throws Exception the exception
138    */
139   protected static final int fromHexDigit(char ch, int index) throws Exception {
140     int digit = Character.digit(ch, 16);
141     if (digit == -1) {
142       throw new Exception("Illegal hex character '" + ch + "' at index " + index);
143     }
144     return digit;
145   }
146
147 }