Skip to content

Commit

Permalink
Adding a uuid v4 implementation with crypto:rand_bytes/1
Browse files Browse the repository at this point in the history
The default version uses random:uniform/1, which is based
on a broken implementation of the whitchman-hill PRNG. It
has bad divergence, which makes the UUID somewhat easy to guess.
On the other hand, crypto module's PRNG is crypto-safe and
shouldn't be as easy to guess, making a potential safe use
of the UUID for sessions or other values.
  • Loading branch information
ferd committed Apr 8, 2011
1 parent 3f8b8d7 commit 6426e2c
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/uuid.erl
Expand Up @@ -65,6 +65,7 @@
get_v1_time/1,
get_v3/1,
get_v4/0,
get_v4_safe/0,
get_v5/1,
uuid_to_string/1,
increment/1]).
Expand Down Expand Up @@ -167,6 +168,12 @@ get_v4() ->
0:1, 1:1, % reserved bits
Rand3Part2:24, Rand4:32>>.

get_v4_safe() ->
% Version = <<4:4/big>>, bits 12-15 of time_hi_and_v (60-63)
% Variant = <<2:2/big>>, 6-7 of clock_seq_hi (70-71)
<<Start:60, _Version:4, Mid:6, _Variant:2, End:56>> = crypto:rand_bytes(16),
<<Start:60, 4:4, Mid:6, 2:2, End:56>>.

get_v5([I | _] = Name)
when is_integer(I) ->
<<B1:60, B2:6, B3a:56, B3b:38>> = crypto:sha(Name),
Expand Down

0 comments on commit 6426e2c

Please sign in to comment.