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