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.
26 package org.openecomp.sparky.util;
29 * The Class EncryptConvertor.
31 public class EncryptConvertor {
33 private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
36 * toHexString(String) - convert a string into its hex equivalent.
41 public static final String toHexString(String buf) {
45 return toHexString(buf.getBytes());
49 * toHexString(byte[]) - convert a byte-string into its hex equivalent.
54 public static final String toHexString(byte[] buf) {
59 char[] chars = new char[2 * buf.length];
60 for (int i = 0; i < buf.length; ++i) {
61 chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
62 chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
64 return new String(chars);
68 * Convert a hex string to its equivalent value.
70 * @param hexString the hex string
72 * @throws Exception the exception
74 public static final String stringFromHex(String hexString) throws Exception {
75 if (hexString == null) {
78 return stringFromHex(hexString.toCharArray());
84 * @param hexCharArray the hex char array
86 * @throws Exception the exception
88 public static final String stringFromHex(char[] hexCharArray) throws Exception {
89 if (hexCharArray == null) {
92 return new String(bytesFromHex(hexCharArray));
98 * @param hexString the hex string
100 * @throws Exception the exception
102 public static final byte[] bytesFromHex(String hexString) throws Exception {
103 if (hexString == null) {
106 return bytesFromHex(hexString.toCharArray());
112 * @param hexCharArray the hex char array
114 * @throws Exception the exception
116 public static final byte[] bytesFromHex(char[] hexCharArray) throws Exception {
117 if (hexCharArray == null) {
120 int len = hexCharArray.length;
121 if ((len % 2) != 0) {
122 throw new Exception("Odd number of characters: '" + String.valueOf(hexCharArray) + "'");
124 byte[] txtInByte = new byte[len / 2];
126 for (int i = 0; i < len; i += 2) {
127 txtInByte[counter++] =
128 (byte) (((fromHexDigit(hexCharArray[i], i) << 4) | fromHexDigit(hexCharArray[i + 1], i))
138 * @param index the index
140 * @throws Exception the exception
142 protected static final int fromHexDigit(char ch, int index) throws Exception {
143 int digit = Character.digit(ch, 16);
145 throw new Exception("Illegal hex character '" + ch + "' at index " + index);