Ejemplo n.º 1
0
        public void ChangeKey(byte[] key)
        {
            // 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, 0, key.Length);
                key = _lazyHashProvider.FinalizeHashAndReset();
            }

            HashProvider oldHashProvider = _hMacProvider;

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

            ActualKey = key;
        }
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);
        }
            public static int HashData(string hashAlgorithmId, ReadOnlySpan <byte> source, Span <byte> destination)
            {
                HashProvider provider = HashProviderDispenser.CreateHashProvider(hashAlgorithmId);

                provider.AppendHashData(source);
                return(provider.FinalizeHashAndReset(destination));
            }
Ejemplo n.º 4
0
        public void ChangeKey(byte[] key)
        {
            HashProvider oldHashProvider = _hashProvider;

            _hashProvider = null;
            if (oldHashProvider != null)
            {
                oldHashProvider.Dispose(true);
            }
            _hashProvider = HashProviderDispenser.CreateMacProvider(_hashAlgorithmId, key);
        }
Ejemplo n.º 5
0
        public static unsafe void Fill(
            ReadOnlySpan <byte> password,
            ReadOnlySpan <byte> salt,
            int iterations,
            HashAlgorithmName hashAlgorithmName,
            Span <byte> destination)
        {
            Debug.Assert(!destination.IsEmpty);
            Debug.Assert(hashAlgorithmName.Name is not null);
            IntPtr    evpHashType = HashProviderDispenser.HashAlgorithmToEvp(hashAlgorithmName.Name);
            int       result      = Interop.Crypto.Pbkdf2(password, salt, iterations, evpHashType, destination);
            const int Success     = 1;

            if (result != Success)
            {
                Debug.Assert(result == 0, $"Unexpected result {result}");
                throw Interop.Crypto.CreateOpenSslCryptographicException();
            }
        }