Created
December 5, 2016 07:36
-
-
Save gingemonster/bb72bf92c3db5095d9280c8bcdeb47ca to your computer and use it in GitHub Desktop.
Adventofcode Day 5 - Challenge 1
This file contains hidden or 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
using System; | |
using System.Security.Cryptography; | |
using System.Text; | |
namespace ConsoleApplication | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var input = "abbhdwsy"; | |
var index = 0; | |
var password = ""; | |
using (MD5 md5Hash = MD5.Create()) | |
{ | |
while(true){ | |
var hash = GetMd5Hash(md5Hash, input + index); | |
if(startsWithFiveZeros(hash)){ | |
password+=hash.Substring(5,1); | |
} | |
if(password.Length==8) break; | |
index++; | |
} | |
} | |
Console.WriteLine(password); | |
} | |
private static string GetMd5Hash(MD5 md5Hash, string input) | |
{ | |
// Convert the input string to a byte array and compute the hash. | |
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); | |
// Create a new Stringbuilder to collect the bytes | |
// and create a string. | |
StringBuilder sBuilder = new StringBuilder(); | |
// Loop through each byte of the hashed data | |
// and format each one as a hexadecimal string. | |
for (int i = 0; i < data.Length; i++) | |
{ | |
sBuilder.Append(data[i].ToString("x2")); | |
} | |
// Return the hexadecimal string. | |
return sBuilder.ToString(); | |
} | |
private static bool startsWithFiveZeros(string input){ | |
return input.Substring(0,5) == "00000"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment