Skip to content
FutureBlitz

    Install

    Import std.crypto.passwords

    std.crypto.passwords

    Argon2id and Bcrypt, for keeping passwords: Hash picks a new random salt and the parameters, and gives a String that has them with the hash, which is what to keep; Verify tells whether a password is the one a kept hash was made from. Both are slow on purpose, so guessing is too. Argon2id, over Argon2's reference code, is the one to choose; Bcrypt, over Solar Designer's crypt_blowfish, is for the hashes other systems keep.

    std/crypto/passwords.fb

    Types

    Argon2id struct

    Public Struct Argon2id

    Argon2id (RFC 9106), a hash that takes memory as well as time.

    std/crypto/passwords.fb, line 22

    • Public Static Method Hash(password: String): String static method #

      The PHC string of password's hash, "$argon2id$v=19$m=19456,t=2,p=1$$", with a new 16-byte salt and OWASP's parameters: 19 MiB of memory, 2 passes, and 1 lane.

    • @PanicsAtCaller Public Static Method Hash(password: String, memory: Int, iterations: Int, parallelism: Int): String static method #

      The same, with memory KiB, iterations passes, and parallelism lanes. Panics unless parallelism is from 1 to 16777215, memory is at least 8 times parallelism, and iterations is 1 or more.

    • Public Static Method Verify(password: String, hash: String): Bool static method #

      Whether password is the one hash, a PHC string from Hash, was made from. A hash that isn't an Argon2id PHC string is no password's.

    • @PanicsAtCaller Public Static Method Derive(password: UInt8[], salt: UInt8[], memory: Int, iterations: Int, parallelism: Int, length: Int, secret: UInt8[] = [], associated: UInt8[] = []): UInt8[] static method #

      length bytes of key from password and salt, with those costs, and with secret, a key that isn't kept with the hash, and associated data, which can both be empty. Panics as Hash does, or unless salt is 8 bytes or more and length 4 or more.

    Bcrypt struct

    Public Struct Bcrypt

    bcrypt, OpenBSD's password hash, 2 to the cost rounds of Blowfish's key setup.

    std/crypto/passwords.fb, line 66

    • @PanicsAtCaller Public Static Method Hash(password: String, cost: Int = 12): String Throws ArgumentError static method #

      password's hash, "$2b$$", with a new 16-byte salt, and a cost from 4 to 31, 12 unless it's given. bcrypt reads only a password's first 72 bytes, and none past a NUL, so a longer one, or one with a NUL, would share its hash with others; it throws an ArgumentError instead. Panics when cost is outside 4 to 31.

    • Public Static Method Verify(password: String, hash: String): Bool static method #

      Whether password is the one hash, of any of bcrypt's versions, $2a$, $2b$, or $2y$, was made from, reading its first 72 bytes, as bcrypt does. A password with a NUL, or a hash that isn't bcrypt's, is none, like crypt_blowfish's $2x$, which gives many passwords one hash.

    Report an issue with this page or with FutureBlitz on GitHub.