Uuid.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Net;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. namespace RepoTasks
  8. {
  9. /// <summary>
  10. /// Implementation of RFC 4122 - A Universally Unique Identifier (UUID) URN Namespace.
  11. /// </summary>
  12. internal static class Uuid
  13. {
  14. /// <summary>
  15. /// Generates a version 3 UUID given a namespace UUID and name. This is based on the algorithm described in
  16. /// RFC 4122 (http://www.apps.ietf.org/rfc/rfc4122.html), section 4.3.
  17. /// </summary>
  18. /// <param name="namespaceId"></param>
  19. /// <param name="name"></param>
  20. /// <returns></returns>
  21. public static Guid Create(Guid namespaceId, string name)
  22. {
  23. // 1. Convert the name to a canonical sequence of octets (as defined by the standards or conventions of its name space); put the name space ID in network byte order.
  24. byte[] namespaceBytes = namespaceId.ToByteArray();
  25. // Octet 0-3
  26. int timeLow = IPAddress.HostToNetworkOrder(BitConverter.ToInt32(namespaceBytes, 0));
  27. // Octet 4-5
  28. short timeMid = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(namespaceBytes, 4));
  29. // Octet 6-7
  30. short timeHiVersion = IPAddress.HostToNetworkOrder(BitConverter.ToInt16(namespaceBytes, 6));
  31. // 2. Compute the hash of the namespace ID concatenated with the name
  32. byte[] nameBytes = Encoding.Unicode.GetBytes(name);
  33. byte[] hashBuffer = new byte[namespaceBytes.Length + nameBytes.Length];
  34. Buffer.BlockCopy(BitConverter.GetBytes(timeLow), 0, hashBuffer, 0, 4);
  35. Buffer.BlockCopy(BitConverter.GetBytes(timeMid), 0, hashBuffer, 4, 2);
  36. Buffer.BlockCopy(BitConverter.GetBytes(timeHiVersion), 0, hashBuffer, 6, 2);
  37. Buffer.BlockCopy(namespaceBytes, 8, hashBuffer, 8, 8);
  38. Buffer.BlockCopy(nameBytes, 0, hashBuffer, 16, nameBytes.Length);
  39. byte[] hash;
  40. using (SHA256 sha256 = SHA256.Create())
  41. {
  42. hash = sha256.ComputeHash(hashBuffer);
  43. }
  44. Array.Resize(ref hash, 16);
  45. // 3. Set octets zero through 3 of the time_low field to octets zero through 3 of the hash.
  46. timeLow = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(hash, 0));
  47. Buffer.BlockCopy(BitConverter.GetBytes(timeLow), 0, hash, 0, 4);
  48. // 4. Set octets zero and one of the time_mid field to octets 4 and 5 of the hash.
  49. timeMid = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(hash, 4));
  50. Buffer.BlockCopy(BitConverter.GetBytes(timeMid), 0, hash, 4, 2);
  51. // 5. Set octets zero and one of the time_hi_and_version field to octets 6 and 7 of the hash.
  52. timeHiVersion = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(hash, 6));
  53. // 6. Set the four most significant bits (bits 12 through 15) of the time_hi_and_version field to the appropriate 4-bit version number from Section 4.1.3.
  54. timeHiVersion = (short)((timeHiVersion & 0x0fff) | 0x3000);
  55. Buffer.BlockCopy(BitConverter.GetBytes(timeHiVersion), 0, hash, 6, 2);
  56. // 7. Set the clock_seq_hi_and_reserved field to octet 8 of the hash.
  57. // 8. Set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively.
  58. hash[8] = (byte)((hash[8] & 0x3f) | 0x80);
  59. // Steps 9-11 are essentially no-ops, but provided for completion sake
  60. // 9. Set the clock_seq_low field to octet 9 of the hash.
  61. // 10. Set octets zero through five of the node field to octets 10 through 15 of the hash.
  62. // 11. Convert the resulting UUID to local byte order.
  63. return new Guid(hash);
  64. }
  65. }
  66. }