2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 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.
21 * ============LICENSE_END=========================================================
24 package org.onap.tlv.sdc.security;
26 import java.math.BigInteger;
27 import java.security.MessageDigest;
28 import java.security.NoSuchAlgorithmException;
29 import java.security.SecureRandom;
30 import java.util.Arrays;
31 import java.util.Random;
34 * A copy from the org.onap.sdc:security-utils artifact that works with java 7.
36 public class Passwords {
38 private static final Random RANDOM = new SecureRandom();
39 private static final int SALT = 0;
40 private static final int HASH = 1;
41 private static final String HASH_ALGORITHM = "SHA-256";
44 * static utility class
50 * the method calculates a hash with a generated salt for the given password
53 * @return a "salt:hash" value
55 public static String hashPassword(String password) {
56 byte[] salt = getNextSalt();
57 byte byteData[] = hash(salt, password.getBytes());
58 if (byteData != null) {
59 return toHex(salt) + ":" + toHex(byteData);
66 * the method checks if the given password matches the calculated hash
72 public static boolean isExpectedPassword(String password, String expectedHash) {
73 String[] params = expectedHash.split(":");
74 return isExpectedPassword(password, params[SALT], params[HASH]);
78 * the method checks if the given password matches the calculated hash
83 * the hash generated using the salt
84 * @return true if the password matched the hash
86 public static boolean isExpectedPassword(String password, String salt, String hash) {
87 byte[] saltBytes = fromHex(salt);
88 byte[] hashBytes = fromHex(hash);
90 byte byteData[] = hash(saltBytes, password.getBytes());
91 if (byteData != null) {
92 return Arrays.equals(byteData, hashBytes);
97 public static void main(String[] args) {
98 if (args.length > 1 || args.length > 0) {
99 System.out.println("[" + hashPassword(args[0]) + "]");
101 System.out.println("no passward passed.");
107 * Returns a random salt to be used to hash a password.
109 * @return a 16 bytes random salt
111 private static byte[] getNextSalt() {
112 byte[] salt = new byte[16];
113 RANDOM.nextBytes(salt);
118 * hase's the salt and value using the chosen algorithm
122 * @return an array of bytes resulting from the hash
124 private static byte[] hash(byte[] salt, byte[] password) {
126 byte[] byteData = null;
128 md = MessageDigest.getInstance(HASH_ALGORITHM);
131 byteData = md.digest();
132 } catch (NoSuchAlgorithmException e) {
133 System.out.println("in vlide algorithem name");
139 * Converts a string of hexadecimal characters into a byte array.
143 * @return the hex string decoded into a byte array
145 private static byte[] fromHex(String hex) {
146 byte[] binary = new byte[hex.length() / 2];
147 for (int i = 0; i < binary.length; i++) {
148 binary[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
154 * Converts a byte array into a hexadecimal string.
157 * the byte array to convert
158 * @return a length*2 character string encoding the byte array
160 private static String toHex(byte[] array) {
161 BigInteger bi = new BigInteger(1, array);
162 String hex = bi.toString(16);
163 int paddingLength = (array.length * 2) - hex.length();
164 if (paddingLength > 0)
165 return String.format("%0" + paddingLength + "d", 0) + hex;