Advertisement
Guest User

day4-advent-of-coding

a guest
Dec 4th, 2015
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. package advent.of.code;
  2.  
  3. import java.math.BigInteger;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6.  
  7. public class Day4 {
  8.     public static String getMD5(String input) {
  9.         try {
  10.             MessageDigest md = MessageDigest.getInstance("MD5");
  11.             byte[] messageDigest = md.digest(input.getBytes());
  12.             BigInteger number = new BigInteger(1, messageDigest);
  13.             String hashtext = number.toString(16);
  14.             while (hashtext.length() < 32) {
  15.                 hashtext = "0" + hashtext;
  16.             }
  17.             return hashtext;
  18.         }
  19.         catch (NoSuchAlgorithmException e) {
  20.             throw new RuntimeException(e);
  21.         }
  22.     }
  23.  
  24.     public static void main(String[] args) throws NoSuchAlgorithmException {
  25.         String text = "yzbqklnj";
  26.         boolean found = false;
  27.        
  28.         int counter = 1;
  29.         while(found != true) {
  30.             String input = text + String.valueOf(counter);
  31.             String md5 = getMD5(input);
  32.             if(md5.substring(0, 5).equals("00000")) {       // part1
  33.             //if(md5.substring(0, 6).equals("000000")) {        // part2
  34.                 found = true;
  35.                 System.out.println(input);
  36.                 System.out.println(md5);
  37.             }
  38.             counter++;
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement