using System; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Security; using System.Security.Cryptography; using System.Security.Permissions; using System.Text; using Microsoft.CodeAnalysis; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")] [assembly: AssemblyCompany("Bradley Grainger")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyCopyright("Copyright 2023 Bradley Grainger")] [assembly: AssemblyDescription("Creates GUIDs according to RFC 4122 and the UUID Revision Working Draft.")] [assembly: AssemblyFileVersion("0.2.0.0")] [assembly: AssemblyInformationalVersion("0.2.0+0752fad0bd0c6f95ace49389bc01bdc07f7d6c59")] [assembly: AssemblyProduct("NGuid")] [assembly: AssemblyTitle("NGuid")] [assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/bgrainger/NGuid.git")] [assembly: InternalsVisibleTo("NGuid.Tests")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("0.0.0.0")] [module: UnverifiableCode] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)] internal sealed class NullableContextAttribute : Attribute { public readonly byte Flag; public NullableContextAttribute(byte P_0) { Flag = P_0; } } [CompilerGenerated] [Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace NGuid { public static class GuidHelpers { public static readonly Guid DnsNamespace = new Guid("6ba7b810-9dad-11d1-80b4-00c04fd430c8"); public static readonly Guid UrlNamespace = new Guid("6ba7b811-9dad-11d1-80b4-00c04fd430c8"); public static readonly Guid IsoOidNamespace = new Guid("6ba7b812-9dad-11d1-80b4-00c04fd430c8"); public static readonly Guid Sha256HashSpaceId = new Guid("3fb32780-953c-4464-9cfd-e85dbbe9843d"); public static readonly Guid Sha384HashSpaceId = new Guid("e6800581-f333-484b-8778-601ff2b58da8"); public static readonly Guid Sha512HashSpaceId = new Guid("0fde22f2-e7ba-4fd1-9753-9c2ea88fa3f9"); private static readonly DateTime s_gregorianEpoch = new DateTime(1582, 10, 15, 0, 0, 0, DateTimeKind.Utc); public static Guid CreateFromName(Guid namespaceId, string name, int version = 5) { if (name == null) { throw new ArgumentNullException("name"); } byte[] bytes = Encoding.UTF8.GetBytes(name); return CreateFromName(namespaceId, bytes, version); } public static Guid CreateFromName(Guid namespaceId, byte[] name, int version = 5) { if ((version != 3 && version != 5) || 1 == 0) { throw new ArgumentOutOfRangeException("version", version, "version must be either 3 or 5."); } byte[] array = namespaceId.ToByteArray(); SwapByteOrder(array); byte[] buffer = array.Concat(name).ToArray(); byte[] sourceArray; using (HashAlgorithm hashAlgorithm = ((version == 3) ? ((HashAlgorithm)MD5.Create()) : ((HashAlgorithm)SHA1.Create()))) { sourceArray = hashAlgorithm.ComputeHash(buffer); } byte[] array2 = new byte[16]; Array.Copy(sourceArray, 0, array2, 0, 16); array2[6] = (byte)((array2[6] & 0xF) | (version << 4)); array2[8] = (byte)((array2[8] & 0x3F) | 0x80); SwapByteOrder(array2); return new Guid(array2); } public static Guid CreateVersion6() { return CreateVersion6(DateTimeOffset.UtcNow); } private static Guid CreateVersion6(DateTimeOffset timestamp) { long ticks = (timestamp.UtcDateTime - s_gregorianEpoch).Ticks; if (ticks < 0) { throw new ArgumentOutOfRangeException("timestamp", timestamp, "The timestamp must be after 15 October 1582."); } uint a = (uint)(ticks >> 28); ushort b = (ushort)(ticks >> 12); ushort c = (ushort)((ticks & 0xFFF) | 0x6000); byte[] array = new byte[8]; using RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create(); randomNumberGenerator.GetBytes(array); return new Guid(a, b, c, (byte)((array[0] & 0x3F) | 0x80), array[1], array[2], array[3], array[4], array[5], array[6], array[7]); } public static Guid CreateVersion6FromVersion1(Guid guid) { byte[] array = guid.ToByteArray(); if ((array[7] & 0xF0) != 16) { throw new ArgumentException("The GUID must be a version 1 GUID.", "guid"); } ulong num = (ulong)((long)(array[7] & 0xF) << 56) | ((ulong)array[6] << 48) | ((ulong)array[5] << 40) | ((ulong)array[4] << 32) | ((ulong)array[3] << 24) | ((ulong)array[2] << 16) | ((ulong)array[1] << 8) | array[0]; array[3] = (byte)(num >> 52); array[2] = (byte)(num >> 44); array[1] = (byte)(num >> 36); array[0] = (byte)(num >> 28); array[5] = (byte)(num >> 20); array[4] = (byte)(num >> 12); array[7] = (byte)(0x60 | ((num >> 8) & 0xF)); array[6] = (byte)num; return new Guid(array); } public static Guid CreateVersion7() { return CreateVersion7(DateTimeOffset.UtcNow); } private static Guid CreateVersion7(DateTimeOffset timestamp) { long num = timestamp.ToUnixTimeMilliseconds(); if (num < 0) { throw new ArgumentOutOfRangeException("timestamp", timestamp, "The timestamp must be after 1 January 1970."); } uint a = (uint)(num >> 16); ushort b = (ushort)num; byte[] array = new byte[10]; using RandomNumberGenerator randomNumberGenerator = RandomNumberGenerator.Create(); randomNumberGenerator.GetBytes(array); ushort c = (ushort)(0x7000uL | (ulong)((array[0] & 0xF) << 8) | array[1]); return new Guid(a, b, c, (byte)((array[2] & 0x3F) | 0x80), array[3], array[4], array[5], array[6], array[7], array[8], array[9]); } public static Guid CreateVersion8(byte[] bytes) { if (bytes == null) { throw new ArgumentNullException("bytes"); } if (bytes.Length < 16) { throw new ArgumentException("At least sixteen bytes must be provided", "bytes"); } byte[] array = new byte[16]; bytes.AsSpan(0, 16).CopyTo(array); SwapByteOrder(array); array[7] = (byte)(0x80 | (array[7] & 0xF)); array[8] = (byte)(0x80 | (array[8] & 0x3F)); return new Guid(array); } public static Guid CreateVersion8FromName(HashAlgorithmName hashAlgorithmName, Guid namespaceId, byte[] name) { var (guid, hashAlgorithm) = GetHashSpaceAndAlgorithm(hashAlgorithmName); using (hashAlgorithm) { byte[] array = guid.ToByteArray(); SwapByteOrder(array); hashAlgorithm.TransformBlock(array, 0, array.Length, null, 0); byte[] array2 = namespaceId.ToByteArray(); SwapByteOrder(array2); hashAlgorithm.TransformBlock(array2, 0, array2.Length, null, 0); hashAlgorithm.TransformFinalBlock(name, 0, name.Length); byte[] array3 = new byte[16]; Array.Copy(hashAlgorithm.Hash, array3, 16); array3[6] = (byte)((array3[6] & 0xF) | 0x80); array3[8] = (byte)((array3[8] & 0x3F) | 0x80); SwapByteOrder(array3); return new Guid(array3); } } private static (Guid NamespaceId, HashAlgorithm Algorithm) GetHashSpaceAndAlgorithm(HashAlgorithmName hashAlgorithmName) { return hashAlgorithmName.Name switch { "SHA256" => (NamespaceId: Sha256HashSpaceId, Algorithm: SHA256.Create()), "SHA384" => (NamespaceId: Sha384HashSpaceId, Algorithm: SHA384.Create()), "SHA512" => (NamespaceId: Sha512HashSpaceId, Algorithm: SHA512.Create()), _ => throw new ArgumentException("Unsupported hash algorithm name: " + hashAlgorithmName.Name, "hashAlgorithmName"), }; } internal static void SwapByteOrder(Span guid) { SwapBytes(guid, 0, 3); SwapBytes(guid, 1, 2); SwapBytes(guid, 4, 5); SwapBytes(guid, 6, 7); } private static void SwapBytes(Span guid, int left, int right) { ref byte source = ref Unsafe.AsRef(in guid[0]); ref byte reference = ref Unsafe.Add(ref source, right); ref byte reference2 = ref Unsafe.Add(ref source, left); byte b = Unsafe.Add(ref source, left); byte b2 = Unsafe.Add(ref source, right); reference = b; reference2 = b2; } } }