How to hash a string with SHA-256 in Java

1 Answer

0 votes
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
  
public class MyClass {
    public static void main(String args[]) throws Exception {
        String str = "Java is a high-level programming language";

        MessageDigest md = MessageDigest.getInstance("SHA-256");
    
        byte[] hash = md.digest(str.getBytes());
    
        StringBuilder hexSB = new StringBuilder();
        
        for (byte b : hash) {
            hexSB.append(String.format("%02x", b));
        }
    
        System.out.println(hexSB);
    }
}
       
       
       
       
/*
run:
       
adf8e9246301310d6ea290dd8a39a8e94e78d05c10d31847e880aa5bcfb9f092
       
*/

 



answered Oct 7, 2023 by avibootz

Related questions

...