Module uuid

Erlang UUID Generation

http://www.ietf.org/rfc/rfc4122.txt is the reference for official UUIDs.

Copyright © 2011-2014 Michael Truog

Version: 1.3.2 Sep 30 2014 14:04:49 ------------------------------------------------------------------------

Authors: Michael Truog (mjtruog [at] gmail (dot) com).

Description

Erlang UUID Generation

http://www.ietf.org/rfc/rfc4122.txt is the reference for official UUIDs. This implementation provides a version 1 UUID that includes both the Erlang pid identifier (ID, Serial, Creation) and the distributed Erlang node name within the 48 bit node ID. To make room for the Erlang pid identifier, the 48 bits from the MAC address (i.e., 3 OCI (Organizationally Unique Identifier) bytes and 3 NIC (Network Interface Controller) specific bytes) and the distributed Erlang node name are bitwise-XORed down to 16 bits. The Erlang pid is bitwise-XORed from 72 bits down to 32 bits. The version 3 (MD5), version 4 (random), and version 5 (SHA) methods are provided as specified within the RFC.

Data Types

state()

state() = #uuid_state{}

uuid()

uuid() = <<_:128>>

Function Index

get_v1/1

Get a v1 UUID.

.
get_v1_time/0

Get the current time value in a manner consistent with the v1 UUID.

The result is an integer in microseconds.
get_v1_time/1

Get the current time value in a manner consistent with the v1 UUID.

The result is an integer in microseconds.
get_v3/1

Get a v3 UUID.

.
get_v3/2

Get a v3 UUID in a particular namespace.

.
get_v3_compat/1

Get a compatible v3 UUID.

Do not use all bits from the checksum so that the UUID matches external implementations.
get_v3_compat/2

Get a compatible v3 UUID in a particular namespace.

Do not use all bits from the checksum so that the UUID matches external implementations.
get_v4/0

Get a v4 UUID (using crypto/openssl).

crypto:strong_rand_bytes/1 repeats in the same way as RAND_bytes within OpenSSL.
get_v4/1
get_v4_urandom/0

Get a v4 UUID (using Wichmann-Hill 2006).

random_wh06_int:uniform/1 repeats every 2.66e36 (2^121) approx.
get_v4_urandom_bigint/0

Get a v4 UUID (using Wichmann-Hill 1982).

random:uniform/1 repeats every 2.78e13 (see B.A. Wichmann and I.D.Hill, in 'An efficient and portable pseudo-random number generator', Journal of Applied Statistics. AS183. 1982, or Byte March 1987) a single random:uniform/1 call can provide a maximum of 45 bits (currently this is not significantly faster because multiple function calls are necessary)

.
get_v4_urandom_native/0

Get a v4 UUID (using Wichmann-Hill 1982).

Attempt to only use native integers (Erlang limits integers to 27 bits before using bigints) to investigate the speed when using HiPE.
get_v5/1

Get a v5 UUID.

.
get_v5/2

Get a v5 UUID in a particular namespace.

.
get_v5_compat/1

Get a compatible v5 UUID.

Do not use all bits from the checksum so that the UUID matches external implementations.
get_v5_compat/2

Get a compatible v5 UUID in a particular namespace.

Do not use all bits from the checksum so that the UUID matches external implementations.
increment/1

Increment the clock sequence of v1 UUID state.

The RFC said to increment the clock sequence counter if the system clock was set backwards.
is_uuid/1

Is the binary a UUID?

.
is_v1/1

Is the binary a v1 UUID?

.
is_v3/1

Is the binary a v3 UUID?

.
is_v4/1

Is the binary a v4 UUID?

.
is_v5/1

Is the binary a v5 UUID?

.
mac_address/0

Provide a usable network interface MAC address.

.
new/1

Create new UUID state for v1 UUID generation.

.
new/2

Create new UUID state for v1 UUID generation using a specific type of timestamp.

The timestamp can either be based on erlang:now/0 with erlang or os:timestamp/0 with os.
string_to_uuid/1

Convert a string representation to a UUID.

.
test/0

Regression test.

.
uuid_to_list/1

Convert a UUID to a list.

.
uuid_to_string/1

Convert a UUID to a string representation.

.
uuid_to_string/2

Convert a UUID to a string representation based on an option.

.

Function Details

get_v1/1

get_v1(Uuid_state::#uuid_state{}) -> uuid()

Get a v1 UUID.

get_v1_time/0

get_v1_time() -> non_neg_integer()

Get the current time value in a manner consistent with the v1 UUID.

The result is an integer in microseconds.

get_v1_time/1

get_v1_time(Uuid_state::os | erlang | #uuid_state{} | uuid()) -> non_neg_integer()

Get the current time value in a manner consistent with the v1 UUID.

The result is an integer in microseconds.

get_v3/1

get_v3(Data::binary()) -> uuid()

Get a v3 UUID.

get_v3/2

get_v3(Namespace::dns | url | oid | x500 | binary(), Data::binary() | iolist()) -> uuid()

Get a v3 UUID in a particular namespace.

get_v3_compat/1

get_v3_compat(Data::binary()) -> uuid()

Get a compatible v3 UUID.

Do not use all bits from the checksum so that the UUID matches external implementations.

get_v3_compat/2

get_v3_compat(Namespace::dns | url | oid | x500 | binary(), Data::binary() | iolist()) -> uuid()

Get a compatible v3 UUID in a particular namespace.

Do not use all bits from the checksum so that the UUID matches external implementations.

get_v4/0

get_v4() -> uuid()

Get a v4 UUID (using crypto/openssl).

crypto:strong_rand_bytes/1 repeats in the same way as RAND_bytes within OpenSSL. crypto:rand_bytes/1 repeats in the same way as RAND_pseudo_bytes within OpenSSL. if OpenSSL is configured to use the MD PRNG (default) with SHA1 (in openssl/crypto/rand/md_rand.c), the collisions are between 2^80 and 2^51 (http://eprint.iacr.org/2008/469.pdf). So, that means "weak" would repeat ideally every 1.21e24 and at worst every 2.25e15. if OpenSSL was compiled in FIPS mode, it uses ANSI X9.31 RNG and would have collisions based on 3DES.

get_v4/1

get_v4(X1::strong | weak) -> uuid()

get_v4_urandom/0

get_v4_urandom() -> uuid()

Get a v4 UUID (using Wichmann-Hill 2006).

random_wh06_int:uniform/1 repeats every 2.66e36 (2^121) approx. (see B.A. Wichmann and I.D.Hill, in 'Generating good pseudo-random numbers', Computational Statistics and Data Analysis 51 (2006) 1614-1622) a single random_wh06_int:uniform/1 call can provide a maximum of 124 bits (see random_wh06_int.erl for details)

get_v4_urandom_bigint/0

get_v4_urandom_bigint() -> uuid()

Get a v4 UUID (using Wichmann-Hill 1982).

random:uniform/1 repeats every 2.78e13 (see B.A. Wichmann and I.D.Hill, in 'An efficient and portable pseudo-random number generator', Journal of Applied Statistics. AS183. 1982, or Byte March 1987) a single random:uniform/1 call can provide a maximum of 45 bits (currently this is not significantly faster because multiple function calls are necessary)

explain the 45 bits of randomness: random:uniform/0 code: B1 = (A1*171) rem 30269, B2 = (A2*172) rem 30307, B3 = (A3*170) rem 30323, put(random_seed, {B1,B2,B3}), R = A1/30269 + A2/30307 + A3/30323, R - trunc(R).

{B1, B2, B3} becomes the next seed value {A1, A2, A3}, so: R = (918999161 * A1 + 917846887 * A2 + 917362583 * A3) / 27817185604309 Whatever the values for A1, A2, and A3, (918999161 * A1 + 917846887 * A2 + 917362583 * A3) can not exceed 27817185604309 (30269 * 30307 * 30323) because of the previous modulus. So, random:uniform/1 is unable to uniformly sample numbers beyond a N of 27817185604309. The bits required to represent 27817185604309: 1> (math:log(27817185604309) / math:log(2)) + 1. 45.6610416965467

get_v4_urandom_native/0

get_v4_urandom_native() -> uuid()

Get a v4 UUID (using Wichmann-Hill 1982).

Attempt to only use native integers (Erlang limits integers to 27 bits before using bigints) to investigate the speed when using HiPE.

get_v5/1

get_v5(Data::binary()) -> uuid()

Get a v5 UUID.

get_v5/2

get_v5(Namespace::dns | url | oid | x500 | binary(), Data::binary() | iolist()) -> uuid()

Get a v5 UUID in a particular namespace.

get_v5_compat/1

get_v5_compat(Data::binary()) -> uuid()

Get a compatible v5 UUID.

Do not use all bits from the checksum so that the UUID matches external implementations.

get_v5_compat/2

get_v5_compat(Namespace::dns | url | oid | x500 | binary(), Data::binary() | iolist()) -> uuid()

Get a compatible v5 UUID in a particular namespace.

Do not use all bits from the checksum so that the UUID matches external implementations.

increment/1

increment(State::#uuid_state{}) -> #uuid_state{}

Increment the clock sequence of v1 UUID state.

The RFC said to increment the clock sequence counter if the system clock was set backwards. However, erlang:now/0 always provides increasing time values, so this function is not necessary when the system clock changes. Since the version 1 node id contains the Erlang PID ID, Serial, and Creation numbers in a (non-destructive) bitwise-xor operation, the node id is specific to both the Erlang node and the Erlang node lifetime (the PID Creation is different after a node crash). Therefore, it is unclear why this function would be necessary within this Erlang implementation of v1 UUID generation (if the system is always running). The only event that seems to require this function's usage is if the v1 UUID has been stored and retrieved where both actions occurred at a point with a system clock change inbetween or possibly on different machines with a large difference in system clocks (i.e., in some situation that isn't handled by the Erlang VM, so possibly if an external distribution mechanism was used between Erlang VMs, not connected with distributed Erlang).

is_uuid/1

is_uuid(Value::any()) -> boolean()

Is the binary a UUID?

is_v1/1

is_v1(Value::any()) -> boolean()

Is the binary a v1 UUID?

is_v3/1

is_v3(Value::any()) -> boolean()

Is the binary a v3 UUID?

is_v4/1

is_v4(Value::any()) -> boolean()

Is the binary a v4 UUID?

is_v5/1

is_v5(Value::any()) -> boolean()

Is the binary a v5 UUID?

mac_address/0

mac_address() -> [non_neg_integer()]

Provide a usable network interface MAC address.

new/1

new(Pid::pid()) -> #uuid_state{}

Create new UUID state for v1 UUID generation.

new/2

new(Pid::pid(), Options::os | erlang | [{timestamp_type, os | erlang} | {mac_address, [non_neg_integer()]}]) -> #uuid_state{}

Create new UUID state for v1 UUID generation using a specific type of timestamp.

The timestamp can either be based on erlang:now/0 with erlang or os:timestamp/0 with os. erlang:now/0 will make sure all time values are increasing, even if the system clock changes. os:timestamp/0 will get the system clock quickly without modifying the result.

string_to_uuid/1

string_to_uuid(X1::string() | binary()) -> uuid()

Convert a string representation to a UUID.

test/0

test() -> ok

Regression test.

uuid_to_list/1

uuid_to_list(Value::uuid()) -> iolist()

Convert a UUID to a list.

uuid_to_string/1

uuid_to_string(Value::uuid()) -> string()

Convert a UUID to a string representation.

uuid_to_string/2

uuid_to_string(Value::uuid(), Option::standard | nodash | list_standard | list_nodash | binary_standard | binary_nodash) -> string() | binary()

Convert a UUID to a string representation based on an option.


Generated by EDoc, Sep 30 2014, 14:04:49.