Skip to content

Commit

Permalink
Add fingerprint to output
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeder committed Dec 10, 2024
1 parent 58c5300 commit 972fcd9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 51 deletions.
71 changes: 30 additions & 41 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use dialoguer::{theme::ColorfulTheme, Confirm, Input, Password};
use humantime::format_duration;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use mimalloc::MiMalloc;
use sha2::{Digest, Sha256, Sha512};
use sha2::{Digest, Sha512};
use std::{
cmp::Ordering,
collections::VecDeque,
Expand All @@ -41,7 +41,10 @@ use utils::{
version::Version,
},
color_hash::color_hash,
outputs::output::{OpenOutputOptions, Output, OutputOptions},
outputs::{
fingerprint::Fingerprint,
output::{OpenOutputOptions, Output, OutputOptions},
},
};

#[global_allocator]
Expand Down Expand Up @@ -599,21 +602,6 @@ fn show_hint(data: &str, description: &str, hex: bool) {
}
}

fn print_fingerprint(options: &SlowKeyOptions, salt: &[u8], password: &[u8]) {
let mut data = serde_json::to_string(&options).unwrap().as_bytes().to_vec();
data.extend_from_slice(salt);
data.extend_from_slice(password);

let mut sha256 = Sha256::new();
sha256.update(data);
let hash = sha256.finalize();

println!(
"Fingerprint: {}\n",
hex::encode(&hash[0..8]).to_uppercase().with(color_hash(hash.as_ref()))
);
}

struct DeriveOptions {
options: SlowKeyOptions,
checkpoint_data: Option<CheckpointData>,
Expand Down Expand Up @@ -647,28 +635,6 @@ fn derive(derive_options: DeriveOptions) {

let mut checkpointing_interval: usize = 0;

if let Some(dir) = derive_options.checkpoint_dir {
checkpointing_interval = derive_options.checkpoint_interval.unwrap();

if output_key.is_none() {
output_key = Some(get_output_key());
}

checkpoint = Some(Checkpoint::new(&CheckpointOptions {
iterations: options.iterations,
dir: dir.to_owned(),
key: output_key.clone().unwrap(),
max_checkpoints_to_keep: derive_options.max_checkpoints_to_keep,
slowkey: options.clone(),
}));

println!(
"Checkpoint will be created every {} iterations and saved to the \"{}\" checkpoints directory\n",
checkpointing_interval.to_string().cyan(),
&dir.to_string_lossy().cyan()
);
}

if let Some(checkpoint_data) = &derive_options.checkpoint_data {
checkpoint_data.print(DisplayOptions::default());
}
Expand Down Expand Up @@ -699,7 +665,30 @@ fn derive(derive_options: DeriveOptions) {
}

// Print the colored hash fingerprint of the parameters
print_fingerprint(&options, &salt, &password);
let fingerprint = Fingerprint::from_data(&options, &salt, &password);
fingerprint.print();

if let Some(dir) = derive_options.checkpoint_dir {
checkpointing_interval = derive_options.checkpoint_interval.unwrap();

if output_key.is_none() {
output_key = Some(get_output_key());
}

checkpoint = Some(Checkpoint::new(&CheckpointOptions {
iterations: options.iterations,
dir: dir.to_owned(),
key: output_key.clone().unwrap(),
max_checkpoints_to_keep: derive_options.max_checkpoints_to_keep,
slowkey: options.clone(),
}));

println!(
"Checkpoint will be created every {} iterations and saved to the \"{}\" checkpoints directory\n",
checkpointing_interval.to_string().cyan(),
&dir.to_string_lossy().cyan()
);
}

let mb = MultiProgress::new();

Expand Down Expand Up @@ -841,7 +830,7 @@ fn derive(derive_options: DeriveOptions) {
Some(&prev_data_guard[..])
};

out.save(&key, prev_data_option);
out.save(&key, prev_data_option, &fingerprint);

println!("Saved encrypted output to \"{}\"\n", &out.path.to_str().unwrap().cyan(),);
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/color_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use byteorder::{BigEndian, ReadBytesExt};
use crossterm::style::Color;

pub fn color_hash(data: &[u8; 32]) -> Color {
let v = Cursor::new(data).read_u32::<BigEndian>().expect("Hash is too small") as usize;
let v = Cursor::new(data).read_u32::<BigEndian>().unwrap();

let b = (v & 0xFF) as u8;
let g = ((v >> 8) & 0xFF) as u8;
Expand Down
35 changes: 35 additions & 0 deletions src/utils/outputs/fingerprint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::color_hash;
use crossterm::style::Stylize;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::slowkey::SlowKeyOptions;

#[derive(Serialize, Deserialize, PartialEq, Clone)]
pub struct Fingerprint {
pub hash: [u8; 32],
}

impl Fingerprint {
pub fn from_data(options: &SlowKeyOptions, salt: &[u8], password: &[u8]) -> Self {
let mut data = serde_json::to_string(&options).unwrap().as_bytes().to_vec();
data.extend_from_slice(salt);
data.extend_from_slice(password);

let mut sha256 = Sha256::new();
sha256.update(data);

Self {
hash: sha256.finalize().into(),
}
}

pub fn print(&self) {
println!(
"Fingerprint: {}\n",
hex::encode(&self.hash[0..8])
.to_uppercase()
.with(color_hash(&self.hash))
);
}
}
1 change: 1 addition & 0 deletions src/utils/outputs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod fingerprint;
pub mod output;
pub mod version;
20 changes: 11 additions & 9 deletions src/utils/outputs/output.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use super::{fingerprint::Fingerprint, version::Version};
use crate::{
slowkey::{SlowKey, SlowKeyOptions},
utils::chacha20poly1305::{ChaCha20Poly1305, Nonce},
DisplayOptions,
};
use base64::{engine::general_purpose, Engine as _};
use crossterm::style::Stylize;
use serde::{Deserialize, Serialize};
Expand All @@ -7,14 +13,6 @@ use std::{
path::PathBuf,
};

use crate::{
slowkey::{SlowKey, SlowKeyOptions},
utils::chacha20poly1305::{ChaCha20Poly1305, Nonce},
DisplayOptions,
};

use super::version::Version;

#[derive(PartialEq, Debug, Clone)]
pub struct OutputOptions {
pub path: PathBuf,
Expand Down Expand Up @@ -102,13 +100,16 @@ impl OutputData {
if display.options {
self.data.slowkey.print();
}

self.data.fingerprint.print();
}
}

#[derive(Serialize, Deserialize)]
pub struct SlowKeyData {
pub data: Vec<u8>,
pub prev_data: Option<Vec<u8>>,
pub fingerprint: Fingerprint,
pub slowkey: SlowKeyOptions,
}

Expand Down Expand Up @@ -162,7 +163,7 @@ impl Output {
}
}

pub fn save(&self, data: &[u8], prev_data: Option<&[u8]>) {
pub fn save(&self, data: &[u8], prev_data: Option<&[u8]>, fingerprint: &Fingerprint) {
let file = File::create(&self.path).unwrap();
let mut writer = BufWriter::new(file);

Expand All @@ -171,6 +172,7 @@ impl Output {
data: SlowKeyData {
data: data.to_vec(),
prev_data: prev_data.map(|slice| slice.to_vec()),
fingerprint: fingerprint.clone(),
slowkey: self.slowkey.clone(),
},
};
Expand Down

0 comments on commit 972fcd9

Please sign in to comment.