Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash String via SHA-256 in Java

By looking around here as well as the internet in general, I have found Bouncy Castle. I want to use Bouncy Castle (or some other freely available utility) to generate a SHA-256 Hash of a String in Java. Looking at their documentation I can't seem to find any good examples of what I want to do. Can anybody here help me out?

like image 204
knpwrs Avatar asked Jun 23 '10 16:06

knpwrs


People also ask

How do you hash a string in Java?

Java String hashCode() MethodThe hashCode() method returns the hash code of a string. where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation.

How do you create a hash in Java?

If you want a cryptographic hash of a String, the Java crypto libraries include implementations of MD5, SHA-1 and so on. You'll typically need to turn the String into a byte array, and then feed that to the hash generator / digest generator.

What is a SHA256 string?

A cryptographic hash (sometimes called 'digest') is a kind of 'signature' for a text or a data file. SHA-256 generates an almost-unique 256-bit (32-byte) signature for a text. See below for the source code. Enter any message to check its SHA-256 hash.


2 Answers

To hash a string, use the built-in MessageDigest class:

import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.nio.charset.StandardCharsets; import java.math.BigInteger;  public class CryptoHash {   public static void main(String[] args) throws NoSuchAlgorithmException {     MessageDigest md = MessageDigest.getInstance("SHA-256");     String text = "Text to hash, cryptographically.";      // Change this to UTF-16 if needed     md.update(text.getBytes(StandardCharsets.UTF_8));     byte[] digest = md.digest();      String hex = String.format("%064x", new BigInteger(1, digest));     System.out.println(hex);   } } 

In the snippet above, digest contains the hashed string and hex contains a hexadecimal ASCII string with left zero padding.

like image 63
Brendan Long Avatar answered Sep 30 '22 14:09

Brendan Long


This is already implemented in the runtime libs.

public static String calc(InputStream is) {     String output;     int read;     byte[] buffer = new byte[8192];      try {         MessageDigest digest = MessageDigest.getInstance("SHA-256");         while ((read = is.read(buffer)) > 0) {             digest.update(buffer, 0, read);         }         byte[] hash = digest.digest();         BigInteger bigInt = new BigInteger(1, hash);         output = bigInt.toString(16);         while ( output.length() < 32 ) {             output = "0"+output;         }     }      catch (Exception e) {         e.printStackTrace(System.err);         return null;     }      return output; } 

In a JEE6+ environment one could also use JAXB DataTypeConverter:

import javax.xml.bind.DatatypeConverter;  String hash = DatatypeConverter.printHexBinary(             MessageDigest.getInstance("MD5").digest("SOMESTRING".getBytes("UTF-8"))); 
like image 27
stacker Avatar answered Sep 30 '22 14:09

stacker