Exemple #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.
                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);
        }
        private IncrementalHash(HashAlgorithmName name, HashProvider hash)
        {
            Debug.Assert(!string.IsNullOrEmpty(name.Name));
            Debug.Assert(hash != null);

            _algorithmName = name;
            _hash          = hash;
        }
Exemple #3
0
        /// <summary>
        /// Saves a stream to a file returns a new HashableFile
        /// </summary>
        /// <param name="StreamToSave">A stream to save to a file</param>
        /// <param name="Provider">If specified computes the hash after writing the file</param>
        /// <param name="SigningCert">If specified with a valid Provider, computes the signature after writing the file</param>
        public HashableFile(Stream StreamToSave, HashProvider?Provider = null, Cert SigningCert = null)
        {
            _filePath = FilePath;

            using (var fileStream = File.Create(_filePath)) {
                StreamToSave.Seek(0, SeekOrigin.Begin);
                StreamToSave.CopyTo(fileStream);
            }

            if (Provider.HasValue)
            {
                this.ComputeHash(Provider.Value, SigningCert);
            }
        }
Exemple #4
0
        /// <summary>
        /// Release all resources used by the current instance of the
        /// <see cref="IncrementalHash"/> class.
        /// </summary>
        public void Dispose()
        {
            _disposed = true;

            if (_hash != null)
            {
                _hash.Dispose();
                _hash = null;
            }

            if (_hmac != null)
            {
                _hmac.Dispose(true);
                _hmac = null;
            }
        }