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