Skip to content

@wardbekker /crack_util.erl
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
make sure you've started crypto with `application:start(crypto)`
-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
Something went wrong with that request. Please try again.