Skip to content

Commit

Permalink
Add salt/password/encryption key hints
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeder committed Nov 20, 2024
1 parent 747d0bc commit c3cff26
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 14 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ SlowKey Parameters:
✔ Enter your salt · ********
Salt is: s...t
✔ Enter your password · ********
Password is: p...d
████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 1/10 10% (54s)
```
Expand All @@ -204,8 +208,12 @@ Final result:
```sh
✔ Enter your salt · ********
Salt is: s...t
✔ Enter your password · ********
Password is: p...d
████████████████████████████████████████████████████████████████████████████████ 10/10 100% (0s)
Key is (please highlight to see): 0xad9aa031287b42f45c40a5caf3b3ed47f795d9315d22ab50a25652b3f2a6b716
Expand All @@ -226,13 +234,17 @@ Despite the text being invisible, it's important to note that the text remains p
```sh
✔ Enter your salt · ********
Salt is: s...t
Salt's size 4 is shorter than 16 and will be SHA512 hashed and then truncated to 16 bytes.
Do you want to continue? [y/n]
```
```sh
✔ Enter your salt · ********
Salt is: s...t
Salt's size 20 is longer than 16 and will be SHA512 hashed and then truncated to 16 bytes.
Do you want to continue? [y/n]
```
Expand Down Expand Up @@ -281,8 +293,12 @@ SlowKey Parameters:
✔ Enter your salt · ********
Salt is: s...t
✔ Enter your password · ********
Password is: p...d
████████████████████████████████████████████████████████████████░░░░░░░░░░░░░░░░ 5/10 80% (10s)
Created checkpoint #5 with data hash 0x3c0c7ab8bb2001c1efd67ce049a437c760cf95d4cc2967160b708fb7216d74d1
Expand Down Expand Up @@ -333,8 +349,12 @@ SlowKey Parameters:
✔ Enter your salt · ********
Salt is: s...t
✔ Enter your password · ********
Password is: p...d
Verifying the checkpoint...
The password, salt and internal data are correct
Expand Down Expand Up @@ -364,8 +384,12 @@ SlowKey Parameters:
✔ Enter your salt · ********
Salt is: s...t
✔ Enter your password · ********
Password is: p...d
Verifying the checkpoint...
The password, salt and internal data are correct
Expand Down Expand Up @@ -409,8 +433,12 @@ SlowKey Parameters:
✔ Enter your salt · ********
Salt is: s...t
✔ Enter your password · ********
Password is: p...d
Verifying the checkpoint...
The password, salt and internal data are correct
Expand Down Expand Up @@ -447,8 +475,12 @@ SlowKey Parameters:
✔ Enter your salt · ********
Salt is: s...t
✔ Enter your password · ********
Password is: p...d
████████████████████████████████████████████████████████████████████████████████ 10/10 100% (0s)
Key is (please highlight to see): 0xad9aa031287b42f45c40a5caf3b3ed47f795d9315d22ab50a25652b3f2a6b716
Expand All @@ -471,8 +503,12 @@ Please input all data either in raw or hex format starting with the 0x prefix
✔ Enter your salt · ********
Salt is: s...t
✔ Enter your password · ********
Password is: p...d
SlowKey Parameters:
Iterations: 10
Length: 32
Expand Down Expand Up @@ -542,8 +578,12 @@ SlowKey Parameters:
✔ Enter your salt · ********
Salt is: s...t
✔ Enter your password · ********
Password is: p...d
Verifying the output...
The password, salt and internal data are correct
Expand Down
59 changes: 45 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ enum Commands {
}

const HEX_PREFIX: &str = "0x";
const MIN_SECRET_LENGTH_TO_REVEAL: usize = 8;

#[derive(PartialEq, Debug, Clone, Default)]
pub struct DisplayOptions {
Expand All @@ -213,19 +214,23 @@ pub struct DisplayOptions {
}

fn get_salt() -> Vec<u8> {
let input = Password::with_theme(&ColorfulTheme::default())
let input_salt = Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter your salt")
.with_confirmation("Enter your salt again", "Error: salts don't match")
.allow_empty_password(true)
.interact()
.unwrap();

let mut salt = if input.starts_with(HEX_PREFIX) {
hex::decode(input.strip_prefix(HEX_PREFIX).unwrap()).unwrap()
let mut hex = false;
let mut salt = if input_salt.starts_with(HEX_PREFIX) {
hex = true;
hex::decode(input_salt.strip_prefix(HEX_PREFIX).unwrap()).unwrap()
} else {
input.as_bytes().to_vec()
input_salt.as_bytes().to_vec()
};

show_hint(&input_salt, "Salt", hex);

let salt_len = salt.len();
match salt_len {
0 => {
Expand Down Expand Up @@ -305,23 +310,29 @@ fn get_salt() -> Vec<u8> {
}

fn get_password() -> Vec<u8> {
let password = Password::with_theme(&ColorfulTheme::default())
let input_password = Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter your password")
.with_confirmation("Enter your password again", "Error: passwords don't match")
.interact()
.unwrap();

let mut hex = false;
let password = if input_password.starts_with(HEX_PREFIX) {
hex = true;
hex::decode(input_password.strip_prefix(HEX_PREFIX).unwrap()).unwrap()
} else {
input_password.as_bytes().to_vec()
};

show_hint(&input_password, "Password", hex);

println!();

if password.starts_with(HEX_PREFIX) {
hex::decode(password.strip_prefix(HEX_PREFIX).unwrap()).unwrap()
} else {
password.as_bytes().to_vec()
}
password
}

fn get_output_key() -> Vec<u8> {
let key = Password::with_theme(&ColorfulTheme::default())
let input = Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter your checkpoint/output encryption key")
.with_confirmation(
"Enter your checkpoint/output encryption key again",
Expand All @@ -330,12 +341,16 @@ fn get_output_key() -> Vec<u8> {
.interact()
.unwrap();

let mut key = if key.starts_with(HEX_PREFIX) {
hex::decode(key.strip_prefix(HEX_PREFIX).unwrap()).unwrap()
let mut hex = false;
let mut key = if input.starts_with(HEX_PREFIX) {
hex = true;
hex::decode(input.strip_prefix(HEX_PREFIX).unwrap()).unwrap()
} else {
key.as_bytes().to_vec()
input.as_bytes().to_vec()
};

show_hint(&input, "Output encryption key", hex);

let key_len = key.len();
match key_len.cmp(&ChaCha20Poly1305::KEY_SIZE) {
Ordering::Less => {
Expand Down Expand Up @@ -394,6 +409,22 @@ fn get_output_key() -> Vec<u8> {
key
}

fn show_hint(data: &str, description: &str, hex: bool) {
let len = data.len();

if len < MIN_SECRET_LENGTH_TO_REVEAL {
println!(
"\n{}: {} is too short, therefore hints won't be shown",
"Warning".dark_yellow(),
description,
);
} else {
let prefix_len = if hex { 3 } else { 1 };

println!("\n{} is: {}...{}", description, &data[..prefix_len], &data[len - 1..]);
}
}

fn main() {
better_panic::install();
color_backtrace::install();
Expand Down

0 comments on commit c3cff26

Please sign in to comment.