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