Getting started
saga_bcrypt is a bcrypt password-hashing library for saga. Under the hood it wraps Rust's bcrypt crate through a NIF, so you get a well-tested implementation with a Saga API.
Requirements
Because the library ships a NIF, building it compiles a small Rust crate. The first build needs both cargo (the Rust toolchain) and rebar3 on your PATH. After that, hashes are computed in native code with no runtime dependencies.
Install
Add saga_bcrypt to your project.toml:
[dependencies]
saga_bcrypt = { git = "https://github.com/dylantf/saga_bcrypt" }Then run saga install to fetch it. The Rust crate builds automatically the first time you compile your project.
Your first hash
The whole API is two verbs — hash and verify — plus a small Config for the work factor.
import Bcrypt (hasher, hash, verify)
# Hash a password. `hasher` is a Config with the default cost (12).
let stored = case hash "correct horse battery staple" hasher {
Ok h -> h # "$2b$12$..."
Err e -> panic $"hashing failed: {e}"
}
# Later, check a login attempt against the stored hash.
verify "correct horse battery staple" stored # Ok True
verify "wrong password" stored # Ok FalseBoth hash and verify return a Result type. hash gives you back a $2b$ modular-crypt string with the salt embedded, so the hash is the only thing you need to store; there is no separate salt to manage.
From here, see Hashing & verifying for the work-factor knob and how verification behaves, and Error handling for the failure cases.