Merge pull request #1542 from ninjarobot/UriTemplateMatchException
[mono.git] / mcs / class / corlib / System.Security.Cryptography / PasswordDeriveBytes.cs
index a722a7857c3f81e830bd8b7ecfeb73a1a00d5a1d..57d80eafba05fd9a7a789c52d34178db73e5202c 100644 (file)
@@ -5,7 +5,7 @@
 //     Sebastien Pouliot (sebastien@ximian.com)
 //
 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
-// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-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
@@ -39,9 +39,7 @@ namespace System.Security.Cryptography {
 // b.  IETF RFC2898: PKCS #5: Password-Based Cryptography Specification Version 2.0
 //     http://www.rfc-editor.org/rfc/rfc2898.txt
 
-#if NET_2_0
 [ComVisible (true)]
-#endif
 public class PasswordDeriveBytes : DeriveBytes {
 
        private string HashNameValue;
@@ -84,7 +82,6 @@ public class PasswordDeriveBytes : DeriveBytes {
                }
        }
 
-#if NET_2_0
        public PasswordDeriveBytes (byte[] password, byte[] salt) 
        {
                Prepare (password, salt, "SHA1", 100);
@@ -112,9 +109,8 @@ public class PasswordDeriveBytes : DeriveBytes {
                                Locale.GetText ("CspParameters not supported by Mono for PasswordDeriveBytes."));
                }
        }
-#endif
 
-       ~PasswordDeriveBytes () 
+       protected override void Dispose (bool disposing)
        {
                // zeroize buffer
                if (initial != null) {
@@ -122,10 +118,13 @@ public class PasswordDeriveBytes : DeriveBytes {
                        initial = null;
                }
                // zeroize temporary password storage
-               Array.Clear (password, 0, password.Length);
+               if (password != null) {
+                       Array.Clear (password, 0, password.Length);
+                       password = null;
+               }
+               base.Dispose (disposing);
        }
 
-#if NET_2_0
        private void Prepare (string strPassword, byte[] rgbSalt, string strHashName, int iterations) 
        {
                if (strPassword == null)
@@ -149,25 +148,6 @@ public class PasswordDeriveBytes : DeriveBytes {
                IterationCount = iterations;
                state = 0;
        }
-#else
-       private void Prepare (string strPassword, byte[] rgbSalt, string strHashName, int iterations)
-       {
-               if (strPassword == null)
-                       password = null;
-               else
-                       password = Encoding.UTF8.GetBytes (strPassword);
-
-               if (rgbSalt == null)
-                       SaltValue = null;
-               else
-                       SaltValue = (byte[]) rgbSalt.Clone ();
-
-               HashName = strHashName;
-               IterationCount = iterations;
-               state = 0;
-       }
-#endif
-
        public string HashName {
                get { return HashNameValue; } 
                set {
@@ -205,15 +185,10 @@ public class PasswordDeriveBytes : DeriveBytes {
                                throw new CryptographicException (
                                        Locale.GetText ("Can't change this property at this stage"));
                        }
-#if NET_2_0
                        if (value != null)
                                SaltValue = (byte[]) value.Clone ();
                        else
                                SaltValue = null;
-#else
-                       // this will cause a NullReferenceException if set to null (like 1.0/1.1)
-                       SaltValue = (byte[]) value.Clone ();
-#endif
                }
        }
 
@@ -228,21 +203,20 @@ public class PasswordDeriveBytes : DeriveBytes {
        }
 
        // note: Key is returned - we can't zeroize it ourselve :-(
+       [Obsolete ("see Rfc2898DeriveBytes for PKCS#5 v2 support")]
+ #pragma warning disable 809
        public override byte[] GetBytes (int cb) 
        {
-#if ! NET_2_0
-               // 1.0/1.1 was a little late at throwing the argument exception ;-)
-               if (password == null)
-                       throw new ArgumentNullException ("Password");
-#endif
+ #pragma warning restore 809
+
                if (cb < 1)
                        throw new IndexOutOfRangeException ("cb");
 
                if (state == 0) {
-                       state = 1;
                        // it's now impossible to change the HashName, Salt
                        // and IterationCount
                        Reset ();
+                       state = 1;
                }
 
                byte[] result = new byte [cb];
@@ -295,7 +269,7 @@ public class PasswordDeriveBytes : DeriveBytes {
 
        public override void Reset () 
        {
-               // note: Reset doesn't change state
+               state = 0;
                position = 0;
                hashnumber = 0;