-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathSecurePassword.java
More file actions
150 lines (136 loc) · 5.3 KB
/
SecurePassword.java
File metadata and controls
150 lines (136 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* AMRIT – Accessible Medical Records via Integrated Technology
* Integrated EHR (Electronic Health Records) Solution
*
* Copyright (C) "Piramal Swasthya Management and Research Institute"
*
* This file is part of AMRIT.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
package com.iemr.common.config.encryption;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import org.springframework.stereotype.Service;
@Service
public class SecurePassword {
public String generateStrongPassword(String password) throws NoSuchAlgorithmException, InvalidKeySpecException {
int iterations = 1001;
char[] chars = password.toCharArray();
byte[] salt = getSalt();
PBEKeySpec spec = new PBEKeySpec(chars, salt, iterations, 512);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
byte[] hash = skf.generateSecret(spec).getEncoded();
return iterations + ":" + toHex(salt) + ":" + toHex(hash);
}
private byte[] getSalt() throws NoSuchAlgorithmException {
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
sr.nextBytes(salt);
return salt;
}
private String toHex(byte[] array) {
BigInteger bi = new BigInteger(1, array);
String hex = bi.toString(16);
int paddingLength = array.length * 2 - hex.length();
if (paddingLength > 0) {
return String.format(new StringBuilder().append("%0").append(paddingLength).append("d").toString(),
new Object[] { Integer.valueOf(0) }) + hex;
}
return hex;
}
public int validatePassword(String originalPassword, String storedPassword)
throws NoSuchAlgorithmException, InvalidKeySpecException {
int validCount = 0;
String[] parts = storedPassword.split(":");
int iterations = Integer.parseInt(parts[0]);
byte[] salt = fromHex(parts[1]);
byte[] hash = fromHex(parts[2]);
if (iterations == 1000) {
PBEKeySpec spec = new PBEKeySpec(originalPassword.toCharArray(), salt, 1000, hash.length * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] testHash = skf.generateSecret(spec).getEncoded();
int diff = hash.length ^ testHash.length;
for (int i = 0; (i < hash.length) && (i < testHash.length); i++) {
diff |= hash[i] ^ testHash[i];
}
if (diff == 0) {
// return 1 if using SHA1 algorithm to execute save and login Operation
validCount = 1;
return validCount;
} else {
PBEKeySpec spec1 = new PBEKeySpec(originalPassword.toCharArray(), salt, iterations, hash.length * 8);
SecretKeyFactory skf1 = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
byte[] testHash1 = skf1.generateSecret(spec1).getEncoded();
int diff1 = hash.length ^ testHash1.length;
for (int i = 0; (i < hash.length) && (i < testHash1.length); i++) {
diff1 |= hash[i] ^ testHash1[i];
}
if (diff1 == 0) {
// return 2 if using SHA512 algorithm to execute login Operation
validCount = 2;
return validCount;
} else {
// return 0 if wrong password
validCount = 0;
return validCount;
}
}
}
if (iterations == 1001) {
PBEKeySpec spec = new PBEKeySpec(originalPassword.toCharArray(), salt, iterations, hash.length * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
byte[] testHash = skf.generateSecret(spec).getEncoded();
int diff = hash.length ^ testHash.length;
for (int i = 0; (i < hash.length) && (i < testHash.length); i++) {
diff |= hash[i] ^ testHash[i];
}
if (diff == 0) {
// return 3 if using SHA512 algorithm to execute login Operation
validCount = 3;
return validCount;
} else {
validCount = 0;
return validCount;
}
}
return validCount;
}
public boolean validatePasswordExisting(String originalPassword, String storedPassword)
throws NoSuchAlgorithmException, InvalidKeySpecException {
String[] parts = storedPassword.split(":");
int iterations = Integer.parseInt(parts[0]);
byte[] salt = fromHex(parts[1]);
byte[] hash = fromHex(parts[2]);
PBEKeySpec spec = new PBEKeySpec(originalPassword.toCharArray(), salt, iterations, hash.length * 8);
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
byte[] testHash = skf.generateSecret(spec).getEncoded();
int diff = hash.length ^ testHash.length;
for (int i = 0; (i < hash.length) && (i < testHash.length); i++) {
diff |= hash[i] ^ testHash[i];
}
return diff == 0;
}
private byte[] fromHex(String hex) {
byte[] bytes = new byte[hex.length() / 2];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = ((byte) Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16));
}
return bytes;
}
}