Skip to content

Type Method

hash(_:salt:)

Creates a bcrypt hash using a provided salt.
static func hash(_ plaintext: String, salt: String) throws(BcryptError) -> String

Parameters

plaintext

The plaintext string to hash.

salt

A valid bcrypt salt (22 or 29 characters).

Return Value

The bcrypt hash string.

Discussion

This method allows you to specify your own salt for hashing. The salt can be either:

  • A 22-character raw salt (e.g., J/dtt5ybYUTCJ/dtt5ybYO)

  • A 29-character full salt including algorithm and cost (e.g., $2b$12$J/dtt5ybYUTCJ/dtt5ybYO)

let hash = try BcryptDigest.hash("vapor", salt: "$2b$12$J/dtt5ybYUTCJ/dtt5ybYO")

Important

For most use cases, prefer hash(_:cost:) which generates a secure random salt automatically.

Throws

BcryptError.invalidSalt if the salt format is invalid, or BcryptError.hashFailure if hashing fails.