Ejemplo n.º 1
0
        /// <summary>
        /// Generate a namespace GUID (V3 or V5) with a given algorithm
        /// </summary>
        /// <param name="namespaceId">The namespace we're generating the GUID in</param>
        /// <param name="name">The name we're generating the GUID for</param>
        /// <param name="version">The version to generate (MD5 or SHA1)</param>
        /// <returns></returns>
        private static UUID GenerateNameBased(System.Guid namespaceId, string name, GuidVersion version)
        {
            if (version != GuidVersion.MD5 && version != GuidVersion.SHA1)
            {
                throw new ArgumentException("version", "Name based guids can only be version 3, or 5");
            }

            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "The name parameter cannot be empty or null");
            }

            byte[] nameBytes      = Encoding.UTF8.GetBytes(name);
            byte[] namespaceBytes = namespaceId.ToByteArray();

            if (BitConverter.IsLittleEndian)
            {
                ToggleEndianess(namespaceBytes);
            }

            var hash = version == GuidVersion.MD5 ?
                       HashProvider.GenerateMD5Hash(namespaceBytes, nameBytes) :
                       HashProvider.GenerateSHA1Hash(namespaceBytes, nameBytes);

            if (BitConverter.IsLittleEndian)
            {
                ToggleEndianess(hash);
            }

            return(GenerateFromComponents(
                       hash.Skip(Constants.TIMESTAMP_BYTE_INDEX).Take(Constants.TIME_BYTES_LENGTH).ToArray(),
                       hash.Skip(Constants.CLOCK_SEQUENCE_BYTE_INDEX).Take(Constants.CLOCK_SEQUENCE_BYTES_LENGTH).ToArray(),
                       hash.Skip(Constants.NODE_BYTE_INDEX).Take(Constants.NODE_BYTES_LENGTH).ToArray(),
                       version, GuidVariant.RFC4122));
        }