Keats / rust-bcrypt

Easily hash and verify passwords using Bcrypt
MIT License
333 stars 47 forks source link

bcrypt

Safety Dance Build Status Documentation

Installation

Add the following to Cargo.toml:

bcrypt = "0.15"

The minimum Rust version is 1.60.0.

Usage

The crate makes 3 things public: DEFAULT_COST, hash, verify.

extern crate bcrypt;

use bcrypt::{DEFAULT_COST, hash, verify};

let hashed = hash("hunter2", DEFAULT_COST)?;
let valid = verify("hunter2", &hashed)?;

The cost needs to be an integer between 4 and 31 (see benchmarks to have an idea of the speed for each), the DEFAULT_COST is 12.

no_std

bcrypt crate supports no_std platforms. When alloc feature is enabled, all crate functionality is available. When alloc is not enabled only the raw bcrypt() function is usable.

Benchmarks

Speed depends on the cost used: the highest the slowest. Here are some benchmarks on a 2019 Macbook Pro to give you some ideas on the cost/speed ratio. Note that I don't go above 14 as it takes too long.

test bench_cost_10      ... bench:  51,474,665 ns/iter (+/- 16,006,581)
test bench_cost_14      ... bench: 839,109,086 ns/iter (+/- 274,507,463)
test bench_cost_4       ... bench:     795,814 ns/iter (+/- 42,838)
test bench_cost_default ... bench: 195,344,338 ns/iter (+/- 8,329,675)

Acknowledgments

This gist for the hash splitting and the null termination.

Recommendations

While bcrypt works well as an algorithm, using something like Argon2 is recommended for new projects.

Changelog