Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / util / EncryptConvertor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 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 package org.onap.aai.sparky.util;
22
23 /**
24  * The Class EncryptConvertor.
25  */
26 public class EncryptConvertor {
27
28   private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
29
30   /**
31    * toHexString(String) - convert a string into its hex equivalent.
32    *
33    * @param buf the buf
34    * @return the string
35    */
36   public static final String toHexString(String buf) {
37     if (buf == null) {
38       return "";
39     }
40     return toHexString(buf.getBytes());
41   }
42
43   /**
44    * toHexString(byte[]) - convert a byte-string into its hex equivalent.
45    *
46    * @param buf the buf
47    * @return the string
48    */
49   public static final String toHexString(byte[] buf) {
50
51     if (buf == null) {
52       return "";
53     }
54     char[] chars = new char[2 * buf.length];
55     for (int i = 0; i < buf.length; ++i) {
56       chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
57       chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
58     }
59     return new String(chars);
60   }
61
62   /**
63    * Convert a hex string to its equivalent value.
64    *
65    * @param hexString the hex string
66    * @return the string
67    * @throws Exception the exception
68    */
69   public static final String stringFromHex(String hexString) throws Exception {
70     if (hexString == null) {
71       return "";
72     }
73     return stringFromHex(hexString.toCharArray());
74   }
75
76   /**
77    * String from hex.
78    *
79    * @param hexCharArray the hex char array
80    * @return the string
81    * @throws Exception the exception
82    */
83   public static final String stringFromHex(char[] hexCharArray) throws Exception {
84     if (hexCharArray == null) {
85       return "";
86     }
87     return new String(bytesFromHex(hexCharArray));
88   }
89
90   /**
91    * Bytes from hex.
92    *
93    * @param hexString the hex string
94    * @return the byte[]
95    * @throws Exception the exception
96    */
97   public static final byte[] bytesFromHex(String hexString) throws Exception {
98     if (hexString == null) {
99       return new byte[0];
100     }
101     return bytesFromHex(hexString.toCharArray());
102   }
103
104   /**
105    * Bytes from hex.
106    *
107    * @param hexCharArray the hex char array
108    * @return the byte[]
109    * @throws Exception the exception
110    */
111   public static final byte[] bytesFromHex(char[] hexCharArray) throws Exception {
112     if (hexCharArray == null) {
113       return new byte[0];
114     }
115     int len = hexCharArray.length;
116     if ((len % 2) != 0) {
117       throw new Exception("Odd number of characters: '" + String.valueOf(hexCharArray) + "'");
118     }
119     byte[] txtInByte = new byte[len / 2];
120     int counter = 0;
121     for (int i = 0; i < len; i += 2) {
122       txtInByte[counter++] =
123           (byte) (((fromHexDigit(hexCharArray[i], i) << 4) | fromHexDigit(hexCharArray[i + 1], i))
124               & 0xFF);
125     }
126     return txtInByte;
127   }
128
129   /**
130    * From hex digit.
131    *
132    * @param ch the ch
133    * @param index the index
134    * @return the int
135    * @throws Exception the exception
136    */
137   protected static final int fromHexDigit(char ch, int index) throws Exception {
138     int digit = Character.digit(ch, 16);
139     if (digit == -1) {
140       throw new Exception("Illegal hex character '" + ch + "' at index " + index);
141     }
142     return digit;
143   }
144
145 }