TShockAPI.DB.User.CreateBCryptHash C# (CSharp) Method

CreateBCryptHash() public method

Creates a BCrypt hash for a user and stores it in this object.
public CreateBCryptHash ( string password ) : void
password string The plain text password to hash
return void
        public void CreateBCryptHash(string password)
        {
            if (password.Trim().Length < Math.Max(4, TShock.Config.MinimumPasswordLength))
            {
                throw new ArgumentOutOfRangeException("password", "Password must be > " + TShock.Config.MinimumPasswordLength + " characters.");
            }
            try
            {
                Password = BCrypt.Net.BCrypt.HashPassword(password.Trim(), TShock.Config.BCryptWorkFactor);
            }
            catch (ArgumentOutOfRangeException)
            {
                TShock.Log.ConsoleError("Invalid BCrypt work factor in config file! Creating new hash using default work factor.");
                Password = BCrypt.Net.BCrypt.HashPassword(password.Trim());
            }
        }

Same methods

User::CreateBCryptHash ( string password, int workFactor ) : void

Usage Example

示例#1
0
        /// <summary>
        /// Sets the Hashed Password for a given username
        /// </summary>
        /// <param name="user">User user</param>
        /// <param name="password">string password</param>
        public void SetUserPassword(User user, string password)
        {
            try
            {
                user.CreateBCryptHash(password);

                if (
                    _database.Query("UPDATE Users SET Password = @0 WHERE Username = @1;", user.Password,
                                    user.Name) == 0)
                {
                    throw new UserNotExistException(user.Name);
                }
            }
            catch (Exception ex)
            {
                throw new UserManagerException("SetUserPassword SQL returned an error", ex);
            }
        }
All Usage Examples Of TShockAPI.DB.User::CreateBCryptHash