[erlang-questions] GUID

Caoyuan dcaoyuan@REDACTED
Thu Oct 25 17:24:50 CEST 2007


I have piece of code, but with no function to read the MAC address of
the local machine interfaces:


-define(NUM_CHR_UPPER,
{$0,$1,$2,$3,$4,$5,$6,$7,$8,$9,$A,$B,$C,$D,$E,$F,$G,$H,$I,$J,$K,$L,$M,$N,$O,$P,$Q,$R,$S,$T,$U,$V,$W,$X,$Y,$Z}).
-define(NUM_CHR_LOWER,
{$0,$1,$2,$3,$4,$5,$6,$7,$8,$9,$a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p,$q,$r,$s,$t,$u,$v,$w,$x,$y,$z}).


number_to_based_str(Number, Base) -> number_to_based_str(Number, Base, []).
number_to_based_str(Number, Base, Digits) when is_integer(Digits) ->
    Str = number_to_based_str(Number, Base, []),
    case Digits - length(Str) of
        Rem when Rem > 0 -> lists:duplicate(Rem, $0) ++ Str;
        _                -> Str
    end;
number_to_based_str(Number, Base, _Acc) when Number < Base ->
    [element(Number + 1, ?NUM_CHR_LOWER)];
number_to_based_str(Number, Base,  Acc) ->
    Msd = Number div Base,
    Lsd = Number rem Base,
    case Msd >= Base of
        true  -> number_to_based_str(Msd, Base, [element(Lsd + 1,
?NUM_CHR_LOWER)|Acc]);
        false -> [element(Msd + 1, ?NUM_CHR_LOWER)|[element(Lsd + 1,
?NUM_CHR_LOWER)|Acc]]
    end.

%% @doc Generates a GUID per http://www.ietf.org/rfc/rfc4122.txt
(version 1 generator)
gen_guid() ->
    TimeIn100NanosBeg = calendar:datetime_to_gregorian_seconds({{1582,
10, 15}, {0, 0, 0}}) * 10000000,
    {_MegaSecs, _Secs, MicroSecs} = now(),
    TimeIn100NanosNow =
calendar:datetime_to_gregorian_seconds(calendar:universal_time()) *
10000000 + MicroSecs * 10,
    Time = TimeIn100NanosNow - TimeIn100NanosBeg,
    TimeStr = number_to_based_str(Time, 16, 2),
    TimeStr1 =
      case 15 - length(TimeStr) of
          Rem when Rem > 0 -> lists:duplicate(Rem, $0) ++ TimeStr;
          _                -> TimeStr
      end,
    TimeHiV = lists:sublist(TimeStr1, 1, 3) ++ "1", %% add version number 1,
    TimeMid = lists:sublist(TimeStr1, 4, 4),
    TimeLow = lists:sublist(TimeStr1, 8, 8),
    ClockSeqHiV = io_format("~2.16.0b", [(random:uniform(256) - 1)
band 16#3f bor 16#80]), %% multiplexed variant type (2 bits)
    ClockSeqLow = io_format("~2.16.0b", [(random:uniform(256) - 1)]),
    Node = "001b631ee26b", %% an Ethernet MAC
    TimeLow ++ "-" ++ TimeMid ++ "-" ++ TimeHiV ++ "-" ++ ClockSeqHiV
++ ClockSeqLow ++ "-" ++ Node.

%% @doc Generates a random GUID per
http://www.ietf.org/rfc/rfc4122.txt (version 4 generator)
%% e.g. output: 372472a2-d557-4630-bc7d-bae54c934da1
%% word*2-, word-, (w)ord-, (w)ord-, word*3
gen_guid_v4() ->
    lists:foldl(
      fun (I, Acc) ->
              B = random:uniform(256) - 1, %% return random number
between 0, 255
              S = if  I == 7 -> %% multiplex version number (4 bits)
                          B1 = B band 16#0f bor 16#40, %% version 4 (random)
                          %% The last 0 in 2.16.0 means fill with
leading 0 if necessay
                          io_format("~2.16.0b", [B1]);
                      I == 9 -> %% multiplexed variant type (2 bits)
                          B1 = B band 16#3f bor 16#80,
                          io_format("~2.16.0b", [B1]);
                      I == 4; I == 6; I == 8; I == 10 ->
                          io_format("~2.16.0b-", [B]);
                      true ->
                          io_format("~2.16.0b", [B])
                  end,
              Acc ++ S
      end, [], lists:seq(1, 16)).

On 10/25/07, Joe Armstrong <erlang@REDACTED> wrote:
> Does anybody have some Erlang code to generate a GUID?
> (or to read the MAC address of the local machine interfaces?)
>
> Thanks
>
> /Joe
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>


-- 
- Caoyuan



More information about the erlang-questions mailing list