2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.encryption;
26 import java.util.HashMap;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * HexHelper utility used for encryption/decryption
35 public final class HexHelper {
40 public static final String CM_PATH = "@(#) [viewpath]/[item]";
45 public static final String CM_PROJECT = "@(#) [environment] [baseline]";
50 public static final String CM_VERSION = "@(#) [version] [crtime]";
52 private static final char[] HEX_TABLE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
56 * The logger for this class.
58 private static final Logger LOG = LoggerFactory.getLogger(HexHelper.class);
60 private static Map<Character, Integer> textToHex;
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);
83 * Default private constructor prevents instantiation
90 * Converts an array of bytes to the equivalent string representation using hexadecimal notation
92 * @param bytes The bytes to be converted to a hexadecimal string
93 * @return The string representation
95 public static String convertBytesToHex(byte[] bytes) throws EncryptionException{
98 throw new EncryptionException("Given byte array is null");
100 StringBuilder builder = new StringBuilder(bytes.length * 2);
101 for (byte aByte : bytes) {
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
106 tempChar = HEX_TABLE[(aByte & 0xf0) >>> 4];
107 builder.append(tempChar);
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);
114 return builder.toString();
118 * Converts a hexadecimal string representation of a binary value to an array of bytes
120 * @param hexValue The hex representation string to be converted
121 * @return The array of bytes that contains the binary value
123 @SuppressWarnings("nls")
124 public static byte[] convertHexToBytes(String hexValue) throws EncryptionException {
127 throw new EncryptionException("Given hex value is null");
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');
140 int hexLength = builder.length();
141 int byteLength = hexLength / 2;
143 bytes = new byte[byteLength];
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;
155 catch (NullPointerException e){
156 LOG.error("Given string contains not hexadecimal values", e);
157 throw new EncryptionException("Given string contains not hexadecimal values");