2 // System.Security.Cryptography HashAlgorithm Class implementation
5 // Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
6 // Sebastien Pouliot (sebastien@ximian.com)
8 // Copyright 2001 by Matthew S. Ford.
9 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
35 namespace System.Security.Cryptography {
37 public abstract class HashAlgorithm : ICryptoTransform {
39 protected byte[] HashValue;
40 protected int HashSizeValue;
42 private bool disposed;
44 protected HashAlgorithm ()
49 public virtual bool CanTransformMultipleBlocks {
53 public virtual bool CanReuseTransform {
59 // same as System.IDisposable.Dispose() which is documented
63 public byte[] ComputeHash (byte[] input)
66 throw new ArgumentNullException ("input");
68 return ComputeHash (input, 0, input.Length);
71 public byte[] ComputeHash (byte[] buffer, int offset, int count)
74 throw new ObjectDisposedException ("HashAlgorithm");
76 throw new ArgumentNullException ("buffer");
78 throw new ArgumentOutOfRangeException ("offset", "< 0");
80 throw new ArgumentException ("count", "< 0");
81 // ordered to avoid possible integer overflow
82 if (offset > buffer.Length - count) {
83 throw new ArgumentException ("offset + count",
84 Locale.GetText ("Overflow"));
87 HashCore (buffer, offset, count);
88 HashValue = HashFinal ();
94 public byte[] ComputeHash (Stream inputStream)
96 // don't read stream unless object is ready to use
98 throw new ObjectDisposedException ("HashAlgorithm");
100 byte[] buffer = new byte [4096];
101 int len = inputStream.Read (buffer, 0, 4096);
103 HashCore (buffer, 0, len);
104 len = inputStream.Read (buffer, 0, 4096);
106 HashValue = HashFinal ();
111 public static HashAlgorithm Create ()
113 return Create ("System.Security.Cryptography.HashAlgorithm");
116 public static HashAlgorithm Create (string hashName)
118 return (HashAlgorithm) CryptoConfig.CreateFromName (hashName);
121 public virtual byte[] Hash {
123 if (HashValue == null) {
124 throw new CryptographicUnexpectedOperationException (
125 Locale.GetText ("No hash value computed."));
131 protected abstract void HashCore (byte[] rgb, int start, int size);
133 protected abstract byte[] HashFinal ();
135 public virtual int HashSize {
136 get { return HashSizeValue; }
139 public abstract void Initialize ();
141 protected virtual void Dispose (bool disposing)
146 public virtual int InputBlockSize {
150 public virtual int OutputBlockSize {
154 void IDisposable.Dispose ()
157 GC.SuppressFinalize (this); // Finalization is now unnecessary
160 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
162 if (inputBuffer == null)
163 throw new ArgumentNullException ("inputBuffer");
164 if (outputBuffer == null)
165 throw new ArgumentNullException ("outputBuffer");
168 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
170 throw new ArgumentException ("inputCount");
172 // ordered to avoid possible integer overflow
173 if ((inputOffset < 0) || (inputOffset > inputBuffer.Length - inputCount))
174 throw new ArgumentException ("inputBuffer");
175 if (outputOffset < 0) {
177 throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
179 throw new IndexOutOfRangeException ("outputBuffer");
182 if (outputOffset > outputBuffer.Length - inputCount) {
184 throw new ArgumentException ("outputOffset + inputCount",
185 Locale.GetText ("Overflow"));
187 throw new IndexOutOfRangeException ("outputBuffer");
190 // ordered to avoid possible integer overflow
192 // note: other exceptions are handled by Buffer.BlockCopy
193 Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
194 HashCore (inputBuffer, inputOffset, inputCount);
199 public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
201 if (inputBuffer == null)
202 throw new ArgumentNullException ("inputBuffer");
204 throw new ArgumentException ("inputCount");
205 // ordered to avoid possible integer overflow
206 if (inputOffset > inputBuffer.Length - inputCount) {
207 throw new ArgumentException ("inputOffset + inputCount",
208 Locale.GetText ("Overflow"));
211 byte[] outputBuffer = new byte [inputCount];
213 // note: other exceptions are handled by Buffer.BlockCopy
214 Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, 0, inputCount);
216 HashCore (inputBuffer, inputOffset, inputCount);
217 HashValue = HashFinal ();