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.



public init()

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



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


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


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


plaintext String

Plaintext data to digest and verify.

hash String

Existing BCrypt hash to parse version, salt, and existing digest from.


CryptoError if hashing fails or if data conversion fails.


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