2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
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=========================================================
21 package org.onap.aai.sparky.util;
24 * The Class EncryptConvertor.
26 public class EncryptConvertor {
28 private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
31 * toHexString(String) - convert a string into its hex equivalent.
36 public static final String toHexString(String buf) {
40 return toHexString(buf.getBytes());
44 * toHexString(byte[]) - convert a byte-string into its hex equivalent.
49 public static final String toHexString(byte[] buf) {
54 char[] chars = new char[2 * buf.length];
55 for (int i = 0; i < buf.length; ++i) {
56 chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
57 chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
59 return new String(chars);
63 * Convert a hex string to its equivalent value.
65 * @param hexString the hex string
67 * @throws Exception the exception
69 public static final String stringFromHex(String hexString) throws Exception {
70 if (hexString == null) {
73 return stringFromHex(hexString.toCharArray());
79 * @param hexCharArray the hex char array
81 * @throws Exception the exception
83 public static final String stringFromHex(char[] hexCharArray) throws Exception {
84 if (hexCharArray == null) {
87 return new String(bytesFromHex(hexCharArray));
93 * @param hexString the hex string
95 * @throws Exception the exception
97 public static final byte[] bytesFromHex(String hexString) throws Exception {
98 if (hexString == null) {
101 return bytesFromHex(hexString.toCharArray());
107 * @param hexCharArray the hex char array
109 * @throws Exception the exception
111 public static final byte[] bytesFromHex(char[] hexCharArray) throws Exception {
112 if (hexCharArray == null) {
115 int len = hexCharArray.length;
116 if ((len % 2) != 0) {
117 throw new Exception("Odd number of characters: '" + String.valueOf(hexCharArray) + "'");
119 byte[] txtInByte = new byte[len / 2];
121 for (int i = 0; i < len; i += 2) {
122 txtInByte[counter++] =
123 (byte) (((fromHexDigit(hexCharArray[i], i) << 4) | fromHexDigit(hexCharArray[i + 1], i))
133 * @param index the index
135 * @throws Exception the exception
137 protected static final int fromHexDigit(char ch, int index) throws Exception {
138 int digit = Character.digit(ch, 16);
140 throw new Exception("Illegal hex character '" + ch + "' at index " + index);