argon2-creds/examples/complex.rs

41 lines
1.1 KiB
Rust
Raw Normal View History

2023-10-14 17:08:33 +05:30
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2021-03-04 20:51:06 +05:30
/* To gain fine-grained control over how credentials are managed,
* consider using ConfigBuilder:
*/
2021-01-29 11:42:59 +05:30
2021-03-04 20:41:50 +05:30
use argon2_creds::{Config, ConfigBuilder, PasswordPolicyBuilder};
2021-01-29 11:42:59 +05:30
fn main() {
let config = ConfigBuilder::default()
.username_case_mapped(false)
.profanity(true)
.blacklist(false)
2021-03-04 20:41:50 +05:30
.password_policy(
PasswordPolicyBuilder::default()
.min(12)
.max(80)
.build()
.unwrap(),
)
2021-01-29 11:42:59 +05:30
.build()
.unwrap();
let password = "ironmansucks";
let hash = config.password(password).unwrap();
// email validation
2021-04-13 23:11:34 +05:30
config.email("batman@we.net").unwrap();
2021-01-29 11:42:59 +05:30
// process username
let username = config.username("Realaravinth").unwrap(); // process username
// generate hash
let hash = config.password(password).unwrap();
assert_eq!(username, "realaravinth");
2021-12-17 10:42:22 +05:30
assert!(Config::verify(&hash, password).unwrap(), "verify hashing");
2021-01-29 11:42:59 +05:30
}