Пример #1
0
        private void logButton_Click(object sender, EventArgs e)
        {
            var unitOfWork = new UnitOfWork();

            var username = this.userName.Text;
            var password = PasswordHashAlgorithm.Hash(this.passwordBox.Text);

            var currentUser = unitOfWork.Users.First(user => user.UserName == username);

            if (currentUser == null || currentUser.PasswordHash != password)
            {
                this.wrongLabel.Visible = true;
                this.userName.Text      = string.Empty;
                this.passwordBox.Text   = string.Empty;
            }
            else
            {
                currentUser.IsLogged = true;
                unitOfWork.Commit();

                this.Hide();
                var menu = new Frm_Menu();
                menu.Closed += (s, args) => this.Close();
                menu.Show();
            }
        }
Пример #2
0
        /// <summary>
        /// Creates an instance with specified algorithm and parameters
        /// </summary>
        /// <param name="algorithm"></param>
        /// <param name="saltSize"></param>
        /// <param name="iterations"></param>
        public PasswordHashOptions(PasswordHashAlgorithm algorithm, int?saltSize = null, int?iterations = null)
        {
            HashAlgorithm = algorithm;

            switch (HashAlgorithm)
            {
            case PasswordHashAlgorithm.Sha1:
                SaltSize   = saltSize ?? 10;   // hashed password will contain 40 characters for saltSize = 10
                Iterations = iterations ?? 1024;
                break;

            case PasswordHashAlgorithm.Sha256:
                SaltSize   = saltSize ?? 16;   // hashed password will contain 64 characters for saltSize = 16
                Iterations = iterations ?? 8192;
                break;

            case PasswordHashAlgorithm.Sha384:
                SaltSize   = saltSize ?? 24;   // hashed password will contain 96 characters for saltSize = 24
                Iterations = iterations ?? 10240;
                break;

            case PasswordHashAlgorithm.Sha512:
                SaltSize   = saltSize ?? 32;   // hashed password will contain 128 characters for saltSize = 32
                Iterations = iterations ?? 10240;
                break;
            }
        }
Пример #3
0
        public static string EncodePassword(string password, string salt, PasswordHashAlgorithm pha)
        {
            // 将密码和salt值转换成字节形式并连接起来
            byte[] bytes   = Encoding.Unicode.GetBytes(password);
            byte[] src     = Convert.FromBase64String(salt);
            byte[] dst     = new byte[src.Length + bytes.Length];
            byte[] inArray = null;
            Buffer.BlockCopy(src, 0, dst, 0, src.Length);
            Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
            // 用SHA1算法,对连接后的值进行散列
            HashAlgorithm algorithm = null;

            switch (pha)
            {
            case PasswordHashAlgorithm.sha1:
                algorithm = SHA1.Create();
                break;

            case PasswordHashAlgorithm.md5:
                algorithm = MD5.Create();
                break;
            }
            if (algorithm == null)
            {
                throw new Exception("HashAlgorithm Is Null!");
            }
            inArray = algorithm.ComputeHash(dst);
            // 以字符串形式返回散列值
            return(Convert.ToBase64String(inArray));
        }
 public UserManagementService(
     IPublisher publisher, IODispatcher ioDispatcher, PasswordHashAlgorithm passwordHashAlgorithm,
     bool skipInitializeStandardUsersCheck)
 {
     _log                              = LogManager.GetLoggerFor <UserManagementService>();
     _publisher                        = publisher;
     _ioDispatcher                     = ioDispatcher;
     _passwordHashAlgorithm            = passwordHashAlgorithm;
     _skipInitializeStandardUsersCheck = skipInitializeStandardUsersCheck;
 }
Пример #5
0
        public void LengthRoundTrip(PasswordHashAlgorithm algorithm, int saltSize, int expectedLength)
        {
            // Arrange
            var hasher = new PasswordHash(BuildOptions(algorithm, saltSize));

            // Act & assert - success case
            var hashedPassword = hasher.HashPassword("password 1");

            Assert.Equal(expectedLength, hashedPassword.Length);
        }
Пример #6
0
 public UserEntry(string uname, PasswordHashAlgorithm alg, string hashedPass)
 {
     m_name = uname;
     m_hashAlg = alg;
     m_hashedPass = hashedPass;
     if (m_hashAlg != PasswordHashAlgorithm.NONE)
         m_passBytes = this.Decode(m_hashedPass);
     else
         m_passBytes = null;
 }
Пример #7
0
        public byte[] DeriveKey(string password)
        {
            var passwordDeriveBytes = new PasswordDeriveBytes(password, PasswordSalt);
            var initialVector       = new byte[InitialVector.Length];

            InitialVector.CopyTo(initialVector, 0);
            return(passwordDeriveBytes.CryptDeriveKey(SymmetricAlgorithm.GetType().BaseType.Name,
                                                      PasswordHashAlgorithm.GetType().BaseType.Name,
                                                      SymmetricAlgorithm.LegalKeySizes.First().MaxSize,
                                                      initialVector));
        }
Пример #8
0
 public UserEntry(string uname, PasswordHashAlgorithm alg, string hashedPass)
 {
     m_name       = uname;
     m_hashAlg    = alg;
     m_hashedPass = hashedPass;
     if (m_hashAlg != PasswordHashAlgorithm.NONE)
     {
         m_passBytes = this.Decode(m_hashedPass);
     }
     else
     {
         m_passBytes = null;
     }
 }
        /// <summary>
        /// Returns true if OrgApacheJackrabbitOakSecurityAuthenticationTokenTokenConfiguraProperties instances are equal
        /// </summary>
        /// <param name="other">Instance of OrgApacheJackrabbitOakSecurityAuthenticationTokenTokenConfiguraProperties to be compared</param>
        /// <returns>Boolean</returns>
        public bool Equals(OrgApacheJackrabbitOakSecurityAuthenticationTokenTokenConfiguraProperties other)
        {
            if (other is null)
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            return
                ((
                     TokenExpiration == other.TokenExpiration ||
                     TokenExpiration != null &&
                     TokenExpiration.Equals(other.TokenExpiration)
                     ) &&
                 (
                     TokenLength == other.TokenLength ||
                     TokenLength != null &&
                     TokenLength.Equals(other.TokenLength)
                 ) &&
                 (
                     TokenRefresh == other.TokenRefresh ||
                     TokenRefresh != null &&
                     TokenRefresh.Equals(other.TokenRefresh)
                 ) &&
                 (
                     TokenCleanupThreshold == other.TokenCleanupThreshold ||
                     TokenCleanupThreshold != null &&
                     TokenCleanupThreshold.Equals(other.TokenCleanupThreshold)
                 ) &&
                 (
                     PasswordHashAlgorithm == other.PasswordHashAlgorithm ||
                     PasswordHashAlgorithm != null &&
                     PasswordHashAlgorithm.Equals(other.PasswordHashAlgorithm)
                 ) &&
                 (
                     PasswordHashIterations == other.PasswordHashIterations ||
                     PasswordHashIterations != null &&
                     PasswordHashIterations.Equals(other.PasswordHashIterations)
                 ) &&
                 (
                     PasswordSaltSize == other.PasswordSaltSize ||
                     PasswordSaltSize != null &&
                     PasswordSaltSize.Equals(other.PasswordSaltSize)
                 ));
        }
Пример #10
0
        public void HashRoundTrip(PasswordHashAlgorithm algorithm)
        {
            // Arrange
            var hasher = new PasswordHash(BuildOptions(algorithm));

            // Act & assert - success case
            var hashedPassword = hasher.HashPassword("password 1");
            var successResult  = hasher.VerifyHashedPassword(hashedPassword, "password 1");

            Assert.True(successResult);

            // Act & assert - failure case
            var failedResult = hasher.VerifyHashedPassword(hashedPassword, "password 2");

            Assert.False(failedResult);
        }
Пример #11
0
        public void OptionsRoundTrip(PasswordHashAlgorithm algorithm, int expectedSize)
        {
            // Arrange
            var options = new PasswordHashOptions(algorithm);

            // Act & assert - success case
            Assert.Equal(expectedSize, options.HashSize);

            // Arrange
            options.HashAlgorithm = PasswordHashAlgorithm.Sha1;
            options.SaltSize      = expectedSize;
            options.HashAlgorithm = algorithm;

            // Act & assert - failure case
            Assert.Equal(expectedSize, options.HashSize);
        }
 /// <summary>
 /// Gets the hash code
 /// </summary>
 /// <returns>Hash code</returns>
 public override int GetHashCode()
 {
     unchecked // Overflow is fine, just wrap
     {
         var hashCode = 41;
         // Suitable nullity checks etc, of course :)
         if (TokenExpiration != null)
         {
             hashCode = hashCode * 59 + TokenExpiration.GetHashCode();
         }
         if (TokenLength != null)
         {
             hashCode = hashCode * 59 + TokenLength.GetHashCode();
         }
         if (TokenRefresh != null)
         {
             hashCode = hashCode * 59 + TokenRefresh.GetHashCode();
         }
         if (TokenCleanupThreshold != null)
         {
             hashCode = hashCode * 59 + TokenCleanupThreshold.GetHashCode();
         }
         if (PasswordHashAlgorithm != null)
         {
             hashCode = hashCode * 59 + PasswordHashAlgorithm.GetHashCode();
         }
         if (PasswordHashIterations != null)
         {
             hashCode = hashCode * 59 + PasswordHashIterations.GetHashCode();
         }
         if (PasswordSaltSize != null)
         {
             hashCode = hashCode * 59 + PasswordSaltSize.GetHashCode();
         }
         return(hashCode);
     }
 }
 /// <summary>
 /// Gets the hash code
 /// </summary>
 /// <returns>Hash code</returns>
 public override int GetHashCode()
 {
     unchecked // Overflow is fine, just wrap
     {
         var hashCode = 41;
         // Suitable nullity checks etc, of course :)
         if (UsersPath != null)
         {
             hashCode = hashCode * 59 + UsersPath.GetHashCode();
         }
         if (GroupsPath != null)
         {
             hashCode = hashCode * 59 + GroupsPath.GetHashCode();
         }
         if (SystemRelativePath != null)
         {
             hashCode = hashCode * 59 + SystemRelativePath.GetHashCode();
         }
         if (DefaultDepth != null)
         {
             hashCode = hashCode * 59 + DefaultDepth.GetHashCode();
         }
         if (ImportBehavior != null)
         {
             hashCode = hashCode * 59 + ImportBehavior.GetHashCode();
         }
         if (PasswordHashAlgorithm != null)
         {
             hashCode = hashCode * 59 + PasswordHashAlgorithm.GetHashCode();
         }
         if (PasswordHashIterations != null)
         {
             hashCode = hashCode * 59 + PasswordHashIterations.GetHashCode();
         }
         if (PasswordSaltSize != null)
         {
             hashCode = hashCode * 59 + PasswordSaltSize.GetHashCode();
         }
         if (OmitAdminPw != null)
         {
             hashCode = hashCode * 59 + OmitAdminPw.GetHashCode();
         }
         if (SupportAutoSave != null)
         {
             hashCode = hashCode * 59 + SupportAutoSave.GetHashCode();
         }
         if (PasswordMaxAge != null)
         {
             hashCode = hashCode * 59 + PasswordMaxAge.GetHashCode();
         }
         if (InitialPasswordChange != null)
         {
             hashCode = hashCode * 59 + InitialPasswordChange.GetHashCode();
         }
         if (PasswordHistorySize != null)
         {
             hashCode = hashCode * 59 + PasswordHistorySize.GetHashCode();
         }
         if (PasswordExpiryForAdmin != null)
         {
             hashCode = hashCode * 59 + PasswordExpiryForAdmin.GetHashCode();
         }
         if (CacheExpiration != null)
         {
             hashCode = hashCode * 59 + CacheExpiration.GetHashCode();
         }
         if (EnableRFC7613UsercaseMappedProfile != null)
         {
             hashCode = hashCode * 59 + EnableRFC7613UsercaseMappedProfile.GetHashCode();
         }
         return(hashCode);
     }
 }
        /// <summary>
        /// Returns true if OrgApacheJackrabbitOakSecurityUserUserConfigurationImplProperties instances are equal
        /// </summary>
        /// <param name="other">Instance of OrgApacheJackrabbitOakSecurityUserUserConfigurationImplProperties to be compared</param>
        /// <returns>Boolean</returns>
        public bool Equals(OrgApacheJackrabbitOakSecurityUserUserConfigurationImplProperties other)
        {
            if (other is null)
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            return
                ((
                     UsersPath == other.UsersPath ||
                     UsersPath != null &&
                     UsersPath.Equals(other.UsersPath)
                     ) &&
                 (
                     GroupsPath == other.GroupsPath ||
                     GroupsPath != null &&
                     GroupsPath.Equals(other.GroupsPath)
                 ) &&
                 (
                     SystemRelativePath == other.SystemRelativePath ||
                     SystemRelativePath != null &&
                     SystemRelativePath.Equals(other.SystemRelativePath)
                 ) &&
                 (
                     DefaultDepth == other.DefaultDepth ||
                     DefaultDepth != null &&
                     DefaultDepth.Equals(other.DefaultDepth)
                 ) &&
                 (
                     ImportBehavior == other.ImportBehavior ||
                     ImportBehavior != null &&
                     ImportBehavior.Equals(other.ImportBehavior)
                 ) &&
                 (
                     PasswordHashAlgorithm == other.PasswordHashAlgorithm ||
                     PasswordHashAlgorithm != null &&
                     PasswordHashAlgorithm.Equals(other.PasswordHashAlgorithm)
                 ) &&
                 (
                     PasswordHashIterations == other.PasswordHashIterations ||
                     PasswordHashIterations != null &&
                     PasswordHashIterations.Equals(other.PasswordHashIterations)
                 ) &&
                 (
                     PasswordSaltSize == other.PasswordSaltSize ||
                     PasswordSaltSize != null &&
                     PasswordSaltSize.Equals(other.PasswordSaltSize)
                 ) &&
                 (
                     OmitAdminPw == other.OmitAdminPw ||
                     OmitAdminPw != null &&
                     OmitAdminPw.Equals(other.OmitAdminPw)
                 ) &&
                 (
                     SupportAutoSave == other.SupportAutoSave ||
                     SupportAutoSave != null &&
                     SupportAutoSave.Equals(other.SupportAutoSave)
                 ) &&
                 (
                     PasswordMaxAge == other.PasswordMaxAge ||
                     PasswordMaxAge != null &&
                     PasswordMaxAge.Equals(other.PasswordMaxAge)
                 ) &&
                 (
                     InitialPasswordChange == other.InitialPasswordChange ||
                     InitialPasswordChange != null &&
                     InitialPasswordChange.Equals(other.InitialPasswordChange)
                 ) &&
                 (
                     PasswordHistorySize == other.PasswordHistorySize ||
                     PasswordHistorySize != null &&
                     PasswordHistorySize.Equals(other.PasswordHistorySize)
                 ) &&
                 (
                     PasswordExpiryForAdmin == other.PasswordExpiryForAdmin ||
                     PasswordExpiryForAdmin != null &&
                     PasswordExpiryForAdmin.Equals(other.PasswordExpiryForAdmin)
                 ) &&
                 (
                     CacheExpiration == other.CacheExpiration ||
                     CacheExpiration != null &&
                     CacheExpiration.Equals(other.CacheExpiration)
                 ) &&
                 (
                     EnableRFC7613UsercaseMappedProfile == other.EnableRFC7613UsercaseMappedProfile ||
                     EnableRFC7613UsercaseMappedProfile != null &&
                     EnableRFC7613UsercaseMappedProfile.Equals(other.EnableRFC7613UsercaseMappedProfile)
                 ));
        }
Пример #15
0
        public static string GetHash(string s)
        {
            var encode = PasswordHashAlgorithm.ComputeHash(PasswordEncoding.GetBytes(s));

            return(PasswordEncoding.GetString(encode));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.HideAll();

            var unitOfWork = new UnitOfWork();

            var userName           = this.userNameRegister.Text;
            var password           = this.passwordRegister.Text;
            var confirmPassword    = this.confirmPasswordRegister.Text;
            var email              = this.emailRegister.Text;
            var validationUsername = UsernameRegisterValidation.IsValid(userName);
            var validationPassword = PasswordRegisterValidation.IsValid(password);
            var validationEmail    = EmailRegisterValidation.IsValid(email);

            if (validationUsername != UsernameRegisterValidation.UsernameType.Success)
            {
                if (validationUsername == UsernameRegisterValidation.UsernameType.Empty)
                {
                    this.usernameEmpty.Visible = true;
                }
                else if (validationUsername == UsernameRegisterValidation.UsernameType.Lenght)
                {
                    this.usernameLenght.Visible = true;
                }
                else
                {
                    this.usernameInvalid.Visible = true;
                }
            }
            else if (unitOfWork.Users.Any(user => user.UserName == userName.ToLower()))
            {
                this.usernameExists.Visible = true;
            }
            else if (validationPassword != PasswordRegisterValidation.PasswordType.Success)
            {
                if (validationPassword == PasswordRegisterValidation.PasswordType.Empty)
                {
                    this.passwordEmpty.Visible = true;
                }
                else if (validationPassword == PasswordRegisterValidation.PasswordType.Lenght)
                {
                    this.passwordLenght.Visible = true;
                }
                else
                {
                    this.passwordInvalid.Visible = true;
                }

                this.passwordRegister.Text        = string.Empty;
                this.confirmPasswordRegister.Text = string.Empty;
            }
            else if (password != confirmPassword)
            {
                this.confirmPasswordInvalid.Visible = true;

                this.confirmPasswordRegister.Text = string.Empty;
            }
            else if (validationEmail != EmailRegisterValidation.EmailValidationMsg.Success)
            {
                if (validationEmail == EmailRegisterValidation.EmailValidationMsg.Empty)
                {
                    this.emailEmpty.Visible = true;
                }
                else
                {
                    this.emailInvalid.Visible = true;
                }

                this.emailRegister.Text = string.Empty;
            }
            else if (unitOfWork.Users.Any(user => user.Email == email.ToLower()))
            {
                this.emailExists.Visible = true;
            }
            else
            {
                User user = new User()
                {
                    UserName     = userName.ToLower(),
                    PasswordHash = PasswordHashAlgorithm.Hash(password),
                    Email        = email
                };

                unitOfWork.Users.Add(user);

                unitOfWork.Commit();

                MessageBox.Show("SUCCESSFUL REGISTRATION !!!");

                this.Hide();
                LoginForm login = new LoginForm();
                login.Closed += (s, args) => this.Close();
                login.Show();
            }
        }
Пример #17
0
 /// <summary>
 /// Generates a password hash using a hash algorithm
 /// </summary>
 /// <param name="password"></param>
 /// <returns></returns>
 private string GeneratePassHash(string password)
 {
     return(Encoding.UTF8.GetString(PasswordHashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(password))));
 }
Пример #18
0
 public static IOptions <PasswordHashOptions> BuildOptions(PasswordHashAlgorithm algorithm, int?saltSize = null, int?iterations = null)
 {
     return(new PasswordHashOptions(algorithm, saltSize, iterations));
 }
 public HashAlgorithmInfo(PasswordHashAlgorithm algorithm, int iterations, byte[] salt)
 {
     Algorithm  = algorithm;
     Iterations = iterations;
     Salt       = salt;
 }