Hoàng Huy
Hoàng Huy

Reputation: 31

Hashing password by SHA256 then write to file

I try to Hash this password hoang1@H 3 times with 3 accounts by using SHA256. But when i write this password to file by using FileWriter and BufferedWriter, there are 3 different strings. Why ? Here are my code for SHA256 hashing:

    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(); 
}

Upvotes: 0

Views: 2080

Answers (2)

Joma
Joma

Reputation: 3869

Code

You can test/run this code on ▶▶▶▶▶ https://replit.com/@JomaCorpFX/JavaHashes

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));

  }
}

Output

output1

Upvotes: 1

mtz1406
mtz1406

Reputation: 75

you should call md.reset() before reuse the MessageDigest instance.Just add it before md.digest(....).

Upvotes: 1

Related Questions