• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lucaslorentz / durabletask-extensions / 5835662074

pending completion
5835662074

Pull #28

github

lucaslorentz
Add husky and apply some code fixes
Pull Request #28: Add husky and apply code fixes

2502 of 2502 new or added lines in 91 files covered. (100.0%)

2297 of 2792 relevant lines covered (82.27%)

141.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.57
/src/LLL.DurableTask.Worker/Utils/DeterministicGuid.cs
1
using System;
2
using System.Linq;
3
using System.Security.Cryptography;
4
using System.Text;
5

6
namespace LLL.DurableTask.Worker.Utils;
7

8
public static class DeterministicGuid
9
{
10
    /// <summary>
11
    /// Creates a name-based UUID using the algorithm from RFC 4122 §4.3.
12
    /// </summary>
13
    /// <param name="namespaceId">The ID of the namespace.</param>
14
    /// <param name="name">The name (within that namespace).</param>
15
    /// <returns>A UUID derived from the namespace and name.</returns>
16
    public static Guid Create(Guid namespaceId, string name) => Create(namespaceId, name, 5);
4✔
17

18
    /// <summary>
19
    /// Creates a name-based UUID using the algorithm from RFC 4122 §4.3.
20
    /// </summary>
21
    /// <param name="namespaceId">The ID of the namespace.</param>
22
    /// <param name="name">The name (within that namespace).</param>
23
    /// <param name="version">The version number of the UUID to create; this value must be either
24
    /// 3 (for MD5 hashing) or 5 (for SHA-1 hashing).</param>
25
    /// <returns>A UUID derived from the namespace and name.</returns>
26
    public static Guid Create(Guid namespaceId, string name, int version)
27
    {
28
        if (name is null)
4✔
29
            throw new ArgumentNullException(nameof(name));
×
30

31
        // convert the name to a sequence of octets (as defined by the standard or conventions of its namespace) (step 3)
32
        // ASSUME: UTF-8 encoding is always appropriate
33
        return Create(namespaceId, Encoding.UTF8.GetBytes(name), version);
4✔
34
    }
35

36
    /// <summary>
37
    /// Creates a name-based UUID using the algorithm from RFC 4122 §4.3.
38
    /// </summary>
39
    /// <param name="namespaceId">The ID of the namespace.</param>
40
    /// <param name="nameBytes">The name (within that namespace).</param>
41
    /// <returns>A UUID derived from the namespace and name.</returns>
42
    public static Guid Create(Guid namespaceId, byte[] nameBytes) => Create(namespaceId, nameBytes, 5);
×
43

44
    /// <summary>
45
    /// Creates a name-based UUID using the algorithm from RFC 4122 §4.3.
46
    /// </summary>
47
    /// <param name="namespaceId">The ID of the namespace.</param>
48
    /// <param name="nameBytes">The name (within that namespace).</param>
49
    /// <param name="version">The version number of the UUID to create; this value must be either
50
    /// 3 (for MD5 hashing) or 5 (for SHA-1 hashing).</param>
51
    /// <returns>A UUID derived from the namespace and name.</returns>
52
    public static Guid Create(Guid namespaceId, byte[] nameBytes, int version)
53
    {
54
        if (version != 3 && version != 5)
4✔
55
            throw new ArgumentOutOfRangeException(nameof(version), "version must be either 3 or 5.");
×
56

57
        // convert the namespace UUID to network order (step 3)
58
        var namespaceBytes = namespaceId.ToByteArray();
4✔
59
        SwapByteOrder(namespaceBytes);
4✔
60

61
        // compute the hash of the namespace ID concatenated with the name (step 4)
62
        var data = namespaceBytes.Concat(nameBytes).ToArray();
4✔
63
        byte[] hash;
64
        using (var algorithm = version == 3 ? (HashAlgorithm)MD5.Create() : SHA1.Create())
4✔
65
            hash = algorithm.ComputeHash(data);
4✔
66

67
        // most bytes from the hash are copied straight to the bytes of the new GUID (steps 5-7, 9, 11-12)
68
        var newGuid = new byte[16];
4✔
69
        Array.Copy(hash, 0, newGuid, 0, 16);
4✔
70

71
        // 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 (step 8)
72
        newGuid[6] = (byte)((newGuid[6] & 0x0F) | (version << 4));
4✔
73

74
        // set the two most significant bits (bits 6 and 7) of the clock_seq_hi_and_reserved to zero and one, respectively (step 10)
75
        newGuid[8] = (byte)((newGuid[8] & 0x3F) | 0x80);
4✔
76

77
        // convert the resulting UUID to local byte order (step 13)
78
        SwapByteOrder(newGuid);
4✔
79
        return new Guid(newGuid);
4✔
80
    }
81

82
    /// <summary>
83
    /// The namespace for fully-qualified domain names (from RFC 4122, Appendix C).
84
    /// </summary>
85
    public static readonly Guid DnsNamespace = new("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
×
86

87
    /// <summary>
88
    /// The namespace for URLs (from RFC 4122, Appendix C).
89
    /// </summary>
90
    public static readonly Guid UrlNamespace = new("6ba7b811-9dad-11d1-80b4-00c04fd430c8");
×
91

92
    /// <summary>
93
    /// The namespace for ISO OIDs (from RFC 4122, Appendix C).
94
    /// </summary>
95
    public static readonly Guid IsoOidNamespace = new("6ba7b812-9dad-11d1-80b4-00c04fd430c8");
×
96

97
    // Converts a GUID (expressed as a byte array) to/from network order (MSB-first).
98
    internal static void SwapByteOrder(byte[] guid)
99
    {
100
        SwapBytes(guid, 0, 3);
8✔
101
        SwapBytes(guid, 1, 2);
8✔
102
        SwapBytes(guid, 4, 5);
8✔
103
        SwapBytes(guid, 6, 7);
8✔
104
    }
105

106
    private static void SwapBytes(byte[] guid, int left, int right)
107
    {
108
        var temp = guid[left];
32✔
109
        guid[left] = guid[right];
32✔
110
        guid[right] = temp;
32✔
111
    }
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc