Skip to content

Instantly share code, notes, and snippets.

@jaketarnow
Created May 31, 2016 21:38
Show Gist options
  • Save jaketarnow/d4ec20cda8f633dc5147ef08ac90c0b4 to your computer and use it in GitHub Desktop.
Save jaketarnow/d4ec20cda8f633dc5147ef08ac90c0b4 to your computer and use it in GitHub Desktop.
Given a variable integer X (e.g. 42) and a string word (e.g. "hello"), where word is iteratively run through a SHA-256 hex digest computation X number of times, what is the resulting hex digest?
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class CryptoOne {
public static String getHash(String word, int x) throws NoSuchAlgorithmException {
String oldWord = word;
String newWord = null;
MessageDigest digest = MessageDigest.getInstance("SHA-256");
for (int i = 0; i < x; i++) {
if (newWord != null) {
digest.update(newWord.getBytes());
} else {
digest.update(oldWord.getBytes());
}
newWord = getHex(digest.digest());
}
System.out.println("Answer: " + newWord + "\n");
return newWord;
}
public static String getHex(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (byte b : bytes) {
sb.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println("INIT\n");
try {
getHash("hello", 1);
getHash("bitcoin", 2);
} catch (NoSuchAlgorithmException ne) {
ne.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment