Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to encode a hex SHA256 in Java
import java.security.*;
public class Sha {
public static String hash256(String data) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(data.getBytes());
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();
}
}
@Amboss
Copy link

Amboss commented May 11, 2013

thank you!

@vigneshncc
Copy link

vigneshncc commented Apr 28, 2014

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

@gordonpro
Copy link

gordonpro commented Jul 9, 2014

yes! good gist. I had validated just now.

@chan1004
Copy link

chan1004 commented Feb 4, 2015

hash value -> string(?)-> base64

How do base64 conversion?

@jm2dev
Copy link

jm2dev commented Jun 2, 2015

For base64: https://www.owasp.org/index.php/Hashing_Java

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