Skip to content

Commit

Permalink
Add base64 and base58 options to both show-output and show-checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeder committed Nov 11, 2024
1 parent d46bea1 commit bf295dd
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Leonid Beder <[email protected]>"]
edition = "2021"
name = "slowkey"
version = "1.3.0"
version = "1.4.0"

[dependencies]
better-panic = "0.3.0"
Expand Down
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ Usage: slowkey [COMMAND]

Commands:
derive Derive a key using using Scrypt, Argon2, SHA2, and SHA3
show-checkpoint Decrypt a checkpoint
show-output Decrypt an output file
show-checkpoint Decrypt and print a checkpoint
show-output Decrypt and print an output file
test Print test vectors

Options:
Expand All @@ -87,9 +87,9 @@ Options:
-l, --length <LENGTH>
Length of the derived result (must be greater than 10 and lesser than or equal to 128) [default: 32]
--base64
Output the result in Base64 (in addition to hex)
Show the result in Base64 (in addition to hex)
--base58
Output the result in Base58 (in addition to hex)
Show the result in Base58 (in addition to hex)
--output <OUTPUT>
Optional path for storing the encrypted output
--scrypt-n <SCRYPT_N>
Expand Down Expand Up @@ -237,6 +237,19 @@ Salt's size 20 is longer than 16 and will be SHA512 hashed and then truncated to
### Checkpoints
```sh
Decrypt and print a checkpoint
Usage: slowkey show-checkpoint [OPTIONS] --checkpoint <CHECKPOINT>
Options:
--checkpoint <CHECKPOINT> Path to an existing checkpoint
--verify Verify that the password and salt match the checkpoint
--base64 Show the result in Base64 (in addition to hex)
--base58 Show the result in Base58 (in addition to hex)
-h, --help Print help
```
The tool also supports the creation of periodic checkpoints, which are securely encrypted and stored on the disk. Each checkpoint captures all parameters and the output from the last iteration, enabling you to resume computation from a previously established checkpoint. Additionally, the tool allows for the retention of multiple checkpoints.
Please note that even if the last checkpoint is done at the final iteration (in the case that the number of iterations divides by the check-pointing interval), the checkpoint still won't have the actual output until you complete the recovery process.
Expand Down Expand Up @@ -477,6 +490,19 @@ Total running time: 52s
Let's use the `show-output` command to decrypt its contents:
```sh
Decrypt and print an output file
Usage: slowkey show-output [OPTIONS] --output <OUTPUT>
Options:
--output <OUTPUT> Path to an existing output
--verify Verify that the password and salt match the output
--base64 Show the result in Base64 (in addition to hex)
--base58 Show the result in Base58 (in addition to hex)
-h, --help Print help
```
> slowkey show-output --output ~/output.enc
```sh
Expand Down
64 changes: 55 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ enum Commands {
#[arg(
long,
action = clap::ArgAction::SetTrue,
help = "Output the result in Base64 (in addition to hex)"
help = "Show the result in Base64 (in addition to hex)"
)]
base64: bool,

#[arg(
long,
action = clap::ArgAction::SetTrue,
help = "Output the result in Base58 (in addition to hex)"
help = "Show the result in Base58 (in addition to hex)"
)]
base58: bool,

Expand Down Expand Up @@ -153,22 +153,50 @@ enum Commands {
max_checkpoints_to_keep: usize,
},

#[command(about = "Decrypt a checkpoint")]
#[command(about = "Decrypt and print a checkpoint")]
ShowCheckpoint {
#[arg(long, help = "Path to an existing checkpoint")]
checkpoint: PathBuf,

#[arg(long, help = "Verify that the password and salt match the checkpoint")]
verify: bool,

#[arg(
long,
action = clap::ArgAction::SetTrue,
help = "Show the result in Base64 (in addition to hex)"
)]
base64: bool,

#[arg(
long,
action = clap::ArgAction::SetTrue,
help = "Show the result in Base58 (in addition to hex)"
)]
base58: bool,
},

#[command(about = "Decrypt an output file")]
#[command(about = "Decrypt and print an output file")]
ShowOutput {
#[arg(long, help = "Path to an existing output")]
output: PathBuf,

#[arg(long, help = "Verify that the password and salt match the output")]
verify: bool,

#[arg(
long,
action = clap::ArgAction::SetTrue,
help = "Show the result in Base64 (in addition to hex)"
)]
base64: bool,

#[arg(
long,
action = clap::ArgAction::SetTrue,
help = "Show the result in Base58 (in addition to hex)"
)]
base58: bool,
},

#[command(about = "Print test vectors")]
Expand All @@ -177,6 +205,12 @@ enum Commands {

const HEX_PREFIX: &str = "0x";

#[derive(PartialEq, Debug, Clone, Default)]
pub struct DisplayOptions {
pub base64: bool,
pub base58: bool,
}

fn get_salt() -> Vec<u8> {
let input = Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter your salt")
Expand Down Expand Up @@ -393,7 +427,7 @@ fn main() {
);
}

println!("{}\n", &checkpoint_data);
checkpoint_data.print(DisplayOptions::default());

slowkey_opts = SlowKeyOptions {
iterations,
Expand Down Expand Up @@ -612,7 +646,12 @@ fn main() {
);
},

Some(Commands::ShowCheckpoint { checkpoint, verify }) => {
Some(Commands::ShowCheckpoint {
checkpoint,
verify,
base64,
base58,
}) => {
println!(
"Please input all data either in raw or hex format starting with the {} prefix\n",
HEX_PREFIX
Expand All @@ -625,7 +664,8 @@ fn main() {
path: checkpoint,
});

println!("{}\n", &checkpoint_data);
checkpoint_data.print(DisplayOptions { base64, base58 });

println!("{}\n", &checkpoint_data.data.slowkey);

if verify {
Expand All @@ -646,7 +686,12 @@ fn main() {
}
},

Some(Commands::ShowOutput { output, verify }) => {
Some(Commands::ShowOutput {
output,
verify,
base64,
base58,
}) => {
println!(
"Please input all data either in raw or hex format starting with the {} prefix\n",
HEX_PREFIX
Expand All @@ -659,7 +704,8 @@ fn main() {
path: output,
});

println!("{}\n", &output_data);
output_data.print(DisplayOptions { base64, base58 });

println!("{}\n", &output_data.data.slowkey);

if verify {
Expand Down
32 changes: 27 additions & 5 deletions src/utils/checkpoints/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ use crate::{
chacha20poly1305::{ChaCha20Poly1305, Nonce},
scrypt::ScryptOptions,
},
DisplayOptions,
};

use base64::{engine::general_purpose, Engine as _};
use crossterm::style::Stylize;
use glob::{glob_with, MatchOptions};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -112,16 +114,14 @@ impl CheckpointData {

key == self.data.data
}
}

impl Display for CheckpointData {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
pub fn print(&self, display: DisplayOptions) {
let prev_data = match &self.data.prev_data {
Some(data) => hex::encode(data),
None => "".to_string(),
};

let output = format!(
let mut output = format!(
"{}:\n {}: {}:\n {}: {}:\n {} (please highlight to see): {}\n {} (please highlight to see): {}",
"Checkpoint".yellow(),
"Version".green(),
Expand All @@ -134,7 +134,29 @@ impl Display for CheckpointData {
format!("0x{}", prev_data).black().on_black()
);

write!(f, "{}", output)
if display.base64 {
output = format!(
"{}\n {} (please highlight to see): {}\n {} (please highlight to see): {}",
output,
"Data (base64)".green(),
general_purpose::STANDARD.encode(&self.data.data).black().on_black(),
"Previous Iteration's Data (base64)".green(),
general_purpose::STANDARD.encode(&prev_data).black().on_black()
);
}

if display.base58 {
output = format!(
"{}\n {} (please highlight to see): {}\n {} (please highlight to see): {}",
output,
"Data (base58)".green(),
bs58::encode(&self.data.data).into_string().black().on_black(),
"Previous Iteration's Data (base58)".green(),
bs58::encode(&prev_data).into_string().black().on_black()
);
}

println!("{}\n", output);
}
}

Expand Down
33 changes: 27 additions & 6 deletions src/utils/outputs/output.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use base64::{engine::general_purpose, Engine as _};
use crossterm::style::Stylize;
use serde::{Deserialize, Serialize};
use std::{
fmt::{self, Display, Formatter},
fs::File,
io::{BufReader, BufWriter, Read, Write},
path::PathBuf,
Expand All @@ -10,6 +10,7 @@ use std::{
use crate::{
slowkey::{SlowKey, SlowKeyOptions},
utils::chacha20poly1305::{ChaCha20Poly1305, Nonce},
DisplayOptions,
};

use super::version::Version;
Expand Down Expand Up @@ -53,16 +54,14 @@ impl OutputData {

key == self.data.data
}
}

impl Display for OutputData {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
pub fn print(&self, display: DisplayOptions) {
let prev_data = match &self.data.prev_data {
Some(data) => hex::encode(data),
None => "".to_string(),
};

let output = format!(
let mut output = format!(
"{}:\n {}: {}\n {} (please highlight to see): {}\n {} (please highlight to see): {}",
"Output".yellow(),
"Iterations".green(),
Expand All @@ -73,7 +72,29 @@ impl Display for OutputData {
format!("0x{}", prev_data).black().on_black()
);

write!(f, "{}", output)
if display.base64 {
output = format!(
"{}\n {} (please highlight to see): {}\n {} (please highlight to see): {}",
output,
"Data (base64)".green(),
general_purpose::STANDARD.encode(&self.data.data).black().on_black(),
"Previous Iteration's Data (base64)".green(),
general_purpose::STANDARD.encode(&prev_data).black().on_black()
);
}

if display.base58 {
output = format!(
"{}\n {} (please highlight to see): {}\n {} (please highlight to see): {}",
output,
"Data (base58)".green(),
bs58::encode(&self.data.data).into_string().black().on_black(),
"Previous Iteration's Data (base58)".green(),
bs58::encode(&prev_data).into_string().black().on_black()
);
}

println!("{}\n", output);
}
}

Expand Down

0 comments on commit bf295dd

Please sign in to comment.