Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Calculate Hash SHA-256 for toutsurlebitcoin.fr
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) {
builder.append('0');
}
builder.append(hex);
}
return builder.toString();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment