The Java MessageDigest class is used for computing message digests, which are fixed-size hash values generated from input data of arbitrary size. However, there are specific scenarios where its use is preferred and others where it should be avoided.
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class HashExample {
public static void main(String[] args) {
String input = "password";
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(input.getBytes());
System.out.println(bytesToHex(hash));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private static String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?