Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[mono.git] / mcs / class / corlib / System.Security.Cryptography / HMAC.cs
index ed87b6db9afbd1254386b308c8e52a9d4bf029dd..e2c20e29e1cd2fb33a287d65c6b4251b6e304d6b 100644 (file)
@@ -5,7 +5,7 @@
 //     Sebastien Pouliot  <sebastien@ximian.com>
 //
 // (C) 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005, 2007 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
@@ -27,7 +27,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
 
 using System.Runtime.InteropServices;
 using Mono.Security.Cryptography;
@@ -63,13 +62,14 @@ namespace System.Security.Cryptography {
                protected HMAC () 
                {
                        _disposed = false;
+                       _blockSizeValue = 64;
                }
 
                // properties
 
                protected int BlockSizeValue {
                        get { return _blockSizeValue; }
-                       set { _blockSizeValue = value; }
+                       set { _blockSizeValue = value;  }
                }
 
                public string HashName {
@@ -77,30 +77,37 @@ namespace System.Security.Cryptography {
                        set { 
                                _hashName = value; 
                                _algo = HashAlgorithm.Create (_hashName);
-                               _block = new BlockProcessor (_algo, 8);
                        }
                }
 
                public override byte[] Key { 
                        get { return (byte[]) base.Key.Clone (); }
                        set { 
-                               if ((value != null) && (value.Length > 64))
+                               if ((value != null) && (value.Length > BlockSizeValue))
                                        base.Key = _algo.ComputeHash (value);
                                else
                                        base.Key = (byte[]) value.Clone();
                        }
                }
 
+               internal BlockProcessor Block {
+                       get {
+                               if (_block == null)
+                                       _block = new BlockProcessor (_algo, (BlockSizeValue >> 3));
+                               return _block;
+                       }
+               }
+
                // methods
 
                private byte[] KeySetup (byte[] key, byte padding) 
                {
-                       byte[] buf = new byte [64];
+                       byte[] buf = new byte [BlockSizeValue];
        
                        for (int i = 0; i < key.Length; ++i)
                                buf [i] = (byte) ((byte) key [i] ^ padding);
        
-                       for (int i = key.Length; i < 64; ++i)
+                       for (int i = key.Length; i < BlockSizeValue; ++i)
                                buf [i] = padding;
                        
                        return buf;
@@ -122,7 +129,7 @@ namespace System.Security.Cryptography {
                                Initialize ();
                                State = 1;
                        }
-                       _block.Core (rgb, ib, cb);
+                       Block.Core (rgb, ib, cb);
                }
 
                protected override byte[] HashFinal () 
@@ -131,7 +138,7 @@ namespace System.Security.Cryptography {
                                throw new ObjectDisposedException ("HMAC");
                        State = 0;
 
-                       _block.Final ();
+                       Block.Final ();
                        byte[] intermediate = _algo.Hash;
        
                        byte[] buf = KeySetup (Key, 0x5C);
@@ -152,10 +159,10 @@ namespace System.Security.Cryptography {
                                throw new ObjectDisposedException ("HMAC");
 
                        State = 0;
-                       _block.Initialize ();
+                       Block.Initialize ();
                        byte[] buf = KeySetup (Key, 0x36);
                        _algo.Initialize ();
-                       _block.Core (buf);
+                       Block.Core (buf);
                        // zeroize key
                        Array.Clear (buf, 0, buf.Length);
                }
@@ -164,14 +171,17 @@ namespace System.Security.Cryptography {
 
                public static new HMAC Create () 
                {
+#if FULL_AOT_RUNTIME
+                       return new System.Security.Cryptography.HMACSHA1 ();
+#else
                        return Create ("System.Security.Cryptography.HMAC");
+#endif
                }
 
-               public static new HMAC Create (string algName) 
+               public static new HMAC Create (string algorithmName) 
                {
-                       return (HMAC) CryptoConfig.CreateFromName (algName);
+                       return (HMAC) CryptoConfig.CreateFromName (algorithmName);
                }
        }
 }
 
-#endif