hash - 汉克雷斯特。如何检查字节数组等于
问题描述
需要在不涉及数据库的情况下为创建管理员创建应用程序密码。管理员密码在创建后将保存在数据库中。根据以下测试方法,将 byte[] 写入/从文件中读取相同的值存在问题。
这是很好的方法吗?处理二进制数据有什么问题?
import org.apache.commons.io.FileUtils;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.io.*;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
import java.util.Random;
import static org.junit.Assert.assertEquals;
public class Passwords {
static byte[] salt;
static byte[] password_hash;
private static final Random RANDOM = new SecureRandom();
private static final int ITERATIONS = 10000;
private static final int KEY_LENGTH = 256;
static String password_file ="C:\\Users\\hp\\IdeaProjects\\pp20_hotel_v1\\src\\main\\resources\\app_password_hash";
static String salt_file = "C:\\Users\\hp\\IdeaProjects\\pp20_hotel_v1\\src\\main\\resources\\salt_hash";
public static byte[] getNextSalt() {
byte[] salt = new byte[16];
RANDOM.nextBytes(salt);
return salt;
}
private static void writeBinary(String path, byte[] arr){
File file = new File(path);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
FileUtils.writeByteArrayToFile(file, arr);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private 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();
}
}
private static byte[] readBinary(String path){
File file = new File(path);
byte fileContent[] = null;
try {
fileContent = FileUtils.readFileToByteArray(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileContent;
}
@BeforeClass
public static void writeSaltAndPassword(){
String password = "12345678";
salt = getNextSalt();
password_hash = hash(password.toCharArray(), salt);
System.out.println("salt : "+salt);
System.out.println("password_hash : "+password_hash);
writeBinary(password_file, password_hash);
writeBinary(salt_file, salt);
}
@Test
public void checkPassword(){
byte[] actual_password_hash=readBinary(password_file);
assertEquals(password_hash, actual_password_hash);
}
}
解决方案
问题就在于此。Hamcrest 不会逐个检查 byte[] 元素。需要写:
for(int i=0; i<password_hash.length;i++){
if(password_hash[i]!=actual_password_hash[i]){
System.out.println(password_hash[i]!=actual_password_hash[i]);
return;
}
}
推荐阅读
- java - java -version 不想加载我的 Java_home 的新值
- reactjs - 满足条件后如何取消地图?
- python - 创建小型聊天机器人时出现 rasa_nlu 错误
- python - 是否可以从一个文件到另一个文件使用 Django 模板块?
- react-native - 我怎样才能在需要反应原生时制作一个选择器
- angular - 如何在订阅 Angular 中更好地编写订阅?
- pandas - Pandas:如何在多索引时间序列中填充缺失的期间/日期时间值?
- azure - 如何将参数从 Schedule 类型触发器传递到 ADF 管道?
- javascript - 在 JavaScript/Node.js 中列出对象数组中的重复条目
- css - 水平滚动出现在移动端