Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,795 answers

573 users

How to convert a string to a sha256 (one way hash) hex messagedigest in Java

1 Answer

0 votes
import java.security.MessageDigest;

public class MyClass {
    public static void main(String args[]) throws Exception {
        String s = "java programming";
	  
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(s.getBytes());
      
        byte[] barray = md.digest(); 
        for (byte b : barray) {
            System.out.print(b + " ");
        }
      
        // Convert byte array to Hex  
        StringBuffer hex = new StringBuffer();
        for (int i = 0;i < barray.length; i++) {
            hex.append(Integer.toHexString(0xFF & barray[i]));
        }
        
        System.out.println("\n" + hex.toString());     
    }
}
 
 
  
  
/*
run:
  
-25 92 11 48 38 -93 -42 91 -28 101 -98 92 35 111 -33 89 7 71 -19 34 38 -7 -75 30 -1 64 22 55 -103 5 -87 7 
e75cb3026a3d65be4659e5c236fdf59747ed2226f9b51eff401637995a97
  
*/

 



answered Mar 30, 2021 by avibootz

Related questions

1 answer 60 views
1 answer 188 views
188 views asked Aug 8, 2020 by avibootz
1 answer 123 views
1 answer 159 views
1 answer 131 views
1 answer 197 views
...