Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace System.Security.Cryptography
- {
- [ComVisible(true)]
- public abstract class HashAlgorithm : ICryptoTransform, IDisposable
- {
- protected int HashSizeValue;
- protected internal byte[] HashValue;
- protected int State;
- private bool m_bDisposed;
- public virtual int HashSize
- {
- get
- {
- return this.HashSizeValue;
- }
- }
- public virtual byte[] Hash
- {
- get
- {
- if (this.m_bDisposed)
- throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
- if (this.State != 0)
- throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_HashNotYetFinalized"));
- return (byte[]) this.HashValue.Clone();
- }
- }
- public static HashAlgorithm Create()
- {
- return HashAlgorithm.Create("System.Security.Cryptography.HashAlgorithm");
- }
- public static HashAlgorithm Create(string hashName)
- {
- return (HashAlgorithm) CryptoConfig.CreateFromName(hashName);
- }
- public byte[] ComputeHash(Stream inputStream)
- {
- if (this.m_bDisposed)
- throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
- byte[] numArray1 = new byte[4096];
- int cbSize;
- do
- {
- cbSize = inputStream.Read(numArray1, 0, 4096);
- if (cbSize > 0)
- this.HashCore(numArray1, 0, cbSize);
- }
- while (cbSize > 0);
- this.HashValue = this.HashFinal();
- byte[] numArray2 = (byte[]) this.HashValue.Clone();
- this.Initialize();
- return numArray2;
- }
- public byte[] ComputeHash(byte[] buffer)
- {
- if (this.m_bDisposed)
- throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
- if (buffer == null)
- throw new ArgumentNullException(nameof (buffer));
- this.HashCore(buffer, 0, buffer.Length);
- this.HashValue = this.HashFinal();
- byte[] numArray = (byte[]) this.HashValue.Clone();
- this.Initialize();
- return numArray;
- }
- public byte[] ComputeHash(byte[] buffer, int offset, int count)
- {
- if (this.m_bDisposed)
- throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
- if (buffer == null)
- throw new ArgumentNullException(nameof (buffer));
- if (offset < 0)
- throw new ArgumentOutOfRangeException(nameof (offset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
- if (count < 0 || count > buffer.Length)
- throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
- if (buffer.Length - count < offset)
- throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
- this.HashCore(buffer, offset, count);
- this.HashValue = this.HashFinal();
- byte[] numArray = (byte[]) this.HashValue.Clone();
- this.Initialize();
- return numArray;
- }
- public virtual int InputBlockSize
- {
- get
- {
- return 1;
- }
- }
- public virtual int OutputBlockSize
- {
- get
- {
- return 1;
- }
- }
- public virtual bool CanTransformMultipleBlocks
- {
- get
- {
- return true;
- }
- }
- public virtual bool CanReuseTransform
- {
- get
- {
- return true;
- }
- }
- public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
- {
- if (this.m_bDisposed)
- throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
- if (inputBuffer == null)
- throw new ArgumentNullException(nameof (inputBuffer));
- if (inputOffset < 0)
- throw new ArgumentOutOfRangeException(nameof (inputOffset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
- if (inputCount < 0 || inputCount > inputBuffer.Length)
- throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
- if (inputBuffer.Length - inputCount < inputOffset)
- throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
- this.State = 1;
- this.HashCore(inputBuffer, inputOffset, inputCount);
- if (outputBuffer != null && (inputBuffer != outputBuffer || inputOffset != outputOffset))
- Buffer.BlockCopy((Array) inputBuffer, inputOffset, (Array) outputBuffer, outputOffset, inputCount);
- return inputCount;
- }
- public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
- {
- if (this.m_bDisposed)
- throw new ObjectDisposedException((string) null, Environment.GetResourceString("ObjectDisposed_Generic"));
- if (inputBuffer == null)
- throw new ArgumentNullException(nameof (inputBuffer));
- if (inputOffset < 0)
- throw new ArgumentOutOfRangeException(nameof (inputOffset), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
- if (inputCount < 0 || inputCount > inputBuffer.Length)
- throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
- if (inputBuffer.Length - inputCount < inputOffset)
- throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
- this.HashCore(inputBuffer, inputOffset, inputCount);
- this.HashValue = this.HashFinal();
- byte[] numArray = new byte[inputCount];
- if (inputCount != 0)
- Buffer.InternalBlockCopy((Array) inputBuffer, inputOffset, (Array) numArray, 0, inputCount);
- this.State = 0;
- return numArray;
- }
- void IDisposable.Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize((object) this);
- }
- public void Clear()
- {
- ((IDisposable) this).Dispose();
- }
- protected virtual void Dispose(bool disposing)
- {
- if (!disposing)
- return;
- if (this.HashValue != null)
- Array.Clear((Array) this.HashValue, 0, this.HashValue.Length);
- this.HashValue = (byte[]) null;
- this.m_bDisposed = true;
- }
- public abstract void Initialize();
- protected abstract void HashCore(byte[] array, int ibStart, int cbSize);
- protected abstract byte[] HashFinal();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement