Changed to unmaintained
[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  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.tlv.sdc.security;
27
28 import java.math.BigInteger;
29 import java.security.MessageDigest;
30 import java.security.NoSuchAlgorithmException;
31 import java.security.SecureRandom;
32 import java.util.Arrays;
33 import java.util.Random;
34 import javax.xml.bind.DatatypeConverter;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38 /**
39  * A copy from the org.onap.sdc:security-utils artifact that works with java 7.
40  */
41 public class Passwords {
42
43     private static final Random RANDOM = new SecureRandom();
44     private static final int SALT = 0;
45     private static final int HASH = 1;
46     private static final String HASH_ALGORITHM = "SHA-256";
47     private static final EELFLogger log = EELFManager.getInstance().getLogger(Passwords.class);
48
49     /**
50      * static utility class
51      */
52     private Passwords() {
53     }
54
55     /**
56      * the method calculates a hash with a generated salt for the given password
57      * 
58      * @param password
59      * @return a "salt:hash" value
60      */
61     public static String hashPassword(String password) {
62         if (password != null) {
63             byte[] salt = getNextSalt();
64             byte byteData[] = hash(salt, password.getBytes());
65             return DatatypeConverter.printHexBinary(salt) + ":" + DatatypeConverter.printHexBinary(byteData);
66         }
67         return null;
68
69     }
70
71     /**
72      * the method checks if the given password matches the calculated hash
73      * 
74      * @param password
75      * @param expectedHash
76      * @return
77      */
78     public static boolean isExpectedPassword(String password, String expectedHash) {
79         String[] params = expectedHash.split(":");
80         return isExpectedPassword(password, params[SALT], params[HASH]);
81     }
82
83     /**
84      * the method checks if the given password matches the calculated hash
85      * 
86      * @param password
87      * @param salt
88      * @param hash
89      *            the hash generated using the salt
90      * @return true if the password matched the hash
91      */
92     public static boolean isExpectedPassword(String password, String salt, String hash) {
93         byte[] saltBytes = DatatypeConverter.parseHexBinary(salt);
94         byte[] hashBytes = DatatypeConverter.parseHexBinary(hash);
95         if (password != null) {
96             byte byteData[] = hash(saltBytes, password.getBytes());
97             return Arrays.equals(byteData, hashBytes);
98         }
99         return false;
100     }
101
102     public static void main(String[] args) {
103         if (args.length > 0) {
104             log.info("[" + hashPassword(args[0]) + "]");
105         } else {
106             log.info("no password passed");
107         }
108
109     }
110
111     /**
112      * Returns a random salt to be used to hash a password.
113      * 
114      * @return a 16 bytes random salt
115      */
116     private static byte[] getNextSalt() {
117         byte[] salt = new byte[16];
118         RANDOM.nextBytes(salt);
119         return salt;
120     }
121
122     /**
123      * hase's the salt and value using the chosen algorithm
124      * 
125      * @param salt
126      * @param password
127      * @return an array of bytes resulting from the hash
128      */
129     private static byte[] hash(byte[] salt, byte[] password) {
130         MessageDigest md;
131         byte[] byteData = null;
132         try {
133             md = MessageDigest.getInstance(HASH_ALGORITHM);
134             md.update(salt);
135             md.update(password);
136             byteData = md.digest();
137         } catch (NoSuchAlgorithmException e) {
138             log.info("invalid algorithm name", e);
139         }
140         return byteData;
141     }
142
143     /**
144      * Converts a string of hexadecimal characters into a byte array.
145      *
146      * @param hex
147      *            the hex string
148      * @return the hex string decoded into a byte array
149      */
150     /*private static byte[] fromHex(String hex) {
151         byte[] binary = new byte[hex.length() / 2];
152         for (int i = 0; i < binary.length; i++) {
153             binary[i] = (byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16);
154         }
155         return binary;
156     }*/
157
158     /**
159      * Converts a byte array into a hexadecimal string.
160      *
161      * @param array
162      *            the byte array to convert
163      * @return a length*2 character string encoding the byte array
164      */
165 /*    private static String toHex(byte[] array) {
166         BigInteger bi = new BigInteger(1, array);
167         String hex = bi.toString(16);
168         int paddingLength = (array.length * 2) - hex.length();
169         if (paddingLength > 0)
170             return String.format("%0" + paddingLength + "d", 0) + hex;
171         else
172             return hex;
173     }*/
174 }