2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 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.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.util;
28 * The Class EncryptConvertor.
30 public class EncryptConvertor {
32 private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
35 * toHexString(String) - convert a string into its hex equivalent.
40 public static final String toHexString(String buf) {
44 return toHexString(buf.getBytes());
48 * toHexString(byte[]) - convert a byte-string into its hex equivalent.
53 public static final String toHexString(byte[] buf) {
58 char[] chars = new char[2 * buf.length];
59 for (int i = 0; i < buf.length; ++i) {
60 chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
61 chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
63 return new String(chars);
67 * Convert a hex string to its equivalent value.
69 * @param hexString the hex string
71 * @throws Exception the exception
73 public static final String stringFromHex(String hexString) throws Exception {
74 if (hexString == null) {
77 return stringFromHex(hexString.toCharArray());
83 * @param hexCharArray the hex char array
85 * @throws Exception the exception
87 public static final String stringFromHex(char[] hexCharArray) throws Exception {
88 if (hexCharArray == null) {
91 return new String(bytesFromHex(hexCharArray));
97 * @param hexString the hex string
99 * @throws Exception the exception
101 public static final byte[] bytesFromHex(String hexString) throws Exception {
102 if (hexString == null) {
105 return bytesFromHex(hexString.toCharArray());
111 * @param hexCharArray the hex char array
113 * @throws Exception the exception
115 public static final byte[] bytesFromHex(char[] hexCharArray) throws Exception {
116 if (hexCharArray == null) {
119 int len = hexCharArray.length;
120 if ((len % 2) != 0) {
121 throw new Exception("Odd number of characters: '" + String.valueOf(hexCharArray) + "'");
123 byte[] txtInByte = new byte[len / 2];
125 for (int i = 0; i < len; i += 2) {
126 txtInByte[counter++] =
127 (byte) (((fromHexDigit(hexCharArray[i], i) << 4) | fromHexDigit(hexCharArray[i + 1], i))
137 * @param index the index
139 * @throws Exception the exception
141 protected static final int fromHexDigit(char ch, int index) throws Exception {
142 int digit = Character.digit(ch, 16);
144 throw new Exception("Illegal hex character '" + ch + "' at index " + index);