Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Calculate Hash SHA-256 for
public static String calculateHash(Block block) {
if (block != null) {
MessageDigest digest = null;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
return null;
String txt = block.str();
final byte bytes[] = digest.digest(txt.getBytes());
final StringBuilder builder = new StringBuilder();
for (final byte b : bytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
return builder.toString();
return null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.