Beispiel #1
0
        /// <summary>
        /// Creates a named, MD5-based (version 3) GUID.
        /// </summary>
        /// <param name="namespace">The GUID that defines the namespace.</param>
        /// <param name="name">The name within that namespace.</param>
        /// <param name="version">The version of GUID to create.</param>
        private static Guid CreateNamed(Guid @namespace, byte[] name, GuidVersion version)
        {
            var namespaceBytes = @namespace.ToBigEndianByteArray();

            byte[] hash;
#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms
#pragma warning disable CA5350 // Do Not Use Weak Cryptographic Algorithms
            using (var algorithm = version == GuidVersion.NameBasedMd5 ? MD5.Create() : SHA1.Create() as HashAlgorithm)
#pragma warning restore CA5350 // Do Not Use Weak Cryptographic Algorithms
#pragma warning restore CA5351 // Do Not Use Broken Cryptographic Algorithms
            {
                algorithm.TransformBlock(namespaceBytes, 0, namespaceBytes.Length, null, 0);
                algorithm.TransformFinalBlock(name, 0, name.Length);
                hash = algorithm.Hash;
            }

            var guidBytes = new byte[16];
            Array.Copy(hash, 0, guidBytes, 0, 16);
            GuidUtility.EndianSwap(guidBytes);

            // Variant RFC4122
            guidBytes[8] = (byte)((guidBytes[8] & 0x3F) | 0x80); // big-endian octet 8

            // Version
            guidBytes[7] = (byte)((guidBytes[7] & 0x0F) | ((int)version << 4)); // big-endian octet 6

            return(new Guid(guidBytes));
        }
Beispiel #2
0
        /// <summary>
        /// Returns a 16-element byte array that contains the value of the GUID, in big-endian format.
        /// </summary>
        /// <param name="guid">The GUID.</param>
        public static byte[] ToBigEndianByteArray(this in Guid guid)
        {
            var result = guid.ToByteArray();

            GuidUtility.EndianSwap(result);
            return(result);
        }