从.txt中检索SHA-1哈希值

时间:2016-03-22 06:07:59

标签: file hash io server sha1

我在我编写的服务器程序中实现了一个SHA-1哈希函数,它生成一个用户名密码对的.txt文件,其密码由各自的哈希值表示。

我无法从文件中检索哈希值而不会破坏它们。所有“数组复制”类型的函数和任何类型的移动到字节数组似乎都会破坏它们。 (当它们最初作为字符串读入时,它们的值是正确的。

目标是在读取有效密码并将其输入“sha1.isEqual(byte [],byte [])”函数后获得有效密码。

这是我在密码中读到的方法

public void getPasswords()
{   
    String[] splitString = null;

    BufferedReader reader = null;

    try {
        File file = new File("hash_user_pass.txt");
        reader = new BufferedReader(new FileReader(file));
        int i = 0;
        String line;
        while ((line = reader.readLine()) != null) {
            if (line.isEmpty()) continue; 

            splitString = line.split(" ");

            //System.out.println(splitString[0]);
            //System.out.println(splitString[1]);

            usernames.add(splitString[0]);
            passwords.add(splitString[1]);

            //passwords.add(splitString[1].getBytes());
            //System.out.println("Password Bytes: " + passwords.get(i));
            i++;
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

这是我将它们与用户密码进行比较的地方

    hashed_pass = hash(password);

    if(sha1.isEqual(passwords.get(i),hashed_pass))
    {
        System.out.println("Passwords match.");
    }

0 个答案:

没有答案