Updating licenses in all files
[appc.git] / appc-asdc-listener / appc-asdc-listener-bundle / src / main / java / org / openecomp / tlv / sdc / security / Passwords.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
21  */
22
23 package org.openecomp.tlv.sdc.security;
24
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;
31
32 /**
33  * A copy from the org.openecomp.sdc:security-utils artifact that works with java 7.
34  */
35 public class Passwords {
36
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";
41
42     /**
43      * static utility class
44      */
45     private Passwords() {
46     }
47
48     /**
49      * the method calculates a hash with a generated salt for the given password
50      * 
51      * @param password
52      * @return a "salt:hash" value
53      */
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);
59         }
60         return null;
61
62     }
63
64     /**
65      * the method checks if the given password matches the calculated hash
66      * 
67      * @param password
68      * @param expectedHash
69      * @return
70      */
71     public static boolean isExpectedPassword(String password, String expectedHash) {
72         String[] params = expectedHash.split(":");
73         return isExpectedPassword(password, params[SALT], params[HASH]);
74     }
75
76     /**
77      * the method checks if the given password matches the calculated hash
78      * 
79      * @param password
80      * @param salt
81      * @param hash
82      *            the hash generated using the salt
83      * @return true if the password matched the hash
84      */
85     public static boolean isExpectedPassword(String password, String salt, String hash) {
86         byte[] saltBytes = fromHex(salt);
87         byte[] hashBytes = fromHex(hash);
88
89         byte byteData[] = hash(saltBytes, password.getBytes());
90         if (byteData != null) {
91             return Arrays.equals(byteData, hashBytes);
92         }
93         return false;
94     }
95
96     public static void main(String[] args) {
97         if (args.length > 1 || args.length > 0) {
98             System.out.println("[" + hashPassword(args[0]) + "]");
99         } else {
100             System.out.println("no passward passed.");
101         }
102
103     }
104
105     /**
106      * Returns a random salt to be used to hash a password.
107      * 
108      * @return a 16 bytes random salt
109      */
110     private static byte[] getNextSalt() {
111         byte[] salt = new byte[16];
112         RANDOM.nextBytes(salt);
113         return salt;
114     }
115
116     /**
117      * hase's the salt and value using the chosen algorithm
118      * 
119      * @param salt
120      * @param password
121      * @return an array of bytes resulting from the hash
122      */
123     private static byte[] hash(byte[] salt, byte[] password) {
124         MessageDigest md;
125         byte[] byteData = null;
126         try {
127             md = MessageDigest.getInstance(HASH_ALGORITHM);
128             md.update(salt);
129             md.update(password);
130             byteData = md.digest();
131         } catch (NoSuchAlgorithmException e) {
132             System.out.println("in vlide algorithem name");
133         }
134         return byteData;
135     }
136
137     /**
138      * Converts a string of hexadecimal characters into a byte array.
139      *
140      * @param hex
141      *            the hex string
142      * @return the hex string decoded into a byte array
143      */
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);
148         }
149         return binary;
150     }
151
152     /**
153      * Converts a byte array into a hexadecimal string.
154      *
155      * @param array
156      *            the byte array to convert
157      * @return a length*2 character string encoding the byte array
158      */
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;
165         else
166             return hex;
167     }
168 }