Exemple #1
0
        private static Sha1 HashImpl(crypt.SHA1 alg, ReadOnlySpan <byte> span)
        {
            Debug.Assert(alg != null);

            // Do NOT short-circuit here; rely on call-sites to do so
#if !NETSTANDARD2_0
            Span <byte> hash = stackalloc byte[Sha1.ByteLength];
            alg.TryComputeHash(span, hash, out _);
#else
            var hash = alg.ComputeHash(span.ToArray());
#endif
            var sha = new Sha1(hash);
            return(sha);
        }
Exemple #2
0
        private static readonly Sha1 s_empty1 = Sha1.Parse("da39a3ee-5e6b4b0d-3255bfef-95601890-afd80709"); // Well-known

        /// <summary>
        /// Hashes the specified bytes.
        /// </summary>
        /// <param name="alg">The SHA1 instance to use.</param>
        /// <param name="span">The bytes to hash.</param>
        /// <returns></returns>
        public static Sha1 HashData(this crypt.SHA1 alg, ReadOnlySpan <byte> span)
        {
            if (alg == null)
            {
                throw new ArgumentNullException(nameof(alg));
            }
            if (span.Length == 0)
            {
                return(s_empty1);
            }

            Sha1 sha = HashImpl(alg, span);

            return(sha);
        }
Exemple #3
0
        /// <summary>
        /// Hashes the specified stream.
        /// </summary>
        /// <param name="alg">The SHA1 instance to use.</param>
        /// <param name="stream">The stream to hash.</param>
        /// <returns></returns>
        public static Sha1 HashData(this crypt.SHA1 alg, Stream stream)
        {
            if (alg == null)
            {
                throw new ArgumentNullException(nameof(alg));
            }
            if (stream is null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            // Note that length==0 should NOT short-circuit

            byte[] hash = alg.ComputeHash(stream);

            var sha = new Sha1(hash);

            return(sha);
        }
Exemple #4
0
        /// <summary>
        /// Hashes the specified bytes.
        /// </summary>
        /// <param name="alg">The SHA1 instance to use.</param>
        /// <param name="bytes">The bytes to hash.</param>
        /// <returns></returns>
        public static Sha1 HashData(this crypt.SHA1 alg, byte[] bytes)
        {
            if (alg == null)
            {
                throw new ArgumentNullException(nameof(alg));
            }
            if (bytes is null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }
            if (bytes.Length == 0)
            {
                return(s_empty1);
            }

            var span = new ReadOnlySpan <byte>(bytes);

            Sha1 sha = HashImpl(alg, span);

            return(sha);
        }
Exemple #5
0
        /// <summary>
        /// Hashes the specified <paramref name="bytes"/>, starting at the specified <paramref name="start"/> and <paramref name="length"/>.
        /// </summary>
        /// <param name="alg">The SHA1 instance to use.</param>
        /// <param name="bytes">The bytes to hash.</param>
        /// <param name="start">The offset.</param>
        /// <param name="length">The count.</param>
        /// <returns></returns>
        public static Sha1 HashData(this crypt.SHA1 alg, byte[] bytes, int start, int length)
        {
            if (alg == null)
            {
                throw new ArgumentNullException(nameof(alg));
            }
            if (bytes is null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

            var span = new ReadOnlySpan <byte>(bytes, start, length); // Check validity of start/length

            if (length == 0)
            {
                return(s_empty1);
            }

            Sha1 sha = HashImpl(alg, span);

            return(sha);
        }
 public override int GetHashCode(Sha1 obj) => obj.GetHashCode();
 public override bool Equals(Sha1 x, Sha1 y) => x.Equals(y);
 public override int Compare(Sha1 x, Sha1 y) => x.CompareTo(y);