Exemplo n.º 1
0
        protected virtual string EncodePassword(string password, string salt)
        {
            if (password == null)
            {
                return(null);
            }

            if (PasswordFormat == MembershipPasswordFormat.Clear)
            {
                return(password);
            }

            byte[] passwordBytes = Encoding.Unicode.GetBytes(password);
            byte[] saltBytes;
            if (salt != null)
            {
                saltBytes = Convert.FromBase64String(salt);
            }
            else
            {
                saltBytes = new byte[0];
            }
            byte[] bytes = new byte[passwordBytes.Length + saltBytes.Length];
            Buffer.BlockCopy(saltBytes, 0, bytes, 0, saltBytes.Length);
            Buffer.BlockCopy(passwordBytes, 0, bytes, saltBytes.Length, passwordBytes.Length);
            if (PasswordFormat == MembershipPasswordFormat.Hashed)
            {
                HashAlgorithm algorithm;
                string        hashName;
                if (string.IsNullOrEmpty(Membership.HashAlgorithmType))
                {
                    hashName = "System.Security.Cryptography.HashAlgorithm";
                }
                else
                {
                    hashName = Membership.HashAlgorithmType;
                }

                if (!SecurityUtilities.IsRepeatableHashAlgorithm(hashName))
                {
                    hashName = "SHA1";
                }
                algorithm = HashAlgorithm.Create(hashName);
                return(Convert.ToBase64String(algorithm.ComputeHash(bytes)));
            }
            return(Convert.ToBase64String(EncryptPassword(bytes)));
        }