Moving all files to root directory
[appc.git] / appc-common / src / main / java / org / openecomp / appc / encryption / HexHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
22 package org.openecomp.appc.encryption;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * HexHelper utility used for encryption/decryption
32  */
33 public final class HexHelper {
34
35     @SuppressWarnings({
36         "javadoc", "nls"
37     })
38     public static final String CM_PATH = "@(#) [viewpath]/[item]";
39
40     @SuppressWarnings({
41         "nls", "javadoc"
42     })
43     public static final String CM_PROJECT = "@(#) [environment] [baseline]";
44
45     @SuppressWarnings({
46         "javadoc", "nls"
47     })
48     public static final String CM_VERSION = "@(#) [version] [crtime]";
49
50     private static final char[] HEX_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
51         'E', 'F' };
52
53     /**
54      * The logger for this class.
55      */
56     private static final Logger LOG = LoggerFactory.getLogger(HexHelper.class);
57
58     private static Map<Character, Integer> TextToHex;
59
60     static {
61         TextToHex = new HashMap<>();
62         TextToHex.put(Character.valueOf('0'), Integer.valueOf(0));
63         TextToHex.put(Character.valueOf('1'), Integer.valueOf(1));
64         TextToHex.put(Character.valueOf('2'), Integer.valueOf(2));
65         TextToHex.put(Character.valueOf('3'), Integer.valueOf(3));
66         TextToHex.put(Character.valueOf('4'), Integer.valueOf(4));
67         TextToHex.put(Character.valueOf('5'), Integer.valueOf(5));
68         TextToHex.put(Character.valueOf('6'), Integer.valueOf(6));
69         TextToHex.put(Character.valueOf('7'), Integer.valueOf(7));
70         TextToHex.put(Character.valueOf('8'), Integer.valueOf(8));
71         TextToHex.put(Character.valueOf('9'), Integer.valueOf(9));
72         TextToHex.put(Character.valueOf('A'), Integer.valueOf(10));
73         TextToHex.put(Character.valueOf('B'), Integer.valueOf(11));
74         TextToHex.put(Character.valueOf('C'), Integer.valueOf(12));
75         TextToHex.put(Character.valueOf('D'), Integer.valueOf(13));
76         TextToHex.put(Character.valueOf('E'), Integer.valueOf(14));
77         TextToHex.put(Character.valueOf('F'), Integer.valueOf(15));
78     }
79
80     /**
81      * Default private constructor prevents instantiation
82      */
83     private HexHelper() {
84         // no-op
85     }
86
87     /**
88      * Converts an array of bytes to the equivalent string representation using hexadecimal notation and returning that
89      * representation in a StringBuffer.
90      * 
91      * @param bytes
92      *            The bytes to be converted to a hexadecimal string
93      * @return The string representation
94      */
95     public static StringBuffer convertBytesToHexSB(byte[] bytes) {
96         StringBuffer sb = new StringBuffer(bytes.length * 2);
97         int byteLen = bytes.length;
98         for (int index = 0; index < byteLen; index++) {
99             char tempChar;
100             // Get the first 4 bits (high) Do bitwise logical AND to get rid of
101             // low nibble. Shift results to right by 4 and get char
102             // representation
103             tempChar = HEX_TABLE[((bytes[index] & 0xf0) >>> 4)];
104             sb.append(tempChar);
105
106             // Get the last 4 bits (low) Do bitwise logical AND to get rid of
107             // high nibble. Get char representation
108             tempChar = HEX_TABLE[(bytes[index] & 0x0f)];
109             sb.append(tempChar);
110         }
111         return sb;
112     }
113
114     /**
115      * Converts a hexadecimal string representation of a binary value to an array of bytes
116      * 
117      * @param hexValue
118      *            The hex representation string to be converted
119      * @return The array of bytes that contains the binary value
120      */
121     @SuppressWarnings("nls")
122     public static byte[] convertHexToBytes(String hexValue) {
123         byte[] bytes = null;
124         byte high;
125         byte low;
126         char hexChar;
127
128         StringBuffer buffer = new StringBuffer(hexValue.toUpperCase());
129         if (buffer.length() % 2 != 0) {
130             LOG.warn("Invalid HEX value length. "
131                 + "The length of the value has to be a multiple of 2. Prepending '0' value.");
132             buffer.insert(0, '0');
133         }
134         int hexLength = buffer.length();
135         int byteLength = hexLength / 2;
136
137         bytes = new byte[byteLength];
138         for (int index = 0; index < hexLength; index += 2) {
139             hexChar = buffer.charAt(index);
140             high = (TextToHex.get(Character.valueOf(hexChar))).byteValue();
141             high = (byte) (high << 4);
142             hexChar = buffer.charAt(index + 1);
143             low = (TextToHex.get(Character.valueOf(hexChar))).byteValue();
144             high = (byte) (high | low);
145             bytes[index / 2] = high;
146         }
147         return bytes;
148     }
149 }