2006-05-30 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Cryptography / HashAlgorithm.cs
1 //
2 // System.Security.Cryptography HashAlgorithm Class implementation
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)
11 //
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.
30 //
31
32 using System.Globalization;
33 using System.IO;
34 using System.Runtime.InteropServices;
35
36 namespace System.Security.Cryptography {
37
38 #if NET_2_0
39         [ComVisible (true)]
40 #endif
41         public abstract class HashAlgorithm : ICryptoTransform {
42
43 #if NET_2_0
44                 protected internal byte[] HashValue;
45 #else
46                 protected byte[] HashValue;
47 #endif
48                 protected int HashSizeValue;
49                 protected int State;
50                 private bool disposed;
51
52                 protected HashAlgorithm () 
53                 {
54                         disposed = false;
55                 }
56
57                 public virtual bool CanTransformMultipleBlocks {
58                         get { return true; }
59                 }
60
61                 public virtual bool CanReuseTransform {
62                         get { return true; }
63                 }
64
65                 public void Clear () 
66                 {
67                         // same as System.IDisposable.Dispose() which is documented
68                         Dispose (true);
69                 }
70
71                 public byte[] ComputeHash (byte[] input) 
72                 {
73                         if (input == null)
74                                 throw new ArgumentNullException ("input");
75
76                         return ComputeHash (input, 0, input.Length);
77                 }
78
79                 public byte[] ComputeHash (byte[] buffer, int offset, int count) 
80                 {
81                         if (disposed)
82                                 throw new ObjectDisposedException ("HashAlgorithm");
83                         if (buffer == null)
84                                 throw new ArgumentNullException ("buffer");
85                         if (offset < 0)
86                                 throw new ArgumentOutOfRangeException ("offset", "< 0");
87                         if (count < 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"));
93                         }
94
95                         HashCore (buffer, offset, count);
96                         HashValue = HashFinal ();
97                         Initialize ();
98                         
99                         return HashValue;
100                 }
101
102                 public byte[] ComputeHash (Stream inputStream) 
103                 {
104                         // don't read stream unless object is ready to use
105                         if (disposed)
106                                 throw new ObjectDisposedException ("HashAlgorithm");
107
108                         byte[] buffer = new byte [4096];
109                         int len = inputStream.Read (buffer, 0, 4096);
110                         while (len > 0) {
111                                 HashCore (buffer, 0, len);
112                                 len = inputStream.Read (buffer, 0, 4096);
113                         }
114                         HashValue = HashFinal ();
115                         Initialize ();
116                         return HashValue;
117                 }
118         
119                 public static HashAlgorithm Create () 
120                 {
121                         return Create ("System.Security.Cryptography.HashAlgorithm");
122                 }
123         
124                 public static HashAlgorithm Create (string hashName)
125                 {
126                         return (HashAlgorithm) CryptoConfig.CreateFromName (hashName);
127                 }
128         
129                 public virtual byte[] Hash {
130                         get { 
131                                 if (HashValue == null) {
132                                         throw new CryptographicUnexpectedOperationException (
133                                                 Locale.GetText ("No hash value computed."));
134                                 }
135                                 return HashValue; 
136                         }
137                 }
138         
139                 protected abstract void HashCore (byte[] rgb, int start, int size);
140
141                 protected abstract byte[] HashFinal ();
142
143                 public virtual int HashSize {
144                         get { return HashSizeValue; }
145                 }
146         
147                 public abstract void Initialize ();
148
149                 protected virtual void Dispose (bool disposing)
150                 {
151                         disposed = true;
152                 }
153         
154                 public virtual int InputBlockSize {
155                         get { return 1; }
156                 }
157         
158                 public virtual int OutputBlockSize {
159                         get { return 1; }
160                 }
161
162                 void IDisposable.Dispose () 
163                 {
164                         Dispose (true);
165                         GC.SuppressFinalize (this);  // Finalization is now unnecessary
166                 }
167                 
168                 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) 
169                 {
170                         if (inputBuffer == null)
171                                 throw new ArgumentNullException ("inputBuffer");
172                         if (outputBuffer == null)
173                                 throw new ArgumentNullException ("outputBuffer");
174
175                         if (inputOffset < 0)
176                                 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
177                         if (inputCount < 0)
178                                 throw new ArgumentException ("inputCount");
179
180                         // ordered to avoid possible integer overflow
181                         if ((inputOffset < 0) || (inputOffset > inputBuffer.Length - inputCount))
182                                 throw new ArgumentException ("inputBuffer");
183                         if (outputOffset < 0) {
184 #if NET_2_0
185                                 throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
186 #else
187                                 throw new IndexOutOfRangeException ("outputBuffer");
188 #endif
189                         }
190                         if (outputOffset > outputBuffer.Length - inputCount) {
191 #if NET_2_0
192                                 throw new ArgumentException ("outputOffset + inputCount", 
193                                         Locale.GetText ("Overflow"));
194 #else
195                                 throw new IndexOutOfRangeException ("outputBuffer");
196 #endif
197                         }
198                         // ordered to avoid possible integer overflow
199
200                         // note: other exceptions are handled by Buffer.BlockCopy
201                         Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
202                         HashCore (inputBuffer, inputOffset, inputCount);
203
204                         return inputCount;
205                 }
206         
207                 public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount) 
208                 {
209                         if (inputBuffer == null)
210                                 throw new ArgumentNullException ("inputBuffer");
211                         if (inputCount < 0)
212                                 throw new ArgumentException ("inputCount");
213                         // ordered to avoid possible integer overflow
214                         if (inputOffset > inputBuffer.Length - inputCount) {
215                                 throw new ArgumentException ("inputOffset + inputCount", 
216                                         Locale.GetText ("Overflow"));
217                         }
218
219                         byte[] outputBuffer = new byte [inputCount];
220                         
221                         // note: other exceptions are handled by Buffer.BlockCopy
222                         Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, 0, inputCount);
223                         
224                         HashCore (inputBuffer, inputOffset, inputCount);
225                         HashValue = HashFinal ();
226                         Initialize ();
227                         
228                         return outputBuffer;
229                 }
230         }
231 }