I'm converting some C# to Java. The C# is:
// Return a SHA256 hash of a string, formatted in hex
private static string HashPassword(string password)
{
SHA256Managed hash = new SHA256Managed();
byte[] utf8 = UTF8Encoding.UTF8.GetBytes(password);
return BytesToHex(hash.ComputeHash(utf8));
}
In Java I replaced SHA256Managed with MessageDigest:
private static String HashPassword(String password)
{
MessageDigest hash = MessageDigest.getInstance("SHA-256");
byte[] utf8 = hash.digest(password.getBytes(StandardCharsets.UTF_8));
return BytesToHex(hash.ComputeHash(utf8)); // ComputeHash?
}
but MessageDigest does not have ComputeHash() nor do I see its equivalent.
Is MessageDigest the correct class to use here? If so, what do I do for ComputeHash(). If not what class do I use?
Note that BytesToHex Converts a byte array to a hex string.
MessageDigest? The method to finalize the hash isdigest()for a hash calculated in several segments, ordigest(byte[])for a single-segment calculation (or as the final segment of a multi-segment hash).