Struct firebase_scrypt::FirebaseScrypt
source · pub struct FirebaseScrypt { /* private fields */ }
Expand description
Struct to simplify the usage of hash generation and checking.
Holds the salt separator, signer key, round and memory cost to make the usage of the hash generation and checking function easier.
Example
use firebase_scrypt::FirebaseScrypt;
const SALT_SEPARATOR: &str = "Bw==";
const SIGNER_KEY: &str = "jxspr8Ki0RYycVU8zykbdLGjFQ3McFUH0uiiTvC8pVMXAn210wjLNmdZJzxUECKbm0QsEmYUSDzZvpjeJ9WmXA==";
const ROUNDS: u32 = 8;
const MEM_COST: u32 = 14;
let firebase_scrypt = FirebaseScrypt::new(SALT_SEPARATOR, SIGNER_KEY, ROUNDS, MEM_COST);
let password = "user1password";
let salt = "42xEC+ixf3L2lw==";
let password_hash ="lSrfV15cpx95/sZS2W9c9Kp6i/LVgQNDNC/qzrCnh1SAyZvqmZqAjTdn3aoItz+VHjoZilo78198JAdRuid5lQ==";
assert!(firebase_scrypt.verify_password(password, salt, password_hash).unwrap())
Implementations§
source§impl FirebaseScrypt
impl FirebaseScrypt
sourcepub fn new(
salt_separator: &str,
signer_key: &str,
rounds: u32,
mem_cost: u32
) -> Self
pub fn new( salt_separator: &str, signer_key: &str, rounds: u32, mem_cost: u32 ) -> Self
Create a new FirebaseScrypt
instance.
sourcepub fn verify_password(
&self,
password: &str,
salt: &str,
known_hash: &str
) -> Result<bool, GenerateHashError>
pub fn verify_password( &self, password: &str, salt: &str, known_hash: &str ) -> Result<bool, GenerateHashError>
Calls verify_password
with the data from the FirebaseScrypt
Example
# This test doesn't pass for (some?) reason. But the ``verify_password_with_simple_works`` test
# passes, so no idea.
use firebase_scrypt::FirebaseScrypt;
const SALT_SEPARATOR: &str = "Bw==";
const SIGNER_KEY: &str = "jxspr8Ki0RYycVU8zykbdLGjFQ3McFUH0uiiTvC8pVMXAn210wjLNmdZJzxUECKbm0QsEmYUSDzZvpjeJ9WmXA==";
const ROUNDS: u32 = 8;
const MEM_COST: u32 = 14;
let password: &str = "user1password";
let salt: &str = "42xEC+ixf3L2lw==";
let password_hash: &str = "lSrfV15cpx95/sZS2W9c9Kp6i/LVgQNDNC/qzrCnh1SAyZvqmZqAjTdn3aoItz+VHjoZilo78198JAdRuid5lQ==";
let firebase_scrypt = FirebaseScrypt::new(SALT_SEPARATOR, SIGNER_KEY, ROUNDS, MEM_COST);
let is_valid = firebase_scrypt.verify_password(
password,
password_hash,
salt,
).unwrap();
assert!(is_valid)
sourcepub fn verify_password_bool(
&self,
password: &str,
salt: &str,
known_hash: &str
) -> bool
pub fn verify_password_bool( &self, password: &str, salt: &str, known_hash: &str ) -> bool
Calls FirebaseScrypt::verify_password
but returns false also if an error occurs, which
is usually the best thing to do.
sourcepub fn generate_base64_hash(
&self,
password: &str,
salt: &str
) -> Result<String, GenerateHashError>
pub fn generate_base64_hash( &self, password: &str, salt: &str ) -> Result<String, GenerateHashError>
Generates a hash and returns its Base64 form, the same as the hashes from Firebase
Warning: Do not use this function to check if a given password is valid, because that could result in side-channel attacks.
Use the
FirebaseScrypt::verify_password
function instead.
Example
use firebase_scrypt::FirebaseScrypt;
const SALT_SEPARATOR: &str = "Bw==";
const SIGNER_KEY: &str = "jxspr8Ki0RYycVU8zykbdLGjFQ3McFUH0uiiTvC8pVMXAn210wjLNmdZJzxUECKbm0QsEmYUSDzZvpjeJ9WmXA==";
const ROUNDS: u32 = 8;
const MEM_COST: u32 = 14;
let password = "user1password";
let salt = "42xEC+ixf3L2lw==";
let firebase_scrypt = FirebaseScrypt::new(SALT_SEPARATOR, SIGNER_KEY, ROUNDS, MEM_COST);
firebase_scrypt.generate_base64_hash(password, salt).unwrap();
Trait Implementations§
source§impl Clone for FirebaseScrypt
impl Clone for FirebaseScrypt
source§fn clone(&self) -> FirebaseScrypt
fn clone(&self) -> FirebaseScrypt
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for FirebaseScrypt
impl Debug for FirebaseScrypt
source§impl Ord for FirebaseScrypt
impl Ord for FirebaseScrypt
source§fn cmp(&self, other: &FirebaseScrypt) -> Ordering
fn cmp(&self, other: &FirebaseScrypt) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl PartialEq<FirebaseScrypt> for FirebaseScrypt
impl PartialEq<FirebaseScrypt> for FirebaseScrypt
source§fn eq(&self, other: &FirebaseScrypt) -> bool
fn eq(&self, other: &FirebaseScrypt) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd<FirebaseScrypt> for FirebaseScrypt
impl PartialOrd<FirebaseScrypt> for FirebaseScrypt
source§fn partial_cmp(&self, other: &FirebaseScrypt) -> Option<Ordering>
fn partial_cmp(&self, other: &FirebaseScrypt) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl Eq for FirebaseScrypt
impl StructuralEq for FirebaseScrypt
impl StructuralPartialEq for FirebaseScrypt
Auto Trait Implementations§
impl RefUnwindSafe for FirebaseScrypt
impl Send for FirebaseScrypt
impl Sync for FirebaseScrypt
impl Unpin for FirebaseScrypt
impl UnwindSafe for FirebaseScrypt
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more