public static int HashData(string hashAlgorithmId, ReadOnlySpan <byte> source, Span <byte> destination)
            {
                HashProvider provider = HashProviderDispenser.CreateHashProvider(hashAlgorithmId);

                provider.AppendHashData(source);
                return(provider.FinalizeHashAndReset(destination));
            }
Пример #2
0
        /// <summary>
        /// Create an <see cref="IncrementalHash"/> for the algorithm specified by <paramref name="hashAlgorithm"/>.
        /// </summary>
        /// <param name="hashAlgorithm">The name of the hash algorithm to perform.</param>
        /// <returns>
        /// An <see cref="IncrementalHash"/> instance ready to compute the hash algorithm specified
        /// by <paramref name="hashAlgorithm"/>.
        /// </returns>
        /// <exception cref="ArgumentException">
        ///     <paramref name="hashAlgorithm"/>.<see cref="HashAlgorithmName.Name"/> is <c>null</c>, or
        ///     the empty string.
        /// </exception>
        /// <exception cref="CryptographicException"><paramref name="hashAlgorithm"/> is not a known hash algorithm.</exception>
        public static IncrementalHash CreateHash(HashAlgorithmName hashAlgorithm)
        {
            if (string.IsNullOrEmpty(hashAlgorithm.Name))
                throw new ArgumentException(SR.Cryptography_HashAlgorithmNameNullOrEmpty, nameof(hashAlgorithm));

            return new IncrementalHash(hashAlgorithm, HashProviderDispenser.CreateHashProvider(hashAlgorithm.Name));
        }
Пример #3
0
        private byte[]? ChangeKeyImpl(ReadOnlySpan <byte> key)
        {
            byte[]? modifiedKey = null;

            // If _blockSize is -1 the key isn't going to be extractable by the object holder,
            // so there's no point in recalculating it in managed code.
            if (key.Length > _blockSize && _blockSize > 0)
            {
                // Perform RFC 2104, section 2 key adjustment.
                if (_lazyHashProvider == null)
                {
                    _lazyHashProvider = HashProviderDispenser.CreateHashProvider(_hashAlgorithmId);
                }
                _lazyHashProvider.AppendHashData(key);
                modifiedKey = _lazyHashProvider.FinalizeHashAndReset();
            }

            HashProvider?oldHashProvider = _hMacProvider;

            _hMacProvider = null !;
            oldHashProvider?.Dispose(true);
            _hMacProvider = HashProviderDispenser.CreateMacProvider(_hashAlgorithmId, key);

            return(modifiedKey);
        }
        public HMACManagedHashProvider(string hashAlgorithmId, ReadOnlySpan <byte> key)
        {
            _hash1 = HashProviderDispenser.CreateHashProvider(hashAlgorithmId);
            _hash2 = HashProviderDispenser.CreateHashProvider(hashAlgorithmId);

            (_blockSizeValue, _hashSizeValue) = hashAlgorithmId switch
            {
                HashAlgorithmNames.SHA1 => (64, 160 / 8),
                HashAlgorithmNames.SHA256 => (64, 256 / 8),
                HashAlgorithmNames.SHA384 => (128, 384 / 8),
                HashAlgorithmNames.SHA512 => (128, 512 / 8),
                _ => throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmId)),
            };

            _key = InitializeKey(key);
        }
Пример #5
0
 public Implementation()
 {
     _hashProvider = HashProviderDispenser.CreateHashProvider(HashAlgorithmNames.SHA384);
     HashSizeValue = _hashProvider.HashSizeInBytes * 8;
 }
Пример #6
0
 public SHA256Managed()
 {
     _hashProvider = HashProviderDispenser.CreateHashProvider(HashAlgorithmNames.SHA256);
     HashSizeValue = _hashProvider.HashSizeInBytes * 8;
 }
Пример #7
0
 public Implementation()
 {
     _hashProvider = HashProviderDispenser.CreateHashProvider(HashAlgorithmNames.SHA512);
 }
Пример #8
0
 public Implementation()
 {
     _hashProvider = HashProviderDispenser.CreateHashProvider(HashAlgorithmNames.GOST3411);
     HashSizeValue = GOST3411_SIZE;
 }
Пример #9
0
        /// <summary>
        /// Create an <see cref="IncrementalHash"/> for the algorithm specified by <paramref name="hashAlgorithm"/>.
        /// </summary>
        /// <param name="hashAlgorithm">The name of the hash algorithm to perform.</param>
        /// <returns>
        /// An <see cref="IncrementalHash"/> instance ready to compute the hash algorithm specified
        /// by <paramref name="hashAlgorithm"/>.
        /// </returns>
        /// <exception cref="ArgumentException">
        ///     <paramref name="hashAlgorithm"/>.<see cref="HashAlgorithmName.Name"/> is <c>null</c>, or
        ///     the empty string.
        /// </exception>
        /// <exception cref="CryptographicException"><paramref name="hashAlgorithm"/> is not a known hash algorithm.</exception>
        public static IncrementalHash CreateHash(HashAlgorithmName hashAlgorithm)
        {
            ArgumentException.ThrowIfNullOrEmpty(hashAlgorithm.Name, nameof(hashAlgorithm));

            return(new IncrementalHash(hashAlgorithm, HashProviderDispenser.CreateHashProvider(hashAlgorithm.Name)));
        }
Пример #10
0
 public SHA1Managed()
 {
     _hashProvider = HashProviderDispenser.CreateHashProvider(HashAlgorithmNames.SHA1);
 }
Пример #11
0
 internal LiteHash(string hashAlgorithmId)
 {
     _provider        = HashProviderDispenser.CreateHashProvider(hashAlgorithmId);
     _hashSizeInBytes = _provider.HashSizeInBytes;
 }