2007-08-17 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Cryptography / HashAlgorithm.cs
index 48e323ccc0ee82c09c5edeebce064c710838415e..91b7f0f0a20d0bda63a7832a2896c267f8c46b16 100644 (file)
@@ -7,17 +7,44 @@
 //
 // Copyright 2001 by Matthew S. Ford.
 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
-// (C) 2004 Novell (http://www.novell.com)
+// Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
 using System.Globalization;
 using System.IO;
+using System.Runtime.InteropServices;
 
 namespace System.Security.Cryptography {
 
+#if NET_2_0
+       [ComVisible (true)]
+#endif
        public abstract class HashAlgorithm : ICryptoTransform {
 
+#if NET_2_0
+               protected internal byte[] HashValue;
+#else
                protected byte[] HashValue;
+#endif
                protected int HashSizeValue;
                protected int State;
                private bool disposed;
@@ -43,6 +70,9 @@ namespace System.Security.Cryptography {
 
                public byte[] ComputeHash (byte[] input) 
                {
+                       if (input == null)
+                               throw new ArgumentNullException ("input");
+
                        return ComputeHash (input, 0, input.Length);
                }
 
@@ -50,6 +80,17 @@ namespace System.Security.Cryptography {
                {
                        if (disposed)
                                throw new ObjectDisposedException ("HashAlgorithm");
+                       if (buffer == null)
+                               throw new ArgumentNullException ("buffer");
+                       if (offset < 0)
+                               throw new ArgumentOutOfRangeException ("offset", "< 0");
+                       if (count < 0)
+                               throw new ArgumentException ("count", "< 0");
+                       // ordered to avoid possible integer overflow
+                       if (offset > buffer.Length - count) {
+                               throw new ArgumentException ("offset + count", 
+                                       Locale.GetText ("Overflow"));
+                       }
 
                        HashCore (buffer, offset, count);
                        HashValue = HashFinal ();
@@ -123,19 +164,65 @@ namespace System.Security.Cryptography {
                        Dispose (true);
                        GC.SuppressFinalize (this);  // Finalization is now unnecessary
                }
-               
+
+               // LAMESPEC: outputBuffer is optional in 2.0 (i.e. can be null).
+               // However a null outputBuffer would throw a ExecutionEngineException under 1.x
                public int TransformBlock (byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset) 
                {
-                       Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
+                       if (inputBuffer == null)
+                               throw new ArgumentNullException ("inputBuffer");
+
+                       if (inputOffset < 0)
+                               throw new ArgumentOutOfRangeException ("inputOffset", "< 0");
+                       if (inputCount < 0)
+                               throw new ArgumentException ("inputCount");
+
+                       // ordered to avoid possible integer overflow
+                       if ((inputOffset < 0) || (inputOffset > inputBuffer.Length - inputCount))
+                               throw new ArgumentException ("inputBuffer");
+
+                       if (outputBuffer != null) {
+                               if (outputOffset < 0) {
+#if NET_2_0
+                                       throw new ArgumentOutOfRangeException ("outputOffset", "< 0");
+#else
+                                       throw new IndexOutOfRangeException ("outputBuffer");
+#endif
+                               }
+                               // ordered to avoid possible integer overflow
+                               if (outputOffset > outputBuffer.Length - inputCount) {
+#if NET_2_0
+                                       throw new ArgumentException ("outputOffset + inputCount", 
+                                               Locale.GetText ("Overflow"));
+#else
+                                       throw new IndexOutOfRangeException ("outputBuffer");
+#endif
+                               }
+                       }
+
                        HashCore (inputBuffer, inputOffset, inputCount);
 
+                       if (outputBuffer != null)
+                               Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
+
                        return inputCount;
                }
        
                public byte[] TransformFinalBlock (byte[] inputBuffer, int inputOffset, int inputCount) 
                {
+                       if (inputBuffer == null)
+                               throw new ArgumentNullException ("inputBuffer");
+                       if (inputCount < 0)
+                               throw new ArgumentException ("inputCount");
+                       // ordered to avoid possible integer overflow
+                       if (inputOffset > inputBuffer.Length - inputCount) {
+                               throw new ArgumentException ("inputOffset + inputCount", 
+                                       Locale.GetText ("Overflow"));
+                       }
+
                        byte[] outputBuffer = new byte [inputCount];
                        
+                       // note: other exceptions are handled by Buffer.BlockCopy
                        Buffer.BlockCopy (inputBuffer, inputOffset, outputBuffer, 0, inputCount);
                        
                        HashCore (inputBuffer, inputOffset, inputCount);