2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sparky.util;
26 * The Class EncryptConvertor.
28 public class EncryptConvertor {
30 private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
33 * toHexString(String) - convert a string into its hex equivalent.
38 public static final String toHexString(String buf) {
42 return toHexString(buf.getBytes());
46 * toHexString(byte[]) - convert a byte-string into its hex equivalent.
51 public static final String toHexString(byte[] buf) {
56 char[] chars = new char[2 * buf.length];
57 for (int i = 0; i < buf.length; ++i) {
58 chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
59 chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
61 return new String(chars);
65 * Convert a hex string to its equivalent value.
67 * @param hexString the hex string
69 * @throws Exception the exception
71 public static final String stringFromHex(String hexString) throws Exception {
72 if (hexString == null) {
75 return stringFromHex(hexString.toCharArray());
81 * @param hexCharArray the hex char array
83 * @throws Exception the exception
85 public static final String stringFromHex(char[] hexCharArray) throws Exception {
86 if (hexCharArray == null) {
89 return new String(bytesFromHex(hexCharArray));
95 * @param hexString the hex string
97 * @throws Exception the exception
99 public static final byte[] bytesFromHex(String hexString) throws Exception {
100 if (hexString == null) {
103 return bytesFromHex(hexString.toCharArray());
109 * @param hexCharArray the hex char array
111 * @throws Exception the exception
113 public static final byte[] bytesFromHex(char[] hexCharArray) throws Exception {
114 if (hexCharArray == null) {
117 int len = hexCharArray.length;
118 if ((len % 2) != 0) {
119 throw new Exception("Odd number of characters: '" + String.valueOf(hexCharArray) + "'");
121 byte[] txtInByte = new byte[len / 2];
123 for (int i = 0; i < len; i += 2) {
124 txtInByte[counter++] =
125 (byte) (((fromHexDigit(hexCharArray[i], i) << 4) | fromHexDigit(hexCharArray[i + 1], i))
135 * @param index the index
137 * @throws Exception the exception
139 protected static final int fromHexDigit(char ch, int index) throws Exception {
140 int digit = Character.digit(ch, 16);
142 throw new Exception("Illegal hex character '" + ch + "' at index " + index);