Skip to content

Commit 4d7ae4b

Browse files
committed
chore: fmt
1 parent db9e475 commit 4d7ae4b

File tree

3 files changed

+59
-56
lines changed

3 files changed

+59
-56
lines changed

src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl From<InvalidOutputLen> for DerivedKeyError {
2929

3030
#[derive(Clone, Debug)]
3131
pub(crate) enum EncryptError {
32-
StreamCipher(StreamCipherError)
32+
StreamCipher(StreamCipherError),
3333
}
3434

3535
impl From<StreamCipherError> for EncryptError {

src/lib.rs

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@
2626
//! assert!(firebase_scrypt.verify_password(password, salt, password_hash).unwrap())
2727
//! ```
2828
29-
use aes::{cipher::{KeyIvInit, StreamCipher}, Aes256};
29+
use crate::errors::{DerivedKeyError, EncryptError, GenerateHashError};
30+
use aes::{
31+
cipher::{KeyIvInit, StreamCipher},
32+
Aes256,
33+
};
3034
use constant_time_eq::constant_time_eq;
3135
use ctr::Ctr128BE;
3236
use scrypt::Params;
33-
use crate::errors::{DerivedKeyError, EncryptError, GenerateHashError};
3437

3538
pub mod errors;
3639
#[cfg(feature = "simple")]
@@ -64,12 +67,7 @@ fn generate_derived_key<'a>(
6467
let params = Params::new(log2_n as u8, rounds, p)?;
6568

6669
let mut result = [0u8; 64];
67-
scrypt::scrypt(
68-
password,
69-
salt.as_slice(),
70-
&params,
71-
&mut result
72-
)?;
70+
scrypt::scrypt(password, salt.as_slice(), &params, &mut result)?;
7371

7472
Ok(result)
7573
}
@@ -78,8 +76,7 @@ fn encrypt(signer_key: &[u8], key: [u8; 32]) -> Result<Vec<u8>, EncryptError> {
7876
let mut cipher = Ctr128BE::<Aes256>::new(&key.into(), &IV.into());
7977

8078
let mut buffer = vec![0u8; signer_key.len()];
81-
cipher
82-
.apply_keystream_b2b(signer_key, &mut buffer)?;
79+
cipher.apply_keystream_b2b(signer_key, &mut buffer)?;
8380

8481
Ok(buffer)
8582
}
@@ -123,24 +120,13 @@ pub fn verify_password(
123120
rounds: u32,
124121
mem_cost: u32,
125122
) -> Result<bool, GenerateHashError> {
126-
let password_hash = generate_raw_hash(
127-
password,
128-
salt,
129-
salt_separator,
130-
signer_key,
131-
rounds,
132-
mem_cost
133-
)?;
134-
135-
Ok(
136-
constant_time_eq(
137-
password_hash.as_slice(),
138-
base64::decode(
139-
clean(known_hash)
140-
)?
141-
.as_slice()
142-
)
143-
)
123+
let password_hash =
124+
generate_raw_hash(password, salt, salt_separator, signer_key, rounds, mem_cost)?;
125+
126+
Ok(constant_time_eq(
127+
password_hash.as_slice(),
128+
base64::decode(clean(known_hash))?.as_slice(),
129+
))
144130
}
145131

146132
/// Generates a hash in the form of a [`Vec<u8>`]
@@ -182,7 +168,8 @@ pub fn generate_raw_hash(
182168
rounds: u32,
183169
mem_cost: u32,
184170
) -> Result<Vec<u8>, GenerateHashError> {
185-
let derived_key = generate_derived_key(password, &clean(salt), salt_separator, rounds, mem_cost)?;
171+
let derived_key =
172+
generate_derived_key(password, &clean(salt), salt_separator, rounds, mem_cost)?;
186173
let signer_key = base64::decode(signer_key)?;
187174

188175
let result = encrypt(signer_key.as_slice(), derived_key[..32].try_into().unwrap())?;
@@ -192,13 +179,15 @@ pub fn generate_raw_hash(
192179
#[cfg(test)]
193180
mod tests {
194181
const SALT_SEPARATOR: &str = "Bw==";
195-
const SIGNER_KEY: &str = "jxspr8Ki0RYycVU8zykbdLGjFQ3McFUH0uiiTvC8pVMXAn210wjLNmdZJzxUECKbm0QsEmYUSDzZvpjeJ9WmXA==";
182+
const SIGNER_KEY: &str =
183+
"jxspr8Ki0RYycVU8zykbdLGjFQ3McFUH0uiiTvC8pVMXAn210wjLNmdZJzxUECKbm0QsEmYUSDzZvpjeJ9WmXA==";
196184
const ROUNDS: u32 = 8;
197185
const MEM_COST: u32 = 14;
198186

199187
const PASSWORD: &str = "user1password";
200188
const SALT: &str = "42xEC+ixf3L2lw==";
201-
const PASSWORD_HASH: &str ="lSrfV15cpx95/sZS2W9c9Kp6i/LVgQNDNC/qzrCnh1SAyZvqmZqAjTdn3aoItz+VHjoZilo78198JAdRuid5lQ==";
189+
const PASSWORD_HASH: &str =
190+
"lSrfV15cpx95/sZS2W9c9Kp6i/LVgQNDNC/qzrCnh1SAyZvqmZqAjTdn3aoItz+VHjoZilo78198JAdRuid5lQ==";
202191

203192
use super::*;
204193

@@ -212,27 +201,30 @@ mod tests {
212201
SIGNER_KEY,
213202
ROUNDS,
214203
MEM_COST
215-
).unwrap())
204+
)
205+
.unwrap())
216206
}
217207

218208
#[test]
219209
fn generate_hash_works() {
220-
assert_eq!(base64::encode(generate_raw_hash(
221-
PASSWORD,
222-
SALT,
223-
SALT_SEPARATOR,
224-
SIGNER_KEY,
225-
ROUNDS,
226-
MEM_COST,
227-
).unwrap()), PASSWORD_HASH)
210+
assert_eq!(
211+
base64::encode(
212+
generate_raw_hash(PASSWORD, SALT, SALT_SEPARATOR, SIGNER_KEY, ROUNDS, MEM_COST,)
213+
.unwrap()
214+
),
215+
PASSWORD_HASH
216+
)
228217
}
229218

230219
#[test]
231220
fn encrypt_works() {
232221
let param_1 = b"randomrandomrandomrandomrandomrandomrandom";
233222
let param_2 = b"12345678901234567890123456789012";
234223

235-
assert_eq!(hex::encode(encrypt(param_1, *param_2).unwrap()), "09f509fa3d09cde568f80709416681e4ed5d9677ca8b4807a932869ba3fd057be3606c2940877850ed96");
224+
assert_eq!(
225+
hex::encode(encrypt(param_1, *param_2).unwrap()),
226+
"09f509fa3d09cde568f80709416681e4ed5d9677ca8b4807a932869ba3fd057be3606c2940877850ed96"
227+
);
236228
}
237229

238230
#[test]

src/simple.rs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{verify_password, GenerateHashError, generate_raw_hash};
1+
use crate::{generate_raw_hash, verify_password, GenerateHashError};
22

33
/// Struct to simplify the usage of hash generation and checking.
44
///
@@ -68,7 +68,12 @@ impl FirebaseScrypt {
6868
///
6969
/// assert!(is_valid)
7070
/// ```
71-
pub fn verify_password(&self, password: &str, salt: &str, known_hash: &str) -> Result<bool, GenerateHashError> {
71+
pub fn verify_password(
72+
&self,
73+
password: &str,
74+
salt: &str,
75+
known_hash: &str,
76+
) -> Result<bool, GenerateHashError> {
7277
verify_password(
7378
password,
7479
known_hash,
@@ -117,7 +122,11 @@ impl FirebaseScrypt {
117122
///
118123
/// firebase_scrypt.generate_base64_hash(password, salt).unwrap();
119124
/// ```
120-
pub fn generate_base64_hash(&self, password: &str, salt: &str) -> Result<String, GenerateHashError> {
125+
pub fn generate_base64_hash(
126+
&self,
127+
password: &str,
128+
salt: &str,
129+
) -> Result<String, GenerateHashError> {
121130
let hash = generate_raw_hash(
122131
password,
123132
salt,
@@ -134,34 +143,36 @@ impl FirebaseScrypt {
134143
#[cfg(test)]
135144
mod tests {
136145
const SALT_SEPARATOR: &str = "Bw==";
137-
const SIGNER_KEY: &str = "jxspr8Ki0RYycVU8zykbdLGjFQ3McFUH0uiiTvC8pVMXAn210wjLNmdZJzxUECKbm0QsEmYUSDzZvpjeJ9WmXA==";
146+
const SIGNER_KEY: &str =
147+
"jxspr8Ki0RYycVU8zykbdLGjFQ3McFUH0uiiTvC8pVMXAn210wjLNmdZJzxUECKbm0QsEmYUSDzZvpjeJ9WmXA==";
138148
const ROUNDS: u32 = 8;
139149
const MEM_COST: u32 = 14;
140150

141151
const PASSWORD: &str = "user1password";
142152
const SALT: &str = "42xEC+ixf3L2lw==";
143-
const PASSWORD_HASH: &str ="lSrfV15cpx95/sZS2W9c9Kp6i/LVgQNDNC/qzrCnh1SAyZvqmZqAjTdn3aoItz+VHjoZilo78198JAdRuid5lQ==";
153+
const PASSWORD_HASH: &str =
154+
"lSrfV15cpx95/sZS2W9c9Kp6i/LVgQNDNC/qzrCnh1SAyZvqmZqAjTdn3aoItz+VHjoZilo78198JAdRuid5lQ==";
144155

145156
use super::*;
146157

147158
#[test]
148159
fn verify_password_with_simple_works() {
149160
let firebase_scrypt = FirebaseScrypt::new(SALT_SEPARATOR, SIGNER_KEY, ROUNDS, MEM_COST);
150161

151-
assert!(firebase_scrypt.verify_password(
152-
PASSWORD,
153-
SALT,
154-
PASSWORD_HASH,
155-
).unwrap())
162+
assert!(firebase_scrypt
163+
.verify_password(PASSWORD, SALT, PASSWORD_HASH,)
164+
.unwrap())
156165
}
157166

158167
#[test]
159168
fn generate_hash_with_simple_works() {
160169
let firebase_scrypt = FirebaseScrypt::new(SALT_SEPARATOR, SIGNER_KEY, ROUNDS, MEM_COST);
161170

162-
assert_eq!(firebase_scrypt.generate_base64_hash(
163-
PASSWORD,
164-
SALT,
165-
).unwrap(), PASSWORD_HASH)
171+
assert_eq!(
172+
firebase_scrypt
173+
.generate_base64_hash(PASSWORD, SALT,)
174+
.unwrap(),
175+
PASSWORD_HASH
176+
)
166177
}
167178
}

0 commit comments

Comments
 (0)