Fix request handler class error
[appc.git] / appc-common / src / main / java / org / onap / appc / encryption / HexHelper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.encryption;
25
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * HexHelper utility used for encryption/decryption
34  */
35 public final class HexHelper {
36
37     @SuppressWarnings({
38         "javadoc", "nls"
39     })
40     public static final String CM_PATH = "@(#) [viewpath]/[item]";
41
42     @SuppressWarnings({
43         "nls", "javadoc"
44     })
45     public static final String CM_PROJECT = "@(#) [environment] [baseline]";
46
47     @SuppressWarnings({
48         "javadoc", "nls"
49     })
50     public static final String CM_VERSION = "@(#) [version] [crtime]";
51
52     private static final char[] HEX_TABLE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
53         'E', 'F'};
54
55     /**
56      * The logger for this class.
57      */
58     private static final Logger LOG = LoggerFactory.getLogger(HexHelper.class);
59
60     private static Map<Character, Integer> textToHex;
61
62     static {
63         textToHex = new HashMap<>();
64         textToHex.put('0', 0);
65         textToHex.put('1', 1);
66         textToHex.put('2', 2);
67         textToHex.put('3', 3);
68         textToHex.put('4', 4);
69         textToHex.put('5', 5);
70         textToHex.put('6', 6);
71         textToHex.put('7', 7);
72         textToHex.put('8', 8);
73         textToHex.put('9', 9);
74         textToHex.put('A', 10);
75         textToHex.put('B', 11);
76         textToHex.put('C', 12);
77         textToHex.put('D', 13);
78         textToHex.put('E', 14);
79         textToHex.put('F', 15);
80     }
81
82     /**
83      * Default private constructor prevents instantiation
84      */
85     private HexHelper() {
86         // no-op
87     }
88
89     /**
90      * Converts an array of bytes to the equivalent string representation using hexadecimal notation
91      *
92      * @param bytes The bytes to be converted to a hexadecimal string
93      * @return The string representation
94      */
95     public static String convertBytesToHex(byte[] bytes) throws EncryptionException{
96
97         if (bytes == null)
98             throw new EncryptionException("Given byte array is null");
99
100         StringBuilder builder = new StringBuilder(bytes.length * 2);
101         for (byte aByte : bytes) {
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[(aByte & 0xf0) >>> 4];
107             builder.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[aByte & 0x0f];
112             builder.append(tempChar);
113         }
114         return builder.toString();
115     }
116
117     /**
118      * Converts a hexadecimal string representation of a binary value to an array of bytes
119      *
120      * @param hexValue The hex representation string to be converted
121      * @return The array of bytes that contains the binary value
122      */
123     @SuppressWarnings("nls")
124     public static byte[] convertHexToBytes(String hexValue) throws EncryptionException {
125
126         if (hexValue ==null)
127             throw new EncryptionException("Given hex value is null");
128
129         byte[] bytes;
130         byte high;
131         byte low;
132         char hexChar;
133
134         StringBuilder builder = new StringBuilder(hexValue.toUpperCase());
135         if (builder.length() % 2 != 0) {
136             LOG.warn("Invalid HEX value length. The length of the value has to be a multiple of 2."
137                 + " Prepending '0' value.");
138             builder.insert(0, '0');
139         }
140         int hexLength = builder.length();
141         int byteLength = hexLength / 2;
142
143         bytes = new byte[byteLength];
144         try {
145             for (int index = 0; index < hexLength; index += 2) {
146                 hexChar = builder.charAt(index);
147                 high = textToHex.get(hexChar).byteValue();
148                 high = (byte) (high << 4);
149                 hexChar = builder.charAt(index + 1);
150                 low = textToHex.get(hexChar).byteValue();
151                 high = (byte) (high | low);
152                 bytes[index / 2] = high;
153             }
154         }
155         catch (NullPointerException e){
156             LOG.error("Given string contains not hexadecimal values", e);
157             throw new EncryptionException("Given string contains not hexadecimal values");
158         }
159
160         return bytes;
161     }
162 }