-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05_part02.fs
36 lines (31 loc) · 1.45 KB
/
day05_part02.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module day05_part02
let calculateMD5Hash (input: string) =
let md5 = System.Security.Cryptography.MD5.Create()
let inputBytes = System.Text.Encoding.ASCII.GetBytes(input)
let hashBytes = md5.ComputeHash(inputBytes)
let sb = System.Text.StringBuilder()
for i = 0 to hashBytes.Length - 1 do
sb.Append(hashBytes.[i].ToString("X2")) |> ignore
sb.ToString()
let rec findPassword (input: string) (index: int) (password: (string*bool)[]) =
match password |> Array.forall(fun b -> snd b) with
| true -> password |> Array.map(fun b -> fst b) |> String.concat ""
| false ->
let hash = calculateMD5Hash (input + (index.ToString()))
if hash.StartsWith("00000") then
let position = hash.[5].ToString()
let character = hash.[6].ToString()
if position >= "0" && position <= "7" then
let position = int position
if not (snd password.[position]) then
password.[position] <- (character, true)
findPassword input (index + 1) password
else
findPassword input (index + 1) password
else
findPassword input (index + 1) password
else
findPassword input (index + 1) password
let execute =
let input = "wtnhxymk"
findPassword input 0 [|("", false); ("", false); ("", false); ("", false); ("", false); ("", false); ("", false); ("", false)|]