Module libreauth::key

source ·
Expand description

Key generation module

Cryptographic security

Many random generators are available, but not all of them are cryptographically secure. That is a problem because if a secret key may be predictable, the security of your system crumbles into pieces. This key generation module uses the getrandom crate which is an interface to the operating system’s preferred random number source.

Examples

Generate a random key and display it in several forms.

let key = libreauth::key::KeyBuilder::new().generate();
println!("Key: Vec<u8>: {:?}", key.as_vec());
println!("Key: hex String: {}", key.as_hex());
println!("Key: base 32 String: {}", key.as_base32());
println!("Key: base 64 String: {}", key.as_base64());
assert!(key.as_vec() == key.as_vec());
assert!(key.as_hex() == key.as_hex());
assert!(key.as_base32() == key.as_base32());
assert!(key.as_base64() == key.as_base64());

Generate two random key and test if they are different.

let k1 = libreauth::key::KeyBuilder::new().generate().as_vec();
let k2 = libreauth::key::KeyBuilder::new().generate().as_vec();
assert!(k1 != k2);

Structs

Functions