Exemple #1
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));
        }
Exemple #2
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.
                modifiedKey = _hashAlgorithmId switch
                {
                    HashAlgorithmNames.SHA256 => SHA256.HashData(key),
                    HashAlgorithmNames.SHA384 => SHA384.HashData(key),
                    HashAlgorithmNames.SHA512 => SHA512.HashData(key),
                    HashAlgorithmNames.SHA1 => SHA1.HashData(key),
                    HashAlgorithmNames.MD5 when Helpers.HasMD5 => MD5.HashData(key),
                    _ => throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, _hashAlgorithmId)),
                };
            }

            HashProvider?oldHashProvider = _hMacProvider;

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

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

                provider.AppendHashData(source);
                return(provider.FinalizeHashAndReset(destination));
            }
Exemple #4
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);
        }
Exemple #6
0
 public Implementation()
 {
     _hashProvider = HashProviderDispenser.CreateHashProvider(HashAlgorithmNames.SHA384);
     HashSizeValue = _hashProvider.HashSizeInBytes * 8;
 }
Exemple #7
0
 public SHA256Managed()
 {
     _hashProvider = HashProviderDispenser.CreateHashProvider(HashAlgorithmNames.SHA256);
     HashSizeValue = _hashProvider.HashSizeInBytes * 8;
 }
Exemple #8
0
 public Implementation()
 {
     _hashProvider = HashProviderDispenser.CreateHashProvider(HashAlgorithmNames.SHA512);
 }
Exemple #9
0
 public Implementation()
 {
     _hashProvider = HashProviderDispenser.CreateHashProvider(HashAlgorithmNames.GOST3411);
     HashSizeValue = GOST3411_SIZE;
 }
Exemple #10
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)));
        }
Exemple #11
0
 public SHA1Managed()
 {
     _hashProvider = HashProviderDispenser.CreateHashProvider(HashAlgorithmNames.SHA1);
 }
Exemple #12
0
 internal LiteHmac(string hashAlgorithmId, ReadOnlySpan <byte> key)
 {
     _provider        = HashProviderDispenser.CreateMacProvider(hashAlgorithmId, key);
     _hashSizeInBytes = _provider.HashSizeInBytes;
 }
Exemple #13
0
 internal LiteHash(string hashAlgorithmId)
 {
     _provider        = HashProviderDispenser.CreateHashProvider(hashAlgorithmId);
     _hashSizeInBytes = _provider.HashSizeInBytes;
 }