4/29/2023 0 Comments Golang uuid generator![]() The generated type 3 or type 5 UUID holds a (partial) hash of the namespace id and name-within-namespace (key). This process can be repeated recursively so that if for instance the "value" associated with a UUID key in turn represents some sort of logical "namespace" like a bucket, container or directory, then its UUID can be used in turn to generate more hierarchical UUIDs. These UUIDs can be thrown into a single key-value store with high probability of avoiding collision. Now you can create unique UUIDs for keys within a namespace using " uuid -v5 $NAMESPACEUUID $KEY". Alternatively you could create one random UUID for your root (or use the null UUID: 00000000-0000-0000-0000-000000000000 as root) and then create a reproducible UUID for each namespace using " uuid -v5 $ROOTUUID $NAMESPACENAME". This could be a type 1 (host+timestamp) or type 4 (random) UUID so long as you stash it somewhere. First, create a root UUID for each namespace. You can generate a large number of distinct logical namespaces using type 3 or type 5 UUIDs. Suppose you have a (key,value) store, but it only supports one namespace. That said, it gives you a way of creating a repeatable/verifiable "hash" function mapping a possibly hierarchical name onto a probabilistically unique 128-bit value, potentially acting like a hierarchical hash or MAC. (Also MD5 is considered cryptographically broken, and SHA1 is on its last legs, so don't use this to verify data that needs to be "very secure"). Only 128-bits are available and 5 bits are used to specify the type, so all of the hash bits don't make it into the UUID. Type 3 UUIDs use MD5 and type 5 UUIDs use SHA1. Roughly speaking, a type 3 or type 5 UUID is generated by hashing together a namespace identifier with a name. Name and namespace can be used to create a hierarchy of (very probably) unique UUIDs. ![]() The name is just the text you want to have appended to the namespace, then hashed, and stuffed into a UUID: uuid = NameToUUID('8e884ace-bee4-11e4-8dfc-aa07a5b093db', 'screwdriver') The name is a string of arbitrary length. 1: UUID Namespace_RectalForeignExtractedObject = '8e884ace-bee4-11e4-8dfc-aa07a5b093db' It can be one of the pre-defined ones, or you can make up your own, e.g. The namespace is either a UUID in string representation or an identifier for internally pre-defined namespace UUIDs (currently known are "ns:DNS", "ns:URL", "ns:OID", and "ns:X500"). Now you can have calls: uuid = NameToUUID(Namespace_DNS, 'uuid = NameToUUID(Namespace_DNS, 'uuid = NameToUUID(Namespace_URL, '') įor version 3 and version 5 UUIDs the additional command line arguments namespace and name have to be given. (Note: the endian-ness of your system can affect indices of the above bytes) set high-nibble to 5 to indicate type 5 Copy first 16-bytes of the hash into our Uuid result Note: All code on stackoverflow is public domain - no attribution required.īyte hash = sha1(NamespaceUUID.ToBytes() + Name.ToBytes()) Now that you have a function that generates a so-called Name, you can have the function (in pseudo-code): UUID NameToUUID(UUID NamespaceUUID, String Name) The basic gist is to only take the first 128 bits, stuff a 5 in the type record, and then set the first two bits of the clock_seq_hi_and_reserved section to 1 and 0, respectively. StackOverflowUrlUUID = sha1(Namespace_URL + "") So, you could hash together: StackOverflowDnsUUID = sha1(Namespace_DNS + "") The UUID RFC pre-defines four namespaces for you: ![]() You prefix your string with a so-called namespace to prevent name conflicts. You're probably wondering what is it that I'm supposed to hash. ╰─low nibble is set to 5, to indicate type 5 ⭡ ⬑first two bits set to 1 and 0, respectively Type 6: unofficial idea for sequential UUIDsĮdit: Unofficial type 6 now has an official rfcĪn SHA1 hash outputs 160 bits (20 bytes) the result of the hash is converted into a UUID.Type 5: stuffs an SHA1 hash into 128 bits.Type 4: stuffs random data into 128 bits.Type 3: stuffs an MD5 hash into 128 bits.Type 1: stuffs MAC address+ datetime into 128 bits.Type 3 and Type 5 UUIDs are just a technique of stuffing a hash into a UUID:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |