Missing Args checks.
[mono.git] / mcs / class / corlib / System.Security.Cryptography / HashAlgorithm.cs
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)
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         [ComVisible (true)]
39         public abstract class HashAlgorithm : ICryptoTransform {
40
41                 protected internal byte[] HashValue;
42                 protected int HashSizeValue;
43                 protected int State;
44                 private bool disposed;
45
46                 protected HashAlgorithm () 
47                 {
48                         disposed = false;
49                 }
50
51                 public virtual bool CanTransformMultipleBlocks {
52                         get { return true; }
53                 }
54
55                 public virtual bool CanReuseTransform {
56                         get { return true; }
57                 }
58
59                 public void Clear () 
60                 {
61                         // same as System.IDisposable.Dispose() which is documented
62                         Dispose (true);
63                 }
64
65                 public byte[] ComputeHash (byte[] buffer) 
66                 {
67                         if (buffer == null)
68                                 throw new ArgumentNullException ("buffer");
69
70                         return ComputeHash (buffer, 0, buffer.Length);
71                 }
72
73                 public byte[] ComputeHash (byte[] buffer, int offset, int count) 
74                 {
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"));
87                         }
88
89                         HashCore (buffer, offset, count);
90                         HashValue = HashFinal ();
91                         Initialize ();
92                         
93                         return HashValue;
94                 }
95
96                 public byte[] ComputeHash (Stream inputStream) 
97                 {
98                         // don't read stream unless object is ready to use
99                         if (disposed)
100                                 throw new ObjectDisposedException ("HashAlgorithm");
101                         if (inputStream == null)
102                                 throw new ArgumentNullException ("InputStream");
103
104                         byte[] buffer = new byte [4096];
105                         int len = inputStream.Read (buffer, 0, 4096);
106                         while (len > 0) {
107                                 HashCore (buffer, 0, len);
108                                 len = inputStream.Read (buffer, 0, 4096);
109                         }
110                         HashValue = HashFinal ();
111                         Initialize ();
112                         return HashValue;
113                 }
114         
115                 public static HashAlgorithm Create () 
116                 {
117 #if FULL_AOT_RUNTIME
118                         return new System.Security.Cryptography.SHA1CryptoServiceProvider ();
119 #else
120                         return Create ("System.Security.Cryptography.HashAlgorithm");
121 #endif
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[] array, int ibStart, int cbSize);
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 #if NET_4_0
163                 public void Dispose ()
164 #else
165                 void IDisposable.Dispose () 
166 #endif
167                 {
168                         Dispose (true);
169                         GC.SuppressFinalize (this);  // Finalization is now unnecessary
170                 }
171
172                 // LAMESPEC: outputBuffer is optional in 2.0 (i.e. can be null).
173                 // However a null outputBuffer would throw a ExecutionEngineException under 1.x
174                 public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) 
175                 {
176                         if (inputBuffer == null)
177                                 throw new ArgumentNullException ("inputBuffer");
178
179                         if (inputOffset < 0)
180                                 throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
181                         if (inputCount < 0)
182                                 throw new ArgumentException ("inputCount");
183
184                         // ordered to avoid possible integer overflow
185                         if ((inputOffset < 0) || (inputOffset > inputBuffer.Length - inputCount))
186                                 throw new ArgumentException ("inputBuffer");
187
188                         if (outputBuffer != null) {
189                                 if (outputOffset < 0) {
190                                         throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
191                                 }
192                                 // ordered to avoid possible integer overflow
193                                 if (outputOffset > outputBuffer.Length - inputCount) {
194                                         throw new ArgumentException ("outputOffset + inputCount", 
195                                                 Locale.GetText ("Overflow"));
196                                 }
197                         }
198
199                         HashCore (inputBuffer, inputOffset, inputCount);
200
201                         if (outputBuffer != null)
202                                 Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, outputOffset, 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 }