Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

bcrypt

Compute a hash of a password using 2^rounds_log rounds of the bcrypt key stretching function. bcrypt is a computationally expensive and cache-hard function, explicitly designed to slow down exhaustive searches.

The function returns the hash as a dk_length byte array, that doesn't include anything besides the hash output.

This function was designed for password storage, not for key derivation. For key derivation, use bcrypt.pbkdf() or bcrypt.opensshKdf() instead.

bcrypt.bcrypt
pub fn bcrypt(
    password: []const u8,
    salt: *const [salt_length]u8,
    params: Params,
) [dk_length]u8

File

lib/std/crypto/bcrypt.zig:468

Code

pub fn bcrypt(
    password: []const u8,
    salt: *const [salt_length]u8,
    params: Params,
) [dk_length]u8 {
    if (password.len <= 72 or params.silently_truncate_password) {
        return bcryptWithTruncation(password, salt, params);
    }

    var pre_hash: [HmacSha512.mac_length]u8 = undefined;
    HmacSha512.create(&pre_hash, password, salt);

    const Encoder = crypt_format.Codec.Encoder;
    var pre_hash_b64: [Encoder.calcSize(pre_hash.len)]u8 = undefined;
    _ = Encoder.encode(&pre_hash_b64, &pre_hash);

    return bcryptWithTruncation(&pre_hash_b64, salt, params);
}