2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / corlib / System.Security.Cryptography / HashAlgorithm.cs
blobb8aac3a7678923528107223e15a9f3f30222d77c
1 //
2 // System.Security.Cryptography.HashAlgorithm.cs
3 //
4 // Authors:
5 // Matthew S. Ford (Matthew.S.Ford@Rose-Hulman.Edu)
6 // Sebastien Pouliot (sebastien@ximian.com)
7 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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;
33 using System.IO;
34 using System.Runtime.InteropServices;
36 namespace System.Security.Cryptography {
38 [ComVisible (true)]
39 public abstract class HashAlgorithm : ICryptoTransform {
41 protected internal byte[] HashValue;
42 protected int HashSizeValue;
43 protected int State;
44 private bool disposed;
46 protected HashAlgorithm ()
48 disposed = false;
51 public virtual bool CanTransformMultipleBlocks {
52 get { return true; }
55 public virtual bool CanReuseTransform {
56 get { return true; }
59 public void Clear ()
61 // same as System.IDisposable.Dispose() which is documented
62 Dispose (true);
65 public byte[] ComputeHash (byte[] buffer)
67 if (buffer == null)
68 throw new ArgumentNullException ("buffer");
70 return ComputeHash (buffer, 0, buffer.Length);
73 public byte[] ComputeHash (byte[] buffer, int offset, int count)
75 if (disposed)
76 throw new ObjectDisposedException ("HashAlgorithm");
77 if (buffer == null)
78 throw new ArgumentNullException ("buffer");
79 if (offset < 0)
80 throw new ArgumentOutOfRangeException ("offset", "< 0");
81 if (count < 0)
82 throw new ArgumentException ("count", "< 0");
83 // ordered to avoid possible integer overflow
84 if (offset > buffer.Length - count) {
85 throw new ArgumentException ("offset + count",
86 Locale.GetText ("Overflow"));
89 HashCore (buffer, offset, count);
90 HashValue = HashFinal ();
91 Initialize ();
93 return HashValue;
96 public byte[] ComputeHash (Stream inputStream)
98 // don't read stream unless object is ready to use
99 if (disposed)
100 throw new ObjectDisposedException ("HashAlgorithm");
102 byte[] buffer = new byte [4096];
103 int len = inputStream.Read (buffer, 0, 4096);
104 while (len > 0) {
105 HashCore (buffer, 0, len);
106 len = inputStream.Read (buffer, 0, 4096);
108 HashValue = HashFinal ();
109 Initialize ();
110 return HashValue;
113 public static HashAlgorithm Create ()
115 return Create ("System.Security.Cryptography.HashAlgorithm");
118 public static HashAlgorithm Create (string hashName)
120 return (HashAlgorithm) CryptoConfig.CreateFromName (hashName);
123 public virtual byte[] Hash {
124 get {
125 if (HashValue == null) {
126 throw new CryptographicUnexpectedOperationException (
127 Locale.GetText ("No hash value computed."));
129 return HashValue;
133 protected abstract void HashCore (byte[] array, int ibStart, int cbSize);
135 protected abstract byte[] HashFinal ();
137 public virtual int HashSize {
138 get { return HashSizeValue; }
141 public abstract void Initialize ();
143 protected virtual void Dispose (bool disposing)
145 disposed = true;
148 public virtual int InputBlockSize {
149 get { return 1; }
152 public virtual int OutputBlockSize {
153 get { return 1; }
156 void IDisposable.Dispose ()
158 Dispose (true);
159 GC.SuppressFinalize (this); // Finalization is now unnecessary
162 // LAMESPEC: outputBuffer is optional in 2.0 (i.e. can be null).
163 // However a null outputBuffer would throw a ExecutionEngineException under 1.x
164 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
166 if (inputBuffer == null)
167 throw new ArgumentNullException ("inputBuffer");
169 if (inputOffset < 0)
170 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
171 if (inputCount < 0)
172 throw new ArgumentException ("inputCount");
174 // ordered to avoid possible integer overflow
175 if ((inputOffset < 0) || (inputOffset > inputBuffer.Length - inputCount))
176 throw new ArgumentException ("inputBuffer");
178 if (outputBuffer != null) {
179 if (outputOffset < 0) {
180 throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
182 // ordered to avoid possible integer overflow
183 if (outputOffset > outputBuffer.Length - inputCount) {
184 throw new ArgumentException ("outputOffset + inputCount",
185 Locale.GetText ("Overflow"));
189 HashCore (inputBuffer, inputOffset, inputCount);
191 if (outputBuffer != null)
192 Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
194 return inputCount;
197 public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount)
199 if (inputBuffer == null)
200 throw new ArgumentNullException ("inputBuffer");
201 if (inputCount < 0)
202 throw new ArgumentException ("inputCount");
203 // ordered to avoid possible integer overflow
204 if (inputOffset > inputBuffer.Length - inputCount) {
205 throw new ArgumentException ("inputOffset + inputCount",
206 Locale.GetText ("Overflow"));
209 byte[] outputBuffer = new byte [inputCount];
211 // note: other exceptions are handled by Buffer.BlockCopy
212 Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, 0, inputCount);
214 HashCore (inputBuffer, inputOffset, inputCount);
215 HashValue = HashFinal ();
216 Initialize ();
218 return outputBuffer;