[erlang-questions] Password generator in Erlang

Zabrane Mickael zabrane3@REDACTED
Tue Aug 14 18:12:14 CEST 2012


Thank Serker.

Moving andom:seed(A1, A2, A3) at start fix it.

1> passwd:test().
Generate 10000 random password and check for collisions ...
Number of collisions: 0


--------------------
test() ->
    {A1,A2,A3} = now(),
    random:seed(A1, A2, A3),
    N = 10000,
    io:format("Generate ~p random password and check for collisions ...~n", [N]),
    test(N, dict:new(), 0).
test(0, _, C) ->
    io:format("Number of collisions: ~p~n", [C]);
test(N, Dict, C) ->
    Password = passwd(),
    case dict:find(Password, Dict) of
        {ok, _} -> %% collision detected
            test(N - 1, Dict, C + 1);  
        error ->
            test(N - 1, dict:append(Password, 1, Dict), C)
    end.

passwd() ->
    passwd(8).
passwd(Length) ->

    lists:flatten(lists:foldl(fun(_,AccIn) ->
                                      [random:uniform(90) + 32 | AccIn] end,
                              [], lists:seq(1,Length))).

--------------------

Regards,
Zabrane

On Aug 14, 2012, at 5:43 PM, Sverker Eriksson wrote:

> Try only call random:seed(A1, A2, A3) once at start.
> 
> For cryptographic safe random generation look at crypto module.
> 
> /Sverker
> 
> 
> Zabrane Mickael wrote:
>> Hi guys,
>> 
>> This one fires a lot of collisions (http://schemecookbook.org/Erlang/NumberRandomNumber):
>> 
>> ------------------
>> test() ->
>>    crypto:start(),
>>    N = 10000,
>>    io:format("Generate ~p random passwords and check for collisions ...~n", [N]),
>>    test(N, dict:new(), 0).
>> test(0, _, C) ->
>>    io:format("Number of collisions: ~p~n", [C]);
>> test(N, Dict, C) ->
>>    Password = passwd(),
>>    case dict:find(Password, Dict) of
>>        {ok, _} -> %% collision detected
>>            test(N - 1, Dict, C + 1);          error ->
>>            test(N - 1, dict:append(Password, 1, Dict), C)
>>    end.
>> 
>> passwd() ->
>>    passwd(8).
>> passwd(Length) ->
>>    {A1,A2,A3} = now(),
>>    random:seed(A1, A2, A3),
>>    lists:flatten(lists:foldl(fun(_,AccIn) ->
>>                                      [random:uniform(90) + 32 | AccIn] end,
>>                              [], lists:seq(1,Length))).
>> 
>> ----------------
>> 
>> > passwd:test().
>> Generate 10000 random password and check for collisions ...
>> Number of collisions: 1182
>> 
>> Anything better guys? Something with less collisions if possible.
>> 
>> Regards,
>> Zabrane
>> 
>>  
> 






More information about the erlang-questions mailing list