Exemplo n.º 1
0
        /// <inheritdoc/>
        public string Create(string value, uint length)
        {
            Guard.MustBeBetweenOrEqualTo <uint>(length, 2, 64, nameof(length));

            using (var hashAlgorithm = SHA256.Create())
            {
                int len = (int)length;

                // Concatenate the hash bytes into one long string.
                int    byteCount = Encoding.ASCII.GetByteCount(value);
                byte[] buffer    = ArrayPool <byte> .Shared.Rent(byteCount);

                Encoding.ASCII.GetBytes(value, 0, value.Length, buffer, 0);
                byte[] hash = hashAlgorithm.ComputeHash(buffer, 0, byteCount);
                ArrayPool <byte> .Shared.Return(buffer);

                var sb = new StringBuilder(len);
                for (int i = 0; i < len / 2; i++)
                {
                    sb.Append(hash[i].ToString("X2"));
                }

                sb.AppendFormat(".{0}", FormatHelpers.GetExtensionOrDefault(this.options.Configuration, value));
                return(sb.ToString());
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public string Create(string value, uint length)
        {
            if (length.CompareTo(2) < 0 || length.CompareTo(64) > 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length), $"Value must be greater than or equal to {2} and less than or equal to {64}.");
            }

            using (var hashAlgorithm = SHA256.Create())
            {
                // Concatenate the hash bytes into one long string.
                int    len  = (int)length;
                byte[] hash = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(value));
                var    sb   = new StringBuilder(len);
                for (int i = 0; i < len / 2; i++)
                {
                    sb.Append(hash[i].ToString("X2"));
                }

                sb.AppendFormat(".{0}", FormatHelpers.GetExtensionOrDefault(this.options.Configuration, value));
                return(sb.ToString());
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Computes a hashed value for the given uri string.
 /// </summary>
 /// <param name="uri">The uri to hash.</param>
 /// <param name="configuration">The library configuration.</param>
 /// <returns>The hashed <see cref="string"/></returns>
 public static string ComputeHash(string uri, Configuration configuration)
 {
     using (var hashAlgorithm = SHA256.Create())
     {
         return($"{Encode(hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(uri)))}.{FormatHelpers.GetExtensionOrDefault(configuration, uri)}");
     }
 }