Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeder committed Dec 10, 2024
1 parent 972fcd9 commit 4013137
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 38 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The SlowKey Key Derivation Scheme is defined as follows:
### Inputs

- `password`: User's password.
- `salt`: Unique salt for hashing. Please note that the salt must be `16` bytes long, therefore shorter/longer salts will be SHA512 hashed and then truncated to `16` bytes.
- `salt`: Unique salt for hashing. Please note that the salt must be `16` bytes long, therefore shorter/longer salts will be SHA512 hashed and then truncated into `16` bytes.
- `iterations`: Number of iterations the process should be repeated.

### Output
Expand Down Expand Up @@ -271,7 +271,7 @@ Total running time: 27s
Average iteration time: 2s 717ms
```
Please note that salt must be `16` bytes long, therefore shorter/longer salts will be SHA512 hashed and then truncated to `16` bytes:
Please note that salt must be `16` bytes long, therefore shorter/longer salts will be SHA512 hashed and then truncated into `16` bytes:
In order to hide the output from prying eyes, we set both the background and foreground colors of text to black in a terminal, so that text becomes "hidden" because it blends into the background. However, in some terminals, highlighting this text with the cursor won't reveal it because the highlight color itself might be configured in a way that doesn't provide sufficient contrast against the black text. This occurs because terminals use default color sets for text, background, and highlights, which can vary based on the terminal and its settings.
Expand All @@ -284,7 +284,7 @@ Despite the text being invisible, it's important to note that the text remains p
Salt is: s...t
Salt's size 4 is shorter than 16 and will be SHA512 hashed and then truncated to 16 bytes.
Salt's size 4 is shorter than 16 and will be SHA512 hashed and then truncated into 16 bytes.
Do you want to continue? [y/n]
```
Expand All @@ -293,7 +293,7 @@ Do you want to continue? [y/n]
Salt is: s...t
Salt's size 20 is longer than 16 and will be SHA512 hashed and then truncated to 16 bytes.
Salt's size 20 is longer than 16 and will be SHA512 hashed and then truncated into 16 bytes.
Do you want to continue? [y/n]
```
Expand All @@ -320,7 +320,7 @@ Each checkpoint, except for the one that coincides with the first iteration, als
Please exercise caution when using this feature. Resuming computation from a compromised checkpoint may undermine your expectations regarding the duration of the key stretching process.
Please note that encryption key must be `32` bytes long, therefore shorter/longer will be first SHA512 hashed and then truncated to `32` bytes:
Please note that encryption key must be `32` bytes long, therefore shorter/longer will be first SHA512 hashed and then truncated into `32` bytes:
For instance, to elaborate on the previous example, suppose we want to create a checkpoint every `5` iterations forcefully terminate the execution at the `8th` iteration:
Expand Down
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ enum Commands {

#[arg(
long,
default_value = SlowKeyOptions::default().ballon_hash.s_cost().to_string(),
default_value = SlowKeyOptions::default().balloon_hash.s_cost().to_string(),
help = format!("Balloon Hash space (memory) cost number of 1 KiB memory block (must be greater than {} and lesser than {})", BalloonHashOptions::MIN_S_COST, BalloonHashOptions::MAX_S_COST))]
balloon_s_cost: u32,

#[arg(
long,
default_value = SlowKeyOptions::default().ballon_hash.t_cost().to_string(),
default_value = SlowKeyOptions::default().balloon_hash.t_cost().to_string(),
help = format!("Balloon Hash number of iterations (must be greater than {} and lesser than {})", BalloonHashOptions::MIN_T_COST, BalloonHashOptions::MAX_T_COST))]
balloon_t_cost: u32,

Expand Down Expand Up @@ -332,7 +332,7 @@ fn get_salt() -> Vec<u8> {
_ => match salt_len.cmp(&SlowKey::SALT_SIZE) {
Ordering::Less => {
println!(
"\nSalt's length {} is shorter than {} and will be SHA512 hashed and then truncated to {} bytes.",
"\nSalt's length {} is shorter than {} and will be SHA512 hashed and then truncated into {} bytes.",
salt_len,
SlowKey::SALT_SIZE,
SlowKey::SALT_SIZE
Expand All @@ -356,7 +356,7 @@ fn get_salt() -> Vec<u8> {
},
Ordering::Greater => {
println!(
"\nSalt's length {} is longer than {} and will be SHA512 hashed and then truncated to {} bytes.",
"\nSalt's length {} is longer than {} and will be SHA512 hashed and then truncated into {} bytes.",
salt_len,
SlowKey::SALT_SIZE,
SlowKey::SALT_SIZE
Expand Down Expand Up @@ -433,7 +433,7 @@ fn get_output_key() -> Vec<u8> {
match key_len.cmp(&ChaCha20Poly1305::KEY_SIZE) {
Ordering::Less => {
println!(
"\nOutput encryption key's length {} is shorter than {} and will be SHA512 hashed and then truncated to {} bytes.",
"\nOutput encryption key's length {} is shorter than {} and will be SHA512 hashed and then truncated into {} bytes.",
key_len,
ChaCha20Poly1305::KEY_SIZE,
ChaCha20Poly1305::KEY_SIZE
Expand All @@ -457,7 +457,7 @@ fn get_output_key() -> Vec<u8> {
},
Ordering::Greater => {
println!(
"\nOutput encryption key's length {} is longer than {} and will be SHA512 hashed and then truncated to {} bytes.",
"\nOutput encryption key's length {} is longer than {} and will be SHA512 hashed and then truncated into {} bytes.",
key_len,
ChaCha20Poly1305::KEY_SIZE,
ChaCha20Poly1305::KEY_SIZE
Expand Down Expand Up @@ -856,7 +856,7 @@ fn derive(derive_options: DeriveOptions) {
.cyan()
);
println!(
"Average iteration time: {}\n",
"Average iteration time: {}",
format_duration(Duration::from_millis(
(running_time.elapsed().as_millis() as f64 / options.iterations as f64).round() as u64
))
Expand Down Expand Up @@ -966,7 +966,7 @@ fn main() {
length: opts.length,
scrypt: opts.scrypt,
argon2id: opts.argon2id,
ballon_hash: opts.balloon_hash,
balloon_hash: opts.balloon_hash,
};

derive(DeriveOptions {
Expand Down
36 changes: 18 additions & 18 deletions src/slowkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct SlowKeyOptions {
pub length: usize,
pub scrypt: ScryptOptions,
pub argon2id: Argon2idOptions,
pub ballon_hash: BalloonHashOptions,
pub balloon_hash: BalloonHashOptions,
}

impl SlowKeyOptions {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl SlowKeyOptions {
length,
scrypt: *scrypt,
argon2id: *argon2id,
ballon_hash: *balloon_hash,
balloon_hash: *balloon_hash,
}
}

Expand All @@ -85,8 +85,8 @@ impl SlowKeyOptions {
&self.argon2id.t_cost().to_string().cyan(),
"Balloon Hash".green(),
BalloonHash::HASH.cyan(),
&self.ballon_hash.s_cost().to_string().cyan(),
&self.ballon_hash.t_cost().to_string().cyan()
&self.balloon_hash.s_cost().to_string().cyan(),
&self.balloon_hash.t_cost().to_string().cyan()
);
}
}
Expand All @@ -98,7 +98,7 @@ impl Default for SlowKeyOptions {
length: Self::DEFAULT_KEY_SIZE,
scrypt: ScryptOptions::default(),
argon2id: Argon2idOptions::default(),
ballon_hash: BalloonHashOptions::default(),
balloon_hash: BalloonHashOptions::default(),
}
}
}
Expand All @@ -125,7 +125,7 @@ impl SlowKey<'_> {
length: opts.length,
scrypt: Scrypt::new(opts.length, &opts.scrypt),
argon2id: Argon2id::new(opts.length, &opts.argon2id),
balloon_hash: BalloonHash::new(opts.length, &opts.ballon_hash),
balloon_hash: BalloonHash::new(opts.length, &opts.balloon_hash),
}
}

Expand Down Expand Up @@ -335,8 +335,8 @@ impl SlowKey<'_> {
options.scrypt.p(),
options.argon2id.m_cost(),
options.argon2id.t_cost(),
options.ballon_hash.s_cost(),
options.ballon_hash.t_cost()
options.balloon_hash.s_cost(),
options.balloon_hash.t_cost()
),
),
&input,
Expand Down Expand Up @@ -365,79 +365,79 @@ mod tests {
length: SlowKeyOptions::MAX_KEY_SIZE,
scrypt: ScryptOptions::default(),
argon2id: Argon2idOptions::default(),
ballon_hash: BalloonHashOptions::default(),
balloon_hash: BalloonHashOptions::default(),
}, b"saltsaltsaltsalt", b"test", &Vec::new(), 0,
"2d819b3d8903a8037630d2a92f88f200fef9d847cd98c958e076526ce7766645aa8f7a2f7177f739c8b117ec23a51d3eaede566c3b3c46af700932bf7182c647")]
#[case(&SlowKeyOptions {
iterations: 10,
length: 32,
scrypt: ScryptOptions::new(1 << 12, 8, 1),
argon2id: Argon2idOptions::default(),
ballon_hash: BalloonHashOptions::default(),
balloon_hash: BalloonHashOptions::default(),
}, b"saltsaltsaltsalt", b"test", &Vec::new(), 0,
"7c586b43929ed4aa3d9bc98fc0de795df6240e3a7a356c67934c5e2d0557fe08")]
#[case(&SlowKeyOptions {
iterations: 10,
length: 32,
scrypt: ScryptOptions::default(),
argon2id: Argon2idOptions::new(1 << 4, 2),
ballon_hash: BalloonHashOptions::new(1 << 5, 3),
balloon_hash: BalloonHashOptions::new(1 << 5, 3),
}, b"saltsaltsaltsalt", b"test", &Vec::new(), 0,
"d1817a0f0ae560729c7ef40c56ddd186351552f437d1431e9db68395c5aca69d")]
#[case(&SlowKeyOptions {
iterations: 4,
length: SlowKeyOptions::MAX_KEY_SIZE,
scrypt: ScryptOptions::new(1 << 20, 8, 1),
argon2id: Argon2idOptions::default(),
ballon_hash: BalloonHashOptions::default(),
balloon_hash: BalloonHashOptions::default(),
}, b"saltsaltsaltsalt", b"test", &Vec::new(), 0,
"e222eb8b3fb65e4b79d9c46b49cfbade3d105822cd5c5546e51d2d47e70f49e579a9d3cbdab23d3f211c78ddc11da48843a7b433b736fc6b18cfa98dbd6aa28c")]
#[case(&SlowKeyOptions {
iterations: 4,
length: SlowKeyOptions::MAX_KEY_SIZE,
scrypt: ScryptOptions::new(1 << 15, 8, 1),
argon2id: Argon2idOptions::default(),
ballon_hash: BalloonHashOptions::default(),
balloon_hash: BalloonHashOptions::default(),
}, b"saltsaltsaltsalt", b"", &Vec::new(), 0,
"5b9c7f850cc65a18d8a327cacc57a8dc2279ff722461e5e4de14e0b49a6975d563a04363876095955e07db22a5b769f6bf1ed6849ea07138a04b9302b19963b3")]
#[case(&SlowKeyOptions {
iterations: 10,
length: SlowKeyOptions::MAX_KEY_SIZE,
scrypt: ScryptOptions::new(1 << 15, 8, 1),
argon2id: Argon2idOptions::default(),
ballon_hash: BalloonHashOptions::default(),
balloon_hash: BalloonHashOptions::default(),
}, b"saltsaltsaltsalt", b"test", &Vec::new(), 0,
"c8219c24a42cb713771ce19bb07c687875a99839965628c2ad4e4ba2bee66f4183b1a8f91f1da808cac74a5a5bd8c12c934a5f457513154978d6ca3d13d66d62")]
#[case(&SlowKeyOptions {
iterations: 10,
length: SlowKeyOptions::MAX_KEY_SIZE,
scrypt: ScryptOptions::new(1 << 15, 8, 1),
argon2id: Argon2idOptions::default(),
ballon_hash: BalloonHashOptions::default(),
balloon_hash: BalloonHashOptions::default(),
}, b"saltsaltsaltsal2", b"test", &Vec::new(), 0,
"eab33d73bcf48337d42605da85cd0cfc0c30e384d905d9634cbf1551530352c67209d656513039fab9de47f99a80a11471c9fd89490b676592b6fcca0eeba847")]
#[case(&SlowKeyOptions {
iterations: 10,
length: SlowKeyOptions::MAX_KEY_SIZE,
scrypt: ScryptOptions::new(1 << 15, 8, 1),
argon2id: Argon2idOptions::default(),
ballon_hash: BalloonHashOptions::default(),
balloon_hash: BalloonHashOptions::default(),
}, b"saltsaltsaltsalt", b"test2", &Vec::new(), 0,
"19987e685650347eb7229dbd339d57a88bb18bb901aae8c97441ac43ce17c17a888e593ead9690528235081b16764fbaaab6aa1e467199d37f9ea97886293905")]
#[case(&SlowKeyOptions {
iterations: 10,
length: 32,
scrypt: ScryptOptions::new(1 << 12, 8, 1),
argon2id: Argon2idOptions::default(),
ballon_hash: BalloonHashOptions::default(),
balloon_hash: BalloonHashOptions::default(),
}, b"saltsaltsaltsalt", b"test", &Vec::new(), 1,
"df88794d493027643559641176ee44acdd263a56e9144c3724926f350179ca95")]
#[case(&SlowKeyOptions {
iterations: 10,
length: SlowKeyOptions::MAX_KEY_SIZE,
scrypt: ScryptOptions::new(1 << 15, 8, 1),
argon2id: Argon2idOptions::default(),
ballon_hash: BalloonHashOptions::default(),
balloon_hash: BalloonHashOptions::default(),
}, b"saltsaltsaltsalt", b"test", &Vec::new(), 5,
"881460654568cc80a8b53de0d49aa3fd665cc27c830b751d7738d14f3e2baf246171d591e8e1b20ca7e5d01dc04d65148f8b2c65505cfb03c114044e2946fde0")]

Expand Down
10 changes: 4 additions & 6 deletions src/utils/checkpoints/checkpoint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::version::Version;
use crate::{
slowkey::{SlowKey, SlowKeyOptions},
utils::{
Expand All @@ -10,7 +11,6 @@ use crate::{
},
DisplayOptions,
};

use base64::{engine::general_purpose, Engine as _};
use crossterm::style::Stylize;
use glob::{glob_with, MatchOptions};
Expand All @@ -23,9 +23,7 @@ use std::{
path::{Path, PathBuf},
};

use super::version::Version;

#[derive(PartialEq, Debug, Clone)]
#[derive(PartialEq, Clone)]
pub struct CheckpointOptions {
pub iterations: usize,
pub dir: PathBuf,
Expand Down Expand Up @@ -58,7 +56,7 @@ impl From<SlowKeyOptions> for CheckpointSlowKeyOptions {
length: options.length,
scrypt: options.scrypt,
argon2id: options.argon2id,
balloon_hash: options.ballon_hash,
balloon_hash: options.balloon_hash,
}
}
}
Expand Down Expand Up @@ -87,7 +85,7 @@ impl CheckpointData {
length: opts.length,
scrypt: opts.scrypt,
argon2id: opts.argon2id,
ballon_hash: opts.balloon_hash,
balloon_hash: opts.balloon_hash,
};

let slowkey = SlowKey::new(&options);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/outputs/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl OutputData {
length: opts.length,
scrypt: opts.scrypt,
argon2id: opts.argon2id,
ballon_hash: opts.ballon_hash,
balloon_hash: opts.balloon_hash,
};

let slowkey = SlowKey::new(&options);
Expand Down

0 comments on commit 4013137

Please sign in to comment.