mscorlib(4.0.0.0) API with additions
HashAlgorithm.cs
1 using System.IO;
3 
5 {
7  [ComVisible(true)]
8  public abstract class HashAlgorithm : IDisposable, ICryptoTransform
9  {
11  protected int HashSizeValue;
12 
14  protected internal byte[] HashValue;
15 
17  protected int State;
18 
19  private bool m_bDisposed;
20 
23  public virtual int HashSize => HashSizeValue;
24 
30  public virtual byte[] Hash
31  {
32  get
33  {
34  if (m_bDisposed)
35  {
36  throw new ObjectDisposedException(null);
37  }
38  if (State != 0)
39  {
40  throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_HashNotYetFinalized"));
41  }
42  return (byte[])HashValue.Clone();
43  }
44  }
45 
48  public virtual int InputBlockSize => 1;
49 
52  public virtual int OutputBlockSize => 1;
53 
57  public virtual bool CanTransformMultipleBlocks => true;
58 
61  public virtual bool CanReuseTransform => true;
62 
65  public static HashAlgorithm Create()
66  {
67  return Create("System.Security.Cryptography.HashAlgorithm");
68  }
69 
103  public static HashAlgorithm Create(string hashName)
104  {
105  return (HashAlgorithm)CryptoConfig.CreateFromName(hashName);
106  }
107 
112  public byte[] ComputeHash(Stream inputStream)
113  {
114  if (m_bDisposed)
115  {
116  throw new ObjectDisposedException(null);
117  }
118  byte[] array = new byte[4096];
119  int num;
120  do
121  {
122  num = inputStream.Read(array, 0, 4096);
123  if (num > 0)
124  {
125  HashCore(array, 0, num);
126  }
127  }
128  while (num > 0);
129  HashValue = HashFinal();
130  byte[] result = (byte[])HashValue.Clone();
131  Initialize();
132  return result;
133  }
134 
141  public byte[] ComputeHash(byte[] buffer)
142  {
143  if (m_bDisposed)
144  {
145  throw new ObjectDisposedException(null);
146  }
147  if (buffer == null)
148  {
149  throw new ArgumentNullException("buffer");
150  }
151  HashCore(buffer, 0, buffer.Length);
152  HashValue = HashFinal();
153  byte[] result = (byte[])HashValue.Clone();
154  Initialize();
155  return result;
156  }
157 
171  public byte[] ComputeHash(byte[] buffer, int offset, int count)
172  {
173  if (buffer == null)
174  {
175  throw new ArgumentNullException("buffer");
176  }
177  if (offset < 0)
178  {
179  throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
180  }
181  if (count < 0 || count > buffer.Length)
182  {
183  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
184  }
185  if (buffer.Length - count < offset)
186  {
187  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
188  }
189  if (m_bDisposed)
190  {
191  throw new ObjectDisposedException(null);
192  }
193  HashCore(buffer, offset, count);
194  HashValue = HashFinal();
195  byte[] result = (byte[])HashValue.Clone();
196  Initialize();
197  return result;
198  }
199 
215  public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
216  {
217  if (inputBuffer == null)
218  {
219  throw new ArgumentNullException("inputBuffer");
220  }
221  if (inputOffset < 0)
222  {
223  throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
224  }
225  if (inputCount < 0 || inputCount > inputBuffer.Length)
226  {
227  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
228  }
229  if (inputBuffer.Length - inputCount < inputOffset)
230  {
231  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
232  }
233  if (m_bDisposed)
234  {
235  throw new ObjectDisposedException(null);
236  }
237  State = 1;
238  HashCore(inputBuffer, inputOffset, inputCount);
239  if (outputBuffer != null && (inputBuffer != outputBuffer || inputOffset != outputOffset))
240  {
241  Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
242  }
243  return inputCount;
244  }
245 
259  public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
260  {
261  if (inputBuffer == null)
262  {
263  throw new ArgumentNullException("inputBuffer");
264  }
265  if (inputOffset < 0)
266  {
267  throw new ArgumentOutOfRangeException("inputOffset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
268  }
269  if (inputCount < 0 || inputCount > inputBuffer.Length)
270  {
271  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
272  }
273  if (inputBuffer.Length - inputCount < inputOffset)
274  {
275  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
276  }
277  if (m_bDisposed)
278  {
279  throw new ObjectDisposedException(null);
280  }
281  HashCore(inputBuffer, inputOffset, inputCount);
282  HashValue = HashFinal();
283  byte[] array;
284  if (inputCount != 0)
285  {
286  array = new byte[inputCount];
287  Buffer.InternalBlockCopy(inputBuffer, inputOffset, array, 0, inputCount);
288  }
289  else
290  {
291  array = EmptyArray<byte>.Value;
292  }
293  State = 0;
294  return array;
295  }
296 
298  public void Dispose()
299  {
300  Dispose(disposing: true);
301  GC.SuppressFinalize(this);
302  }
303 
305  public void Clear()
306  {
307  ((IDisposable)this).Dispose();
308  }
309 
313  protected virtual void Dispose(bool disposing)
314  {
315  if (disposing)
316  {
317  if (HashValue != null)
318  {
319  Array.Clear(HashValue, 0, HashValue.Length);
320  }
321  HashValue = null;
322  m_bDisposed = true;
323  }
324  }
325 
327  public abstract void Initialize();
328 
333  protected abstract void HashCore(byte[] array, int ibStart, int cbSize);
334 
337  protected abstract byte[] HashFinal();
338  }
339 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
abstract int Read([In] [Out] byte[] buffer, int offset, int count)
When overridden in a derived class, reads a sequence of bytes from the current stream and advances th...
int State
Represents the state of the hash computation.
abstract void HashCore(byte[] array, int ibStart, int cbSize)
When overridden in a derived class, routes data written to the object into the hash algorithm for com...
int HashSizeValue
Represents the size, in bits, of the computed hash code.
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
virtual bool CanReuseTransform
Gets a value indicating whether the current transform can be reused.
static void Clear(Array array, int index, int length)
Sets a range of elements in an array to the default value of each element type.
abstract void Initialize()
Initializes an implementation of the T:System.Security.Cryptography.HashAlgorithm class.
abstract byte [] HashFinal()
When overridden in a derived class, finalizes the hash computation after the last data is processed b...
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
static object CreateFromName(string name, params object[] args)
Creates a new instance of the specified cryptographic object with the specified arguments.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
virtual int OutputBlockSize
When overridden in a derived class, gets the output block size.
virtual bool CanTransformMultipleBlocks
When overridden in a derived class, gets a value indicating whether multiple blocks can be transforme...
static HashAlgorithm Create(string hashName)
Creates an instance of the specified implementation of a hash algorithm.
The exception that is thrown when an operation is performed on a disposed object.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static HashAlgorithm Create()
Creates an instance of the default implementation of a hash algorithm.
byte [] ComputeHash(byte[] buffer, int offset, int count)
Computes the hash value for the specified region of the specified byte array.
Accesses the cryptography configuration information.
Definition: CryptoConfig.cs:17
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Copies a specified number of bytes from a source array starting at a particular offset to a destinati...
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
Represents the base class from which all implementations of cryptographic hash algorithms must derive...
Definition: HashAlgorithm.cs:8
internal byte [] HashValue
Represents the value of the computed hash code.
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
The exception that is thrown when one of the arguments provided to a method is not valid.
int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
Computes the hash value for the specified region of the input byte array and copies the specified reg...
Defines the basic operations of cryptographic transformations.
byte [] ComputeHash(byte[] buffer)
Computes the hash value for the specified byte array.
Manipulates arrays of primitive types.
Definition: Buffer.cs:11
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.Security.Cryptography.HashAlgorithm and optiona...
The exception that is thrown when an unexpected operation occurs during a cryptographic operation.
virtual byte [] Hash
Gets the value of the computed hash code.
virtual int InputBlockSize
When overridden in a derived class, gets the input block size.
byte [] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
Computes the hash value for the specified region of the specified byte array.
virtual int HashSize
Gets the size, in bits, of the computed hash code.
void Clear()
Releases all resources used by the T:System.Security.Cryptography.HashAlgorithm class.
void Dispose()
Releases all resources used by the current instance of the T:System.Security.Cryptography....
byte [] ComputeHash(Stream inputStream)
Computes the hash value for the specified T:System.IO.Stream object.
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16