Vapor Documentation Beta

Class BCrypt​Digest

public final class 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.

try BCrypt.hash("vapor", cost: 4)

See BCrypt for more information.

Initializers

init()

public init()

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

Methods

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:​)

public func verify(_ plaintext: String, created hash: String) throws -> Bool

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.

let hash = try BCrypt.hash("vapor", cost: 4)
try BCrypt.verify("vapor", created: hash) // true
try BCrypt.verify("foo", created: hash) // false

Parameters

plaintext String

Plaintext data to digest and verify.

hash String

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.