2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.encryption;
24 import java.util.HashMap;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * HexHelper utility used for encryption/decryption
33 public final class HexHelper {
38 public static final String CM_PATH = "@(#) [viewpath]/[item]";
43 public static final String CM_PROJECT = "@(#) [environment] [baseline]";
48 public static final String CM_VERSION = "@(#) [version] [crtime]";
50 private static final char[] HEX_TABLE = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
54 * The logger for this class.
56 private static final Logger LOG = LoggerFactory.getLogger(HexHelper.class);
58 private static Map<Character, Integer> TextToHex;
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));
81 * Default private constructor prevents instantiation
88 * Converts an array of bytes to the equivalent string representation using hexadecimal notation and returning that
89 * representation in a StringBuffer.
92 * The bytes to be converted to a hexadecimal string
93 * @return The string representation
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++) {
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
103 tempChar = HEX_TABLE[((bytes[index] & 0xf0) >>> 4)];
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)];
115 * Converts a hexadecimal string representation of a binary value to an array of bytes
118 * The hex representation string to be converted
119 * @return The array of bytes that contains the binary value
121 @SuppressWarnings("nls")
122 public static byte[] convertHexToBytes(String hexValue) {
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');
134 int hexLength = buffer.length();
135 int byteLength = hexLength / 2;
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;