Created
January 26, 2013 14:48
-
-
Save wardbekker/4642789 to your computer and use it in GitHub Desktop.
make sure you've started crypto with `application:start(crypto)`
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
-module(crack_util). | |
-export([crack/0]). | |
-define(CHAR_LIST, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW"). | |
crack() -> | |
TryLength = 5, | |
MD5Hash = crypto:md5("12345"), | |
Lol = lists:duplicate(TryLength, ?CHAR_LIST), | |
{Divs, Mods, _Factor} = lists:foldl( | |
fun(I, {Dividends, Moduli, LastFactor}) -> | |
Length = length(lists:nth(I+1, Lol)), | |
{[Dividends,LastFactor], [Moduli,Length], LastFactor * Length} | |
end, | |
{[],[], 1}, | |
lists:seq(length(Lol)-1,0,-1) | |
), | |
FlatDivs = lists:flatten(Divs), | |
FlatMods = lists:flatten(Mods), | |
Products = round(math:pow(length(?CHAR_LIST), TryLength)), | |
lists:foreach(fun(N) -> | |
Product = nthProduct(N, Lol, FlatDivs, FlatMods), | |
case crypto:md5(Product) == MD5Hash of | |
true -> | |
io:format("found product: ~p~n",[lists:flatten(Product)]); | |
_ -> | |
ok | |
end | |
end, | |
lists:seq(0, Products-1)). | |
nthProduct(N, Lol, Dividends, Moduli) -> | |
{Product, _, _, _, _} = lists:foldl( | |
fun fold_product/2, | |
{ [], Dividends, Moduli, Lol, N }, | |
lists:seq(1, length(Lol)) | |
), | |
Product. | |
fold_product(_I, { Acc, [ Div | NewDivs ], [Mod | NewMods], [ List | NewLol ], N}) -> | |
Position = floor(round(N/Div) rem Mod), | |
Char = lists:nth(Position+1, List), | |
{ [Acc, Char], NewDivs, NewMods, NewLol, N }. | |
floor(X) -> | |
T = erlang:trunc(X), | |
case (X - T) of | |
Neg when Neg < 0 -> T - 1; | |
Pos when Pos > 0 -> T; | |
_ -> T | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment