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-2006 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;
34 using System.Runtime.InteropServices;
36 namespace System.Security.Cryptography {
41 public abstract class HashAlgorithm : ICryptoTransform {
44 protected internal byte[] HashValue;
46 protected byte[] HashValue;
48 protected int HashSizeValue;
50 private bool disposed;
52 protected HashAlgorithm ()
57 public virtual bool CanTransformMultipleBlocks {
61 public virtual bool CanReuseTransform {
67 // same as System.IDisposable.Dispose() which is documented
71 public byte[] ComputeHash (byte[] input)
74 throw new ArgumentNullException ("input");
76 return ComputeHash (input, 0, input.Length);
79 public byte[] ComputeHash (byte[] buffer, int offset, int count)
82 throw new ObjectDisposedException ("HashAlgorithm");
84 throw new ArgumentNullException ("buffer");
86 throw new ArgumentOutOfRangeException ("offset", "< 0");
88 throw new ArgumentException ("count", "< 0");
89 // ordered to avoid possible integer overflow
90 if (offset > buffer.Length - count) {
91 throw new ArgumentException ("offset + count",
92 Locale.GetText ("Overflow"));
95 HashCore (buffer, offset, count);
96 HashValue = HashFinal ();
102 public byte[] ComputeHash (Stream inputStream)
104 // don't read stream unless object is ready to use
106 throw new ObjectDisposedException ("HashAlgorithm");
108 byte[] buffer = new byte [4096];
109 int len = inputStream.Read (buffer, 0, 4096);
111 HashCore (buffer, 0, len);
112 len = inputStream.Read (buffer, 0, 4096);
114 HashValue = HashFinal ();
119 public static HashAlgorithm Create ()
121 return Create ("System.Security.Cryptography.HashAlgorithm");
124 public static HashAlgorithm Create (string hashName)
126 return (HashAlgorithm) CryptoConfig.CreateFromName (hashName);
129 public virtual byte[] Hash {
131 if (HashValue == null) {
132 throw new CryptographicUnexpectedOperationException (
133 Locale.GetText ("No hash value computed."));
139 protected abstract void HashCore (byte[] rgb, int start, int size);
141 protected abstract byte[] HashFinal ();
143 public virtual int HashSize {
144 get { return HashSizeValue; }
147 public abstract void Initialize ();
149 protected virtual void Dispose (bool disposing)
154 public virtual int InputBlockSize {
158 public virtual int OutputBlockSize {
162 void IDisposable.Dispose ()
165 GC.SuppressFinalize (this); // Finalization is now unnecessary
168 // LAMESPEC: outputBuffer is optional in 2.0 (i.e. can be null).
169 // However a null outputBuffer would throw a ExecutionEngineException under 1.x
170 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
172 if (inputBuffer == null)
173 throw new ArgumentNullException ("inputBuffer");
176 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
178 throw new ArgumentException ("inputCount");
180 // ordered to avoid possible integer overflow
181 if ((inputOffset < 0) || (inputOffset > inputBuffer.Length - inputCount))
182 throw new ArgumentException ("inputBuffer");
184 if (outputBuffer != null) {
185 if (outputOffset < 0) {
187 throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
189 throw new IndexOutOfRangeException ("outputBuffer");
192 // ordered to avoid possible integer overflow
193 if (outputOffset > outputBuffer.Length - inputCount) {
195 throw new ArgumentException ("outputOffset + inputCount",
196 Locale.GetText ("Overflow"));
198 throw new IndexOutOfRangeException ("outputBuffer");
203 HashCore (inputBuffer, inputOffset, inputCount);
205 if (outputBuffer != null)
206 Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
211 public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
213 if (inputBuffer == null)
214 throw new ArgumentNullException ("inputBuffer");
216 throw new ArgumentException ("inputCount");
217 // ordered to avoid possible integer overflow
218 if (inputOffset > inputBuffer.Length - inputCount) {
219 throw new ArgumentException ("inputOffset + inputCount",
220 Locale.GetText ("Overflow"));
223 byte[] outputBuffer = new byte [inputCount];
225 // note: other exceptions are handled by Buffer.BlockCopy
226 Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, 0, inputCount);
228 HashCore (inputBuffer, inputOffset, inputCount);
229 HashValue = HashFinal ();