HashedPassword

public struct HashedPassword

This class represents hashed passwords. They can be used to store passwords for the purpose of authenticating users. Passwords should not be stored as plaintext values to avoid compromise if they get in the wrong hands.

Example

let password = Password("Correct Horse Battery Staple")!
let hashedPassword = password.hash()!

// Store `hashedPassword.string` to database.

// If a user wants to authenticate, just read it from the database and
// verify it against the password given by the user.
if hashedPassword.isVerified(by: password) {
    // The user is authenticated successfully.
}
  • The size of the hashed password string in bytes. As the string is ASCII encoded it will match the number of characters.

    Declaration

    Swift

    public static let SizeInBytes: UInt32
  • Constructs a HashedPassword instance from a hashed password.

    Declaration

    Swift

    public init?(_ bytes: Bytes)

    Parameters

    bytes

    The hashed password as ASCII decoded string.

  • Construct a HashedPassword instance from a hashed password string.

    Declaration

    Swift

    public init?(_ string: String)

    Parameters

    string

    The hashed password as ASCII encoded string.

  • Returns an ASCII encoded representation of the hashed password. This value can be securely stored on disk or in a database.

    Declaration

    Swift

    public var string: String { get }
  • Check if the password password authenticates the hashed password.

    Declaration

    Swift

    public func isVerified(by password: Password) -> Bool

    Parameters

    password

    The password, the user is trying to authenticate with.