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

source

pub fn new( salt_separator: &str, signer_key: &str, rounds: u32, mem_cost: u32 ) -> Self

Create a new FirebaseScrypt instance.

source

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)
source

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.

source

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

source§

fn clone(&self) -> FirebaseScrypt

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for FirebaseScrypt

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Ord for FirebaseScrypt

source§

fn cmp(&self, other: &FirebaseScrypt) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<FirebaseScrypt> for FirebaseScrypt

source§

fn eq(&self, other: &FirebaseScrypt) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<FirebaseScrypt> for FirebaseScrypt

source§

fn partial_cmp(&self, other: &FirebaseScrypt) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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 more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for FirebaseScrypt

source§

impl StructuralEq for FirebaseScrypt

source§

impl StructuralPartialEq for FirebaseScrypt

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.