Author: krasnov
Date: 2006-11-05 08:32:07 -0500 (Sun, 05 Nov 2006)
New Revision: 67374

Modified:
   trunk/mcs/class/System.Web/System.Web.Security/ChangeLog
   trunk/mcs/class/System.Web/System.Web.Security/MembershipProvider.cs
   trunk/mcs/class/System.Web/System.Web.Security/SqlMembershipProvider.cs
Log:
* MembershipProvider.cs, SqlMembershipProvider.cs: refactoring of 
DecryptPassword and EncryptPassword methods according to documentation


Modified: trunk/mcs/class/System.Web/System.Web.Security/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Security/ChangeLog    2006-11-05 
13:07:49 UTC (rev 67373)
+++ trunk/mcs/class/System.Web/System.Web.Security/ChangeLog    2006-11-05 
13:32:07 UTC (rev 67374)
@@ -1,3 +1,9 @@
+2006-11-05 Vladimir Krasnov <[EMAIL PROTECTED]>
+
+       * MembershipProvider.cs, SqlMembershipProvider.cs:
+       refactoring of DecryptPassword and EncryptPassword methods according
+       to documentation
+
 2006-09-10 Konstantin Triger <[EMAIL PROTECTED]>
 
        * SqlRoleProvider.cs: throw on empty connection string.

Modified: trunk/mcs/class/System.Web/System.Web.Security/MembershipProvider.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Security/MembershipProvider.cs        
2006-11-05 13:07:49 UTC (rev 67373)
+++ trunk/mcs/class/System.Web/System.Web.Security/MembershipProvider.cs        
2006-11-05 13:32:07 UTC (rev 67374)
@@ -77,75 +77,64 @@
                                ValidatingPassword (this, args);
                }
 
-               protected virtual byte[] DecryptPassword (byte[] 
encodedPassword)
+               SymmetricAlgorithm GetAlg (out byte [] decryptionKey)
                {
-                       throw new NotImplementedException ();
-               }
+                       MachineKeySection section = (MachineKeySection) 
WebConfigurationManager.GetSection ("system.web/machineKey");
 
-               protected virtual byte[] EncryptPassword (byte[] password)
-               {
-                       throw new NotImplementedException ();
-               }
+                       if (section.DecryptionKey.StartsWith ("AutoGenerate"))
+                               throw new ProviderException ("You must 
explicitly specify a decryption key in the <machineKey> section when using 
encrypted passwords.");
 
-               public event MembershipValidatePasswordEventHandler 
ValidatingPassword;
+                       string alg_type = section.Decryption;
+                       if (alg_type == "Auto")
+                               alg_type = "AES";
 
-               internal string EncodePassword (string password, 
MembershipPasswordFormat passwordFormat, string salt)
-               {
-                       byte[] password_bytes;
-                       byte[] salt_bytes;
+                       SymmetricAlgorithm alg = null;
+                       if (alg_type == "AES")
+                               alg = Rijndael.Create ();
+                       else if (alg_type == "3DES")
+                               alg = TripleDES.Create ();
+                       else
+                               throw new ProviderException (String.Format 
("Unsupported decryption attribute '{0}' in <machineKey> configuration 
section", alg_type));
 
-                       switch (passwordFormat) {
-                       case MembershipPasswordFormat.Clear:
-                               return password;
-                       case MembershipPasswordFormat.Hashed:
-                               password_bytes = Encoding.Unicode.GetBytes 
(password);
-                               salt_bytes = Convert.FromBase64String (salt);
+                       decryptionKey = section.DecryptionKey192Bits;
+                       return alg;
+               }
 
-                               byte[] hashBytes = new byte[salt_bytes.Length + 
password_bytes.Length];
+               internal const int SALT_BYTES = 16;
+               protected virtual byte [] DecryptPassword (byte [] 
encodedPassword)
+               {
+                       byte [] decryptionKey;
 
-                               Buffer.BlockCopy (salt_bytes, 0, hashBytes, 0, 
salt_bytes.Length);
-                               Buffer.BlockCopy (password_bytes, 0, hashBytes, 
salt_bytes.Length, password_bytes.Length);
+                       using (SymmetricAlgorithm alg = GetAlg (out 
decryptionKey)) {
+                               alg.Key = decryptionKey;
 
-                               MembershipSection section = 
(MembershipSection)WebConfigurationManager.GetSection ("system.web/membership");
-                               string alg_type = section.HashAlgorithmType;
-                               if (alg_type == "") {
-                                       MachineKeySection keysection = 
(MachineKeySection)WebConfigurationManager.GetSection ("system.web/machineKey");
-                                       alg_type = 
keysection.Validation.ToString ();
-                               }
-                               using (HashAlgorithm hash = 
HashAlgorithm.Create (alg_type)) {
-                                       hash.TransformFinalBlock (hashBytes, 0, 
hashBytes.Length);
-                                       return Convert.ToBase64String 
(hash.Hash);
-                               }
-                       case MembershipPasswordFormat.Encrypted:
-                               password_bytes = Encoding.Unicode.GetBytes 
(password);
-                               salt_bytes = Convert.FromBase64String (salt);
+                               using (ICryptoTransform decryptor = 
alg.CreateDecryptor ()) {
 
-                               byte[] buf = new byte[password_bytes.Length + 
salt_bytes.Length];
+                                       byte [] buf = 
decryptor.TransformFinalBlock (encodedPassword, 0, encodedPassword.Length);
+                                       byte [] rv = new byte [buf.Length - 
SALT_BYTES];
 
-                               Array.Copy (salt_bytes, 0, buf, 0, 
salt_bytes.Length);
-                               Array.Copy (password_bytes, 0, buf, 
salt_bytes.Length, password_bytes.Length);
-
-                               return Convert.ToBase64String (EncryptPassword 
(buf));
-                       default:
-                               /* not reached.. */
-                               return null;
+                                       Array.Copy (buf, 16, rv, 0, buf.Length 
- 16);
+                                       return rv;
+                               }
                        }
                }
 
-               internal string DecodePassword (string password, 
MembershipPasswordFormat passwordFormat)
+               protected virtual byte[] EncryptPassword (byte[] password)
                {
-                       switch (passwordFormat) {
-                       case MembershipPasswordFormat.Clear:
-                               return password;
-                       case MembershipPasswordFormat.Hashed:
-                               throw new ProviderException ("Hashed passwords 
cannot be decoded.");
-                       case MembershipPasswordFormat.Encrypted:
-                               return Encoding.Unicode.GetString 
(DecryptPassword (Convert.FromBase64String (password)));
-                       default:
-                               /* not reached.. */
-                               return null;
+                       byte [] decryptionKey;
+                       byte [] iv = new byte [SALT_BYTES];
+
+                       Array.Copy (password, 0, iv, 0, SALT_BYTES);
+                       Array.Clear (password, 0, SALT_BYTES);
+
+                       using (SymmetricAlgorithm alg = GetAlg (out 
decryptionKey)) {
+                               using (ICryptoTransform encryptor = 
alg.CreateEncryptor (decryptionKey, iv)) {
+                                       return encryptor.TransformFinalBlock 
(password, 0, password.Length);
+                               }
                        }
                }
+
+               public event MembershipValidatePasswordEventHandler 
ValidatingPassword;
        }
 }
 #endif

Modified: 
trunk/mcs/class/System.Web/System.Web.Security/SqlMembershipProvider.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Security/SqlMembershipProvider.cs     
2006-11-05 13:07:49 UTC (rev 67373)
+++ trunk/mcs/class/System.Web/System.Web.Security/SqlMembershipProvider.cs     
2006-11-05 13:32:07 UTC (rev 67374)
@@ -43,9 +43,6 @@
 namespace System.Web.Security {
        public class SqlMembershipProvider : MembershipProvider
        {
-
-               const int SALT_BYTES = 16;
-
                /* can this be done just by setting the datetime fields to 0? */
                DateTime DefaultDateTime = new DateTime (1754, 1, 
1).ToUniversalTime ();
 
@@ -104,62 +101,6 @@
                                throw new ArgumentException (String.Format 
("invalid format for {0}", pName));
                }
 
-               SymmetricAlgorithm GetAlg (out byte [] decryptionKey)
-               {
-                       MachineKeySection section = (MachineKeySection) 
WebConfigurationManager.GetSection ("system.web/machineKey");
-
-                       if (section.DecryptionKey.StartsWith ("AutoGenerate"))
-                               throw new ProviderException ("You must 
explicitly specify a decryption key in the <machineKey> section when using 
encrypted passwords.");
-
-                       string alg_type = section.Decryption;
-                       if (alg_type == "Auto")
-                               alg_type = "AES";
-
-                       SymmetricAlgorithm alg = null;
-                       if (alg_type == "AES")
-                               alg = Rijndael.Create ();
-                       else if (alg_type == "3DES")
-                               alg = TripleDES.Create ();
-                       else
-                               throw new ProviderException (String.Format 
("Unsupported decryption attribute '{0}' in <machineKey> configuration 
section", alg_type));
-
-                       decryptionKey = section.DecryptionKey192Bits;
-                       return alg;
-               }
-
-               protected override byte [] DecryptPassword (byte [] 
encodedPassword)
-               {
-                       byte [] decryptionKey;
-
-                       using (SymmetricAlgorithm alg = GetAlg (out 
decryptionKey)) {
-                               alg.Key = decryptionKey;
-
-                               using (ICryptoTransform decryptor = 
alg.CreateDecryptor ()) {
-
-                                       byte [] buf = 
decryptor.TransformFinalBlock (encodedPassword, 0, encodedPassword.Length);
-                                       byte [] rv = new byte [buf.Length - 
SALT_BYTES];
-
-                                       Array.Copy (buf, 16, rv, 0, buf.Length 
- 16);
-                                       return rv;
-                               }
-                       }
-               }
-
-               protected override byte [] EncryptPassword (byte [] password)
-               {
-                       byte [] decryptionKey;
-                       byte [] iv = new byte [SALT_BYTES];
-
-                       Array.Copy (password, 0, iv, 0, SALT_BYTES);
-                       Array.Clear (password, 0, SALT_BYTES);
-
-                       using (SymmetricAlgorithm alg = GetAlg (out 
decryptionKey)) {
-                               using (ICryptoTransform encryptor = 
alg.CreateEncryptor (decryptionKey, iv)) {
-                                       return encryptor.TransformFinalBlock 
(password, 0, password.Length);
-                               }
-                       }
-               }
-
                public override bool ChangePassword (string username, string 
oldPwd, string newPwd)
                {
                        if (username != null) username = username.Trim ();
@@ -1070,6 +1011,64 @@
                        }
                }
 
+               private string EncodePassword (string password, 
MembershipPasswordFormat passwordFormat, string salt)
+               {
+                       byte [] password_bytes;
+                       byte [] salt_bytes;
+
+                       switch (passwordFormat) {
+                               case MembershipPasswordFormat.Clear:
+                                       return password;
+                               case MembershipPasswordFormat.Hashed:
+                                       password_bytes = 
Encoding.Unicode.GetBytes (password);
+                                       salt_bytes = Convert.FromBase64String 
(salt);
+
+                                       byte [] hashBytes = new byte 
[salt_bytes.Length + password_bytes.Length];
+
+                                       Buffer.BlockCopy (salt_bytes, 0, 
hashBytes, 0, salt_bytes.Length);
+                                       Buffer.BlockCopy (password_bytes, 0, 
hashBytes, salt_bytes.Length, password_bytes.Length);
+
+                                       MembershipSection section = 
(MembershipSection) WebConfigurationManager.GetSection 
("system.web/membership");
+                                       string alg_type = 
section.HashAlgorithmType;
+                                       if (alg_type == "") {
+                                               MachineKeySection keysection = 
(MachineKeySection) WebConfigurationManager.GetSection 
("system.web/machineKey");
+                                               alg_type = 
keysection.Validation.ToString ();
+                                       }
+                                       using (HashAlgorithm hash = 
HashAlgorithm.Create (alg_type)) {
+                                               hash.TransformFinalBlock 
(hashBytes, 0, hashBytes.Length);
+                                               return Convert.ToBase64String 
(hash.Hash);
+                                       }
+                               case MembershipPasswordFormat.Encrypted:
+                                       password_bytes = 
Encoding.Unicode.GetBytes (password);
+                                       salt_bytes = Convert.FromBase64String 
(salt);
+
+                                       byte [] buf = new byte 
[password_bytes.Length + salt_bytes.Length];
+
+                                       Array.Copy (salt_bytes, 0, buf, 0, 
salt_bytes.Length);
+                                       Array.Copy (password_bytes, 0, buf, 
salt_bytes.Length, password_bytes.Length);
+
+                                       return Convert.ToBase64String 
(EncryptPassword (buf));
+                               default:
+                                       /* not reached.. */
+                                       return null;
+                       }
+               }
+
+               private string DecodePassword (string password, 
MembershipPasswordFormat passwordFormat)
+               {
+                       switch (passwordFormat) {
+                               case MembershipPasswordFormat.Clear:
+                                       return password;
+                               case MembershipPasswordFormat.Hashed:
+                                       throw new ProviderException ("Hashed 
passwords cannot be decoded.");
+                               case MembershipPasswordFormat.Encrypted:
+                                       return Encoding.Unicode.GetString 
(DecryptPassword (Convert.FromBase64String (password)));
+                               default:
+                                       /* not reached.. */
+                                       return null;
+                       }
+               }
+
                [MonoTODO]
                public override string ApplicationName
                {

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to