Skip to content

BCryptDigest

Josh Wright edited this page Jan 14, 2021 · 3 revisions

BCryptDigest

Creates and verifies BCrypt hashes. Normally you will not need to initialize one of these classes and you will use the global BCrypt convenience instead.

public final class BCryptDigest
try BCrypt.hash("vapor", cost: 4)

See BCrypt for more information.

Initializers

init()

Creates a new BCryptDigest. Use the global BCrypt convenience variable.

public init()

Methods

hashAsync(_:)

Asynchronously hashes a password on a separate thread.

public func hashAsync(_ password: String) -> EventLoopFuture<String>

Parameters

  • password: The password to hash.

Returns

A future containing the hashed password that will resolve on the initiating EventLoop.

verifyAsync(plaintext:hashed:)

Asynchronously verifies a password & hash on a separate thread.

public func verifyAsync(plaintext: String, hashed: String) -> EventLoopFuture<Bool>

Parameters

  • plaintext: The plaintext password.
  • hashed: The hashed password to verify with.

Returns

A future containing a Bool indicating whether the password and hash matched. This will resolve on the initiating EventLoop.

hash(_:cost:)

public func hash(_ plaintext: String, cost: Int = 12) throws -> String

hash(_:salt:)

public func hash(_ plaintext: String, salt: String) throws -> String

verify(_:created:)

Verifies an existing BCrypt hash matches the supplied plaintext value. Verification works by parsing the salt and version from the existing digest and using that information to hash the plaintext data. If hash digests match, this method returns true.

public func verify(_ plaintext: String, created hash: String) throws -> Bool
let hash = try BCrypt.hash("vapor", cost: 4)
try BCrypt.verify("vapor", created: hash) // true
try BCrypt.verify("foo", created: hash) // false

Parameters

  • plaintext: Plaintext data to digest and verify.
  • hash: Existing BCrypt hash to parse version, salt, and existing digest from.

Throws

CryptoError if hashing fails or if data conversion fails.

Returns

true if the hash was created from the supplied plaintext data.

Alchemy
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Fusion
Types
Protocols
Papyrus
Types
Protocols
Clone this wiki locally