2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.encryption;
27 import java.util.HashMap;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * HexHelper utility used for encryption/decryption
36 public final class HexHelper {
41 public static final String CM_PATH = "@(#) [viewpath]/[item]";
46 public static final String CM_PROJECT = "@(#) [environment] [baseline]";
51 public static final String CM_VERSION = "@(#) [version] [crtime]";
53 private static final char[] HEX_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
57 * The logger for this class.
59 private static final Logger LOG = LoggerFactory.getLogger(HexHelper.class);
61 private static Map<Character, Integer> TextToHex;
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));
84 * Default private constructor prevents instantiation
91 * Converts an array of bytes to the equivalent string representation using hexadecimal notation and returning that
92 * representation in a StringBuffer.
95 * The bytes to be converted to a hexadecimal string
96 * @return The string representation
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++) {
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[((bytes[index] & 0xf0) >>> 4)];
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)];
118 * Converts a hexadecimal string representation of a binary value to an array of bytes
121 * The hex representation string to be converted
122 * @return The array of bytes that contains the binary value
124 @SuppressWarnings("nls")
125 public static byte[] convertHexToBytes(String hexValue) {
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');
137 int hexLength = buffer.length();
138 int byteLength = hexLength / 2;
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;