Weekly GitHub Project "Phase III" Go.uuid

Source: Internet
Author: User
Tags posix sha1 unique id
This is a creation in Article, where the information may have evolved or changed.

Go language UUID Package//UUID pack for Go

name Go.uuid
Address Github
Author Satori, etc.
Brief intro UUID Package for Go
Brief introduction Go Language UUID Pack
LICENSE MIT
Stars 170°

What is a UUID?

The full name of the UUID is the universally unique identifier, globally unique authentication, which is a 128-bit number used to identify information in a computer system. The term GUID is also the meaning.

The ultimate goal of the UUID is "uniqueness" and it does not want to rely on central registries or co-ordination between the two parties to generate this uniqueness, with most other numbering plans. Although the UUID will repeat the probability of not zero, but it is close to 0, so it can be negligible.

In the command line, there is a simple method of generating UUID, in the Unix/linux system, as long as the simple execution of Uuidgen can get a new UUID string.

12
$ uuidgen7d976a86-8414-11e7-8ac3-6c92bf136d47

The UUID is a 128-bit number that can be represented as a 32-bit hexadecimal number if it is represented by a 16 binary. Formatting is generally formatted 8-4-4-4-12 as a form, such as

123e4567-e89b-12d3-a456-426655440000
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

Why do you have a UUID?

In distributed systems, because of the high cost of communicating, synchronizing, and maintaining consistency, there are scenarios where you want to generate unique authentication symbols without relying on communication.

How to generate UUID

So how is such a uuid generated?

China is a super-large country, if everyone to generate their own identity card, need to coordinate, then the country is certainly a mess, if everyone to the central registry to determine the uniqueness of the, then the central registration body must be squeezed. Then the identity card generation method is the allocation of the first few digits for the region, the allocation of the middle of the time, the following three bits for a region to determine the uniqueness of the last check can be. This makes it relatively easy to generate a unique ID number.

Similarly, the MAC address and Bluetooth address determination is similar to the strategy, the preceding number of bits is the vendor number, the following number of digits by the manufacturer's internal decision, so as to avoid the conflict.

In fact, the international standard RFC 4122 for UUID defines how 5 versions are generated.

Version 1

Generated based on time and MAC address. Version 1 concatenates the 48-bit MAC address and the 60-bit timestamp.
This timestamp is the number of nanoseconds from the midnight UTC time of October 15, 1582.
According to RFC4122 the provisions, this time can be extended to about 3400 A.D.
However, some software, such as the Libuuid Library, uses timestamps as non-negative types, which can be used in AD 5236.

Version 2

Generated based on time and MAC address, and using the POSIX uid/gid,dce security version.
Version 2 and version 1 are similar except that the most important eight clock sequence bytes are replaced with the local domain number, and the most important 32 timestamp is replaced with the shaping number corresponding to the local domain. On a POSIX system, it is related to the UID and GID. On non-POSIX systems, the system is defined by itself.

Version 3

Hash namespace and name, using MD5 as the hashing algorithm.
The build method for version 3 is the identifier and the name of the hash namespace. The namespace identifier itself is a UUID, which provides a UUID to identify the role of the URL namespace.

Version 4

Randomly generated.
Forgot to say that all of the above versions generated UUID have fields to specify their own version. So not 128-bit can be used arbitrarily.
For a randomly generated version of 4UUID, theoretically there are 2122, or 5.3x1036 species of possibility.

Version 5

Hash namespace and name, using SHA1 as the hashing algorithm. and version 3 are particularly similar. Because the SHA1 algorithm produces a string of 160bit, longer than the MD5, it is forcibly shortened to 128-bit re-insertion.

Conflict

In version 1 or 2, because it is associated with a unique MAC address, the UUID cannot conflict as long as the MAC address does not conflict. Of course, the MAC address can be forged, at that time the conflict is not in our scope of discussion.

However, other versions are likely to have conflicting results. For example, version 4, completely dependent on randomness to produce, two UUID exactly the same is the existence of theory possible.
However, this possibility is extremely low because of the number of bits.
After calculation, the resulting 2.71x1018 uuid has a 50% probability of having at least one conflict.

This probability can be said to be too low, the general application scenario will not have this problem.

Go.uuid

Finally, let's explain the usage of this library. The relative advantage of this library is that it implements RFC4122 all the prescribed versions, although it is not difficult to haha.
Using the sample is simple, here is version 4, and the simplest usage:

12345678910111213141516171819
 Package mainimport ("FMT""Github.com/satori/go.uuid")func  Main()  {//Creating uuid Version 4u1: = uuid. NewV4 () fmt. Printf ("UUIDv4:%s\n", U1)//parsing uuid from string inputU2, err: = uuid. FromString ("6ba7b810-9dad-11d1-80b4-00c04fd430c8")ifnil {fmt. Printf ("Something gone wrong:%s", err)}fmt. Printf ("successfully parsed:%s", U2)}

Reference documents:

    • https://github.com/Knetic/govaluate
    • uuid-wikipedia
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.