Ejemplo n.º 1
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);
        }
Ejemplo n.º 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.
                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);
        }
Ejemplo n.º 3
0
 internal LiteHmac(string hashAlgorithmId, ReadOnlySpan <byte> key)
 {
     _provider        = HashProviderDispenser.CreateMacProvider(hashAlgorithmId, key);
     _hashSizeInBytes = _provider.HashSizeInBytes;
 }