2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.tlv.sdc.security;
25 import java.math.BigInteger;
26 import java.security.MessageDigest;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.SecureRandom;
29 import java.util.Arrays;
30 import java.util.Random;
33 * A copy from the org.openecomp.sdc:security-utils artifact that works with java 7.
35 public class Passwords {
37 private static final Random RANDOM = new SecureRandom();
38 private static final int SALT = 0;
39 private static final int HASH = 1;
40 private static final String HASH_ALGORITHM = "SHA-256";
43 * static utility class
49 * the method calculates a hash with a generated salt for the given password
52 * @return a "salt:hash" value
54 public static String hashPassword(String password) {
55 byte[] salt = getNextSalt();
56 byte byteData[] = hash(salt, password.getBytes());
57 if (byteData != null) {
58 return toHex(salt) + ":" + toHex(byteData);
65 * the method checks if the given password matches the calculated hash
71 public static boolean isExpectedPassword(String password, String expectedHash) {
72 String[] params = expectedHash.split(":");
73 return isExpectedPassword(password, params[SALT], params[HASH]);
77 * the method checks if the given password matches the calculated hash
82 * the hash generated using the salt
83 * @return true if the password matched the hash
85 public static boolean isExpectedPassword(String password, String salt, String hash) {
86 byte[] saltBytes = fromHex(salt);
87 byte[] hashBytes = fromHex(hash);
89 byte byteData[] = hash(saltBytes, password.getBytes());
90 if (byteData != null) {
91 return Arrays.equals(byteData, hashBytes);
96 public static void main(String[] args) {
97 if (args.length > 1 || args.length > 0) {
98 System.out.println("[" + hashPassword(args[0]) + "]");
100 System.out.println("no passward passed.");
106 * Returns a random salt to be used to hash a password.
108 * @return a 16 bytes random salt
110 private static byte[] getNextSalt() {
111 byte[] salt = new byte[16];
112 RANDOM.nextBytes(salt);
117 * hase's the salt and value using the chosen algorithm
121 * @return an array of bytes resulting from the hash
123 private static byte[] hash(byte[] salt, byte[] password) {
125 byte[] byteData = null;
127 md = MessageDigest.getInstance(HASH_ALGORITHM);
130 byteData = md.digest();
131 } catch (NoSuchAlgorithmException e) {
132 System.out.println("in vlide algorithem name");
138 * Converts a string of hexadecimal characters into a byte array.
142 * @return the hex string decoded into a byte array
144 private static byte[] fromHex(String hex) {
145 byte[] binary = new byte[hex.length() / 2];
146 for (int i = 0; i < binary.length; i++) {
147 binary[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
153 * Converts a byte array into a hexadecimal string.
156 * the byte array to convert
157 * @return a length*2 character string encoding the byte array
159 private static String toHex(byte[] array) {
160 BigInteger bi = new BigInteger(1, array);
161 String hex = bi.toString(16);
162 int paddingLength = (array.length * 2) - hex.length();
163 if (paddingLength > 0)
164 return String.format("%0" + paddingLength + "d", 0) + hex;