Add src/hk_id.erl
Browse files1234567891011121314151617181920212223242526272829303132333435363738%%%-------------------------------------------------------------------%%% @doc HK-CORE :: identifier generation.%%%%%% Every entity in the system (agents, sessions, jobs, messages,%%% events, operations) is identified by a UUIDv4-style 128-bit%%% random identifier rendered as a lowercase hyphenated hex string.%%% IDs are generated locally (no coordination) because uniqueness%%% only needs to hold within one HyperKitty deployment's event log,%%% not globally; `crypto:strong_rand_bytes/1` gives us%%% cryptographically-strong randomness so collision probability is%%% negligible for any realistic session volume.%%% @end%%%--------------------------------------------------------------------module(hk_id).
-export([new/0, new/1, timestamp_ms/0]).
-spec new() -> binary().new() -> new(<<>>).
%% @doc Generate an id, optionally prefixed (e.g. `new(<<"agent">>)'%% yields `<<"agent_....">>'). Prefixes make ids self-describing in%% logs and the event stream without requiring a lookup.-spec new(binary()) -> binary().new(Prefix) -> <<A:32, B:16, C:16, D:16, E:48>> = crypto:strong_rand_bytes(16), Hex = iolist_to_binary( io_lib:format("~8.16.0b-~4.16.0b-4~3.16.0b-~4.16.0b-~12.16.0b", [A, B, C band 16#0fff, (D band 16#3fff) bor 16#8000, E])), case Prefix of <<>> -> Hex; _ -> <<Prefix/binary, "_", Hex/binary>> end.
-spec timestamp_ms() -> integer().timestamp_ms() -> erlang:system_time(millisecond).src/hk_id.erlAdded+38