Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to encode a hex SHA256 in Java
public class Sha {
public static String hash256(String data) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
return bytesToHex(md.digest());
public static String bytesToHex(byte[] bytes) {
StringBuffer result = new StringBuffer();
for (byte byt : bytes) result.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1));
return result.toString();
Copy link

Amboss commented May 11, 2013

thank you!

Copy link

vigneshncc commented Apr 28, 2014

Is there a possibility of reversing it using SHA ?? If yes can you share some code snippets . !

Copy link

gordonpro commented Jul 9, 2014

yes! good gist. I had validated just now.

Copy link

chan1004 commented Feb 4, 2015

hash value -> string(?)-> base64

How do base64 conversion?

Copy link

jm2dev commented Jun 2, 2015

For base64:

BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(data);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment