Created
January 21, 2018 10:05
-
-
Save ssaurel/1bacfceedc535a3af241cf1fe9322242 to your computer and use it in GitHub Desktop.
Calculate Hash SHA-256 for toutsurlebitcoin.fr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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