Created
June 3, 2018 12:40
-
-
Save cyberpirate92/26d3d9905113e288ae7b5153649c190f to your computer and use it in GitHub Desktop.
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
import java.util.Base64; | |
import java.util.Scanner; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.nio.charset.StandardCharsets; | |
public class HashBreaker | |
{ | |
public static void main(String[] args) throws NoSuchAlgorithmException { | |
var scanner = new Scanner(System.in); | |
System.out.println("Nonce Finder"); | |
System.out.print("Message : "); | |
String message = scanner.nextLine(); | |
System.out.print("Difficulty : "); | |
int d = scanner.nextInt(); | |
System.out.println("\nCalculating..."); | |
long startTime = System.currentTimeMillis(); | |
long nonce = bruteforce(d, message); | |
long endTime = System.currentTimeMillis(); | |
System.out.println("\nValid Nonce : " + nonce); | |
System.out.println("Hash : " + sha256(message + String.valueOf(nonce))); | |
System.out.println("Time taken : " + (endTime - startTime)/1000f + " seconds"); | |
scanner.close(); | |
} | |
public static String sha256(String text) throws NoSuchAlgorithmException { | |
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | |
digest.update(text.getBytes(StandardCharsets.UTF_8)); | |
byte[] hash = digest.digest(); | |
StringBuffer sb = new StringBuffer(); | |
for (int i = 0; i < hash.length; i++) | |
sb.append(Integer.toString((hash[i] & 0xff) + 0x100, 16).substring(1)); | |
return sb.toString().toUpperCase(); | |
} | |
public static long bruteforce(int d, String s) throws NoSuchAlgorithmException { | |
long nonce = 0; | |
String pre = ""; | |
for (; pre.length()<d; pre += "0"); | |
while (!sha256(s + String.valueOf(nonce)).startsWith(pre)){ | |
nonce++; | |
} | |
return nonce; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment