public
Created

make sure you've started crypto with `application:start(crypto)`

  • Download Gist
crack_util.erl
Erlang
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
-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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.