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