Skip to content

Instantly share code, notes, and snippets.

@Gustav-Simonsson
Created January 28, 2013 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gustav-Simonsson/4653992 to your computer and use it in GitHub Desktop.
Save Gustav-Simonsson/4653992 to your computer and use it in GitHub Desktop.
-module(crack).
-export([crack/0]).
-compile(export_all).
-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)),
tail_recurse(Products, {Lol, FlatDivs, FlatMods}, MD5Hash).
tail_recurse(0, {N, Lol, FlatDivs, FlatMods}, MD5Hash) ->
Product = nthProduct(N, Lol, FlatDivs, FlatMods),
check_md5(Product, MD5Hash);
tail_recurse(N, {Lol, FlatDivs, FlatMods}, MD5Hash) ->
Product = nthProduct(N, Lol, FlatDivs, FlatMods),
check_md5(Product, MD5Hash),
tail_recurse(N - 1, {Lol, FlatDivs, FlatMods}, MD5Hash).
check_md5(Product, MD5Hash) ->
case crypto:md5(Product) == MD5Hash of
true ->
io:format("found product: ~p~n",[lists:flatten(Product)]);
_ ->
ok
end.
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