├── LICENSE ├── README ├── rebar ├── rebar.config └── src ├── uuid.app.src └── uuid.erl /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2008, Travis Vachon 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are 6 | met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | * Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in the 13 | documentation and/or other materials provided with the distribution. 14 | 15 | * Neither the name of the author nor the names of its contributors 16 | may be used to endorse or promote products derived from this 17 | software without specific prior written permission. 18 | 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 25 | TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/travis/erlang-uuid/f6a1cf9027e4af78b4a81201f4f6db1a822b8316/README -------------------------------------------------------------------------------- /rebar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/travis/erlang-uuid/f6a1cf9027e4af78b4a81201f4f6db1a822b8316/rebar -------------------------------------------------------------------------------- /rebar.config: -------------------------------------------------------------------------------- 1 | {deps, []}. 2 | {erl_opts, [debug_info]}. 3 | {cover_enabled, true}. 4 | 5 | -------------------------------------------------------------------------------- /src/uuid.app.src: -------------------------------------------------------------------------------- 1 | {application, uuid, 2 | [ 3 | {description, "UUID v4 (random) generation and utilities"}, 4 | {vsn, git}, 5 | {registered, []}, 6 | {applications, []}, 7 | {env, []} 8 | ]}. 9 | 10 | -------------------------------------------------------------------------------- /src/uuid.erl: -------------------------------------------------------------------------------- 1 | % Copyright (c) 2008, Travis Vachon 2 | % All rights reserved. 3 | % 4 | % Redistribution and use in source and binary forms, with or without 5 | % modification, are permitted provided that the following conditions are 6 | % met: 7 | % 8 | % * Redistributions of source code must retain the above copyright 9 | % notice, this list of conditions and the following disclaimer. 10 | % 11 | % * Redistributions in binary form must reproduce the above copyright 12 | % notice, this list of conditions and the following disclaimer in the 13 | % documentation and/or other materials provided with the distribution. 14 | % 15 | % * Neither the name of the author nor the names of its contributors 16 | % may be used to endorse or promote products derived from this 17 | % software without specific prior written permission. 18 | % 19 | % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 | % "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 | % LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 | % A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 | % OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 | % SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 25 | % TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 | % PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 | % LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 | % NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | % SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | % 31 | -module(uuid). 32 | -export([v4/0, to_string/1, get_parts/1, to_binary/1]). 33 | 34 | % Generates a random binary UUID. 35 | v4() -> 36 | v4(crypto:rand_uniform(1, round(math:pow(2, 48))) - 1, crypto:rand_uniform(1, round(math:pow(2, 12))) - 1, crypto:rand_uniform(1, round(math:pow(2, 32))) - 1, crypto:rand_uniform(1, round(math:pow(2, 30))) - 1). 37 | 38 | v4(R1, R2, R3, R4) -> 39 | <>. 40 | 41 | % Returns a string representation of a binary UUID. 42 | to_string(U) -> 43 | lists:flatten(io_lib:format("~8.16.0b-~4.16.0b-~4.16.0b-~2.16.0b~2.16.0b-~12.16.0b", get_parts(U))). 44 | 45 | % Returns the 32, 16, 16, 8, 8, 48 parts of a binary UUID. 46 | get_parts(<>) -> 47 | [TL, TM, THV, CSR, CSL, N]. 48 | 49 | % Converts a UUID string in the format of 550e8400-e29b-41d4-a716-446655440000 50 | % (with or without the dashes) to binary. 51 | to_binary(U)-> 52 | convert(lists:filter(fun(Elem) -> Elem /= $- end, U), []). 53 | 54 | % Converts a list of pairs of hex characters (00-ff) to bytes. 55 | convert([], Acc)-> 56 | list_to_binary(lists:reverse(Acc)); 57 | convert([X, Y | Tail], Acc)-> 58 | {ok, [Byte], _} = io_lib:fread("~16u", [X, Y]), 59 | convert(Tail, [Byte | Acc]). 60 | --------------------------------------------------------------------------------