项目作者: dburriss

项目描述 :
Named Guid implementation of RFC4122
高级语言: F#
项目地址: git://github.com/dburriss/unique.git
创建时间: 2020-04-19T11:12:01Z
项目社区:https://github.com/dburriss/unique

开源协议:MIT License

下载


unique

Tests
Nuget

Unique allows for the creation of a “Deterministic Guid” which will be unique in a SpaceId over time with a high probability. The same Guid will be generated for a given Namespace ID and name.

Usage

In C# using Unique.CSharp you can use NamedGuid.NewGuid to generate a new Guid. You can check the version of the generated Guid by passing a Guid to Version.

  1. // C# example
  2. using Unique.CSharp;
  3. //...
  4. // Use the pre-defined DNS namespace
  5. Guid uniqueIdForDns = NamedGuid.NewGuid(SpaceId.DNS, "example.com");
  6. Console.WriteLine($"DNS example.com ID is {uniqueIdForDns}");
  7. // OUTPUT: DNS example.com ID is cfbff0d1-9375-5685-968c-48ce8b15ae17
  8. // Use a custom namespace Guid
  9. Guid customGuid = Guid.Parse("AA0F4712-691F-4C72-B5EC-19730324EAFD");
  10. Guid uniqueIdForCustomSpace = NamedGuid.NewGuid(customGuid, "bob@builder.com");
  11. Console.WriteLine($"Custom bob@builder.com ID is {uniqueIdForCustomSpace}");
  12. // OUTPUT: Custom bob@builder.com ID is dbead1ff-3f86-5d73-b577-cb00ee3fccaf
  13. // Get the version of any Guid
  14. int version = NamedGuid.Version(uniqueIdForCustomSpace);
  15. Console.WriteLine($"Version is {version}");
  16. // OUTPUT: Version is 5
  17. // Since the default hash algorithm is SHA-1 the version is always 5
  18. // Use Unique.NamedGuid.newGuid to define the hash algorithm.
  19. version = NamedGuid.Version(Guid.NewGuid());
  20. Console.WriteLine($"Version is {version}");
  21. // OUTPUT: Version is 4

If you need to generate using a byte[] or change the algorithm, use the functions found on Unique.NamedGuid, Unique.NS and Unigue.Algorithm.

  1. // F# example
  2. let guid = NamedGuid.newGuid Algorithm.MD5 NS.DNS "www.example.com"
  3. // val: 5df41881-3aed-3515-88a7-2f4a814cf09e