Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created January 21, 2018 10:05
Show Gist options
  • Save ssaurel/1bacfceedc535a3af241cf1fe9322242 to your computer and use it in GitHub Desktop.
Save ssaurel/1bacfceedc535a3af241cf1fe9322242 to your computer and use it in GitHub Desktop.
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