Skip to content

Instantly share code, notes, and snippets.

@avilches
Created December 21, 2010 16:25
Show Gist options
  • Save avilches/750151 to your computer and use it in GitHub Desktop.
Save avilches/750151 to your computer and use it in GitHub Desktop.
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();
}
}
@s-evsyukov
Copy link

thank you!

@vigneshncc
Copy link

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

@gordonpro
Copy link

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