Bcrypt - CosmosSwift/swift-coin GitHub Wiki

Bcrypt

public enum Bcrypt

Methods

hash(_:cost:)

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

hash(_:salt:)

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

verify(_:against:)

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 static func verify(_ plaintext: String, against hash: String) throws -> Bool
let hash = try BCrypt.hash("password", cost: 4)
try BCrypt.verify("password", against: 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.

generateSalt(cost:algorithm:seed:)

Generates string (29 chars total) containing the algorithm information + the cost + base-64 encoded 22 character salt

public static func generateSalt(cost: Int, algorithm: Algorithm = ._2b, seed: [UInt8]? = nil) -> String
E.g:  $2b$05$J/dtt5ybYUTCJ/dtt5ybYO
      $AA$ => Algorithm
         $CC$ => Cost
             SSSSSSSSSSSSSSSSSSSSSS => Salt

Allowed charset for the salt: [./A-Za-z0-9]

Parameters

  • cost: Desired complexity. Larger cost values take longer to hash and verify.
  • algorithm: Revision to use (2b by default)
  • seed: Salt (without revision data). Generated if not provided. Must be 16 chars long.

Returns

Complete salt