通过 SHA256 散列密码然后写入文件

Hashing password by SHA256 then write to file

我尝试使用 SHA256 对 3 个帐户散列此密码 hoang1@H 3 次。但是当我使用 FileWriterBufferedWriter 将此密码写入文件时,有 3 个不同的字符串。为什么 ? 这是我的 SHA256 哈希代码:

    public byte[] getSHA(String input) throws NoSuchAlgorithmException
{ 
    // Static getInstance method is called with hashing SHA 
    MessageDigest md = MessageDigest.getInstance("SHA-256"); 

    // digest() method called 
    // to calculate message digest of an input 
    // and return array of byte
    return md.digest(input.getBytes(StandardCharsets.UTF_8)); 
}

public String toHexString(byte[] hash)
{
    // Convert byte array into signum representation 
    BigInteger number = new BigInteger(1, hash); 

    // Convert message digest into hex value 
    StringBuilder hexString = new StringBuilder(number.toString(16)); 

    // Pad with leading zeros
    while (hexString.length() < 32) 
    { 
        hexString.insert(0, '0'); 
    } 

    return hexString.toString(); 
}

您应该在重用 MessageDigest 之前调用 md.reset() instance.Just 在 md.digest(....) 之前添加它。

代码

您可以在 ▶▶▶▶▶ https://replit.com/@JomaCorpFX/JavaHashes

上 test/run 此代码

HashAlgorithm.java

public enum HashAlgorithm {

    SHA512("SHA-512"),
    SHA256("SHA-256"),
    SHA384("SHA-384"),
    SHA1("SHA-1"),
    MD5("MD5");

    private String Value = "";

    HashAlgorithm(String Value) {
        this.Value = Value;
    }

    @Override
    public String toString() {
        return Value;
    }

}

HexEncoder.java

import java.util.Formatter;

public class HexEncoder{
  public static String toHex(byte[] data) {
        StringBuilder sb = new StringBuilder(data.length * 2);
        try (Formatter formatter = new Formatter(sb))
        {
            for (byte b : data)
            {
                formatter.format("%02x", b);
            }
        }
        return sb.toString();
    }
}

HashManager.java

import java.security.MessageDigest;
import java.nio.charset.StandardCharsets;


public class HashManager {
    public static byte[] toRawHash(byte[] data, HashAlgorithm algorithm) throws Exception
    {
        byte[] buffer = data;
        MessageDigest messageDigest = MessageDigest.getInstance(algorithm.toString());
        messageDigest.reset();
        messageDigest.update(buffer);
        return messageDigest.digest();
    }

    public static String toHexHash(byte[] data, HashAlgorithm algorithm) throws Exception
    {
       return HexEncoder.toHex(toRawHash(data, algorithm));
    }

    public static String toHexHash(String data, HashAlgorithm algorithm) throws Exception
    {
       return toHexHash(data.getBytes(StandardCharsets.UTF_8), algorithm);
    }
}

Main.java

public class Main {
  public static void main(String[] args) throws Exception {
    String data = "grape";
    System.out.println(HashManager.toHexHash(data, HashAlgorithm.SHA256));
    System.out.println(HashManager.toHexHash(data, HashAlgorithm.SHA256));
    System.out.println(HashManager.toHexHash(data, HashAlgorithm.SHA256));
    System.out.println(HashManager.toHexHash(data, HashAlgorithm.SHA256));

  }
}

输出