获取相同密码的不同哈希值(+盐)
Getting different hash values of the same password (+ salt)
我试图按照 here. 中的代码获取散列密码,目前我只使用 salt
方法的代码,hash
方法和 isExpectedPassword
方法。
我从文本字段中获取密码:
char[] passCharArray = txtPassword.toString().toCharArray();
然后我调用 class 来获取盐值(我将其称为 Encryptor
而不是 Passwords
,就像在原始 post 中那样):
byte[] salt = Encryptor.getNextSalt();
然后我得到哈希密码:
byte[] hashedPass = Encryptor.hash(passCharArray, salt);
使用以下代码打印结果以查看发生了什么并对结果进行评论:
String saltString = Arrays.toString(salt);
System.out.println("SALT: " + saltString);
//SALT: [18, 117, -98, 41, 92, 124, 118, 17, 107, 14, 0, -81, 110, 70, 10, 42]
String hashedPassString = Arrays.toString(hashedPass);
System.out.println("HASHED PASS: " + hashedPassString);
//HASHED PASS: [44, -127, -43, 84, 40, -16, -46, -71, 109, -44, -41, 47, -61, -119, 21, 99, -23, 101, -13, 116, -12, 118, -66, 44, 104, 5, 4, 18, -55, 47, 59, 116]
System.out.println("Passwords match: " + Encryptor.isExpectedPassword(passCharArray, salt, hashedPass));
//Passwords match: false
以下两个是我在 isExpectedPassword
方法中放入的 System.out.print
s,以查看调用时的值。
//Encryptor pwdHash: [-103, -87, 53, -75, 59, 11, 77, 116, 123, 59, 68, -35, 16, -68, 42, 34, -32, 75, 22, -94, -37, -26, 16, 20, 7, -46, -6, -20, -88, 104, -121, 77]
//Encryptor expectedHash: [44, -127, -43, 84, 40, -16, -46, -71, 109, -44, -41, 47, -61, -119, 21, 99, -23, 101, -13, 116, -12, 118, -66, 44, 104, 5, 4, 18, -55, 47, 59, 116]
所以基本上,hashedPass
(和 expectedHash
)应该与 pwdHash
相同,但事实并非如此。我不明白我做错了什么。我的代码中遗漏了什么吗?有什么东西在我不知情的情况下发生了变化吗?
这是我的完整代码,如果人们想要查看整个代码以防万一:
public class Encryptor {
private static final Random RANDOM = new SecureRandom();
private static final int ITERATIONS = 10000;
private static final int KEY_LENGTH = 256;
private Encryptor(){}
public static byte[] getNextSalt(){
byte[] salt = new byte[16];
RANDOM.nextBytes(salt);
return salt;
}
public static byte[] hash(char[] password, byte[] salt) {
PBEKeySpec spec = new PBEKeySpec(password, salt, ITERATIONS, KEY_LENGTH);
Arrays.fill(password, Character.MIN_VALUE);
try {
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
return skf.generateSecret(spec).getEncoded();
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new AssertionError("Error while hashing a password: " + e.getMessage(), e);
} finally {
spec.clearPassword();
}
}
public static boolean isExpectedPassword(char[] password, byte[] salt, byte[] expectedHash) {
byte[] pwdHash = hash(password, salt);
String s = Arrays.toString(pwdHash);
System.out.println("Encryptor pwdHash: " + s);
String s2 = Arrays.toString(expectedHash);
System.out.println("Encryptor expectedHash: " + s2);
Arrays.fill(password, Character.MIN_VALUE);
if (pwdHash.length != expectedHash.length) return false;
for (int i = 0; i < pwdHash.length; i++) {
if (pwdHash[i] != expectedHash[i]) return false;
}
return true;
}
}
public class Controller implements Initializable {
@FXML
private Button btnLogin;
//Some private variables
@FXML
private AnchorPane ancPane;
@FXML
private ImageView imgLogo;
@FXML
private Hyperlink hplRegister;
@FXML
private TextField txtUsername;
@FXML
private TextField txtPassword;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
//Some styling
hplRegister.setOnAction(event -> {
//Registering event
});
btnLogin.setOnAction(event -> {
try {
//Loading fxml data
// I've put the code here just for testing purposes
// and will not be the final placement.
char[] passCharArray = txtPassword.toString().toCharArray();
byte[] salt = Encryptor.getNextSalt();
byte[] hashedPass = Encryptor.hash(passCharArray, salt);
String saltString = Arrays.toString(salt);
System.out.println("SALT: " + saltString);
String hashedPassString = Arrays.toString(hashedPass);
System.out.println("HASHED PASS: " + hashedPassString);
System.out.println("Passwords match: " + Encryptor.isExpectedPassword(passCharArray, salt, hashedPass));
}catch (Exception e){
e.printStackTrace();
}
});
}
//Some getter methods.
}
问题是您正在清除密码。当您对密码进行哈希处理时,您会清除 char 数组并用空白填充它,这会清除 passCharArray
。当您第二次传递它时,它基本上是在检查密码的哈希值与空白数组的哈希值。那些绝对不匹配。
在真实情况下,您将从数据库或其他来源获得加盐和散列密码。在传递给 isExpectedPassword
之前,传入的版本不会被散列,因此会被清除
我试图按照 here. 中的代码获取散列密码,目前我只使用 salt
方法的代码,hash
方法和 isExpectedPassword
方法。
我从文本字段中获取密码:
char[] passCharArray = txtPassword.toString().toCharArray();
然后我调用 class 来获取盐值(我将其称为 Encryptor
而不是 Passwords
,就像在原始 post 中那样):
byte[] salt = Encryptor.getNextSalt();
然后我得到哈希密码:
byte[] hashedPass = Encryptor.hash(passCharArray, salt);
使用以下代码打印结果以查看发生了什么并对结果进行评论:
String saltString = Arrays.toString(salt);
System.out.println("SALT: " + saltString);
//SALT: [18, 117, -98, 41, 92, 124, 118, 17, 107, 14, 0, -81, 110, 70, 10, 42]
String hashedPassString = Arrays.toString(hashedPass);
System.out.println("HASHED PASS: " + hashedPassString);
//HASHED PASS: [44, -127, -43, 84, 40, -16, -46, -71, 109, -44, -41, 47, -61, -119, 21, 99, -23, 101, -13, 116, -12, 118, -66, 44, 104, 5, 4, 18, -55, 47, 59, 116]
System.out.println("Passwords match: " + Encryptor.isExpectedPassword(passCharArray, salt, hashedPass));
//Passwords match: false
以下两个是我在 isExpectedPassword
方法中放入的 System.out.print
s,以查看调用时的值。
//Encryptor pwdHash: [-103, -87, 53, -75, 59, 11, 77, 116, 123, 59, 68, -35, 16, -68, 42, 34, -32, 75, 22, -94, -37, -26, 16, 20, 7, -46, -6, -20, -88, 104, -121, 77]
//Encryptor expectedHash: [44, -127, -43, 84, 40, -16, -46, -71, 109, -44, -41, 47, -61, -119, 21, 99, -23, 101, -13, 116, -12, 118, -66, 44, 104, 5, 4, 18, -55, 47, 59, 116]
所以基本上,hashedPass
(和 expectedHash
)应该与 pwdHash
相同,但事实并非如此。我不明白我做错了什么。我的代码中遗漏了什么吗?有什么东西在我不知情的情况下发生了变化吗?
这是我的完整代码,如果人们想要查看整个代码以防万一:
public class Encryptor {
private static final Random RANDOM = new SecureRandom();
private static final int ITERATIONS = 10000;
private static final int KEY_LENGTH = 256;
private Encryptor(){}
public static byte[] getNextSalt(){
byte[] salt = new byte[16];
RANDOM.nextBytes(salt);
return salt;
}
public static byte[] hash(char[] password, byte[] salt) {
PBEKeySpec spec = new PBEKeySpec(password, salt, ITERATIONS, KEY_LENGTH);
Arrays.fill(password, Character.MIN_VALUE);
try {
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
return skf.generateSecret(spec).getEncoded();
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new AssertionError("Error while hashing a password: " + e.getMessage(), e);
} finally {
spec.clearPassword();
}
}
public static boolean isExpectedPassword(char[] password, byte[] salt, byte[] expectedHash) {
byte[] pwdHash = hash(password, salt);
String s = Arrays.toString(pwdHash);
System.out.println("Encryptor pwdHash: " + s);
String s2 = Arrays.toString(expectedHash);
System.out.println("Encryptor expectedHash: " + s2);
Arrays.fill(password, Character.MIN_VALUE);
if (pwdHash.length != expectedHash.length) return false;
for (int i = 0; i < pwdHash.length; i++) {
if (pwdHash[i] != expectedHash[i]) return false;
}
return true;
}
}
public class Controller implements Initializable {
@FXML
private Button btnLogin;
//Some private variables
@FXML
private AnchorPane ancPane;
@FXML
private ImageView imgLogo;
@FXML
private Hyperlink hplRegister;
@FXML
private TextField txtUsername;
@FXML
private TextField txtPassword;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
//Some styling
hplRegister.setOnAction(event -> {
//Registering event
});
btnLogin.setOnAction(event -> {
try {
//Loading fxml data
// I've put the code here just for testing purposes
// and will not be the final placement.
char[] passCharArray = txtPassword.toString().toCharArray();
byte[] salt = Encryptor.getNextSalt();
byte[] hashedPass = Encryptor.hash(passCharArray, salt);
String saltString = Arrays.toString(salt);
System.out.println("SALT: " + saltString);
String hashedPassString = Arrays.toString(hashedPass);
System.out.println("HASHED PASS: " + hashedPassString);
System.out.println("Passwords match: " + Encryptor.isExpectedPassword(passCharArray, salt, hashedPass));
}catch (Exception e){
e.printStackTrace();
}
});
}
//Some getter methods.
}
问题是您正在清除密码。当您对密码进行哈希处理时,您会清除 char 数组并用空白填充它,这会清除 passCharArray
。当您第二次传递它时,它基本上是在检查密码的哈希值与空白数组的哈希值。那些绝对不匹配。
在真实情况下,您将从数据库或其他来源获得加盐和散列密码。在传递给 isExpectedPassword