Skip to content

Commit

Permalink
Restore missing features
Browse files Browse the repository at this point in the history
  • Loading branch information
lbeder committed Nov 16, 2024
1 parent a25c3c6 commit 9abd01d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 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.

24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
authors = ["Leonid Beder <[email protected]>"]
edition = "2021"
name = "slowkey"
version = "1.4.1"
version = "1.5.3"

[dependencies]
base64 = "0.21.7"
better-panic = "0.3.0"
bs58 = "0.5.1"
chacha20poly1305 = "0.10.1"
chrono = "0.4.38"
clap = { version = "4.5.21", features = ["derive", "string"] }
color-backtrace = "0.6.1"
crossterm = "0.27.0"
dialoguer = "0.11.0"
glob = "0.3.1"
hex = "0.4.3"
humantime = "2.1.0"
mimalloc = { version = "0.1.43", default-features = false }
sha2 = "0.10.8"
sha3 = "0.10.8"
indicatif = "0.17.9"
lazy_static = "1.5.0"
base64 = "0.21.7"
bs58 = "0.5.1"
libsodium-sys-stable = { version = "1.22.1", features = ["optimized"] }
dialoguer = "0.11.0"
mimalloc = { version = "0.1.43", default-features = false }
scrypt = "0.11.0"
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.132"
chacha20poly1305 = "0.10.1"
glob = "0.3.1"
indicatif = "0.17.9"
chrono = "0.4.38"
scrypt = "0.11.0"
sha2 = "0.10.8"
sha3 = "0.10.8"

[dev-dependencies]
rstest = "0.18.2"
Expand Down
31 changes: 17 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ fn main() {
let salt = get_salt();
let password = get_password();

let mut prev_data = Vec::new();

if let Some(checkpoint_data) = restore_from_checkpoint_data {
println!("Verifying the checkpoint...\n");

Expand All @@ -525,16 +527,18 @@ fn main() {
} else {
println!("{}: Unable to verify the first checkpoint\n", "Warning".dark_yellow());
}

// Since we are starting from this checkpoint, set the rolling previous data to its data
prev_data = checkpoint_data.data.data;
}

let mb = MultiProgress::new();

let pb = mb
.add(ProgressBar::new(slowkey_opts.iterations as u64))
.add(ProgressBar::new((slowkey_opts.iterations - offset) as u64))
.with_style(
ProgressStyle::with_template("{bar:80.cyan/blue} {pos:>7}/{len:7} {percent}% ({eta})").unwrap(),
)
.with_position(offset as u64);
);

pb.enable_steady_tick(Duration::from_secs(1));

Expand All @@ -543,10 +547,9 @@ fn main() {
if checkpoint.is_some() && checkpointing_interval != 0 {
cpb = Some(
mb.add(ProgressBar::new(
(slowkey_opts.iterations / checkpointing_interval) as u64,
((slowkey_opts.iterations - offset) / checkpointing_interval) as u64,
))
.with_style(ProgressStyle::with_template("{msg}").unwrap())
.with_position((offset / checkpointing_interval) as u64),
.with_style(ProgressStyle::with_template("{msg}").unwrap()),
);

if let Some(ref mut cpb) = &mut cpb {
Expand All @@ -557,8 +560,9 @@ fn main() {
let start_time = SystemTime::now();
let running_time = Instant::now();
let slowkey = SlowKey::new(&slowkey_opts);
let prev_data = Arc::new(Mutex::new(Vec::new()));
let prev_data_thread = Arc::clone(&prev_data);

let prev_data_mutex = Arc::new(Mutex::new(prev_data));
let prev_data_thread = Arc::clone(&prev_data_mutex);

let handle = thread::spawn(move || {
let key = slowkey.derive_key_with_callback(
Expand All @@ -574,22 +578,21 @@ fn main() {
let prev_data: Option<&[u8]> = if current_iteration == 0 { None } else { Some(&prev_data) };

if let Some(checkpoint) = &mut checkpoint {
checkpoint.create_checkpoint(&salt, current_iteration, current_data, prev_data);
checkpoint.create_checkpoint(current_iteration, current_data, prev_data);
}

if let Some(ref mut cpb) = &mut cpb {
let hash =
Checkpoint::hash_checkpoint(&salt, current_iteration, current_data, prev_data);
let hash = Checkpoint::hash_checkpoint(current_iteration, current_data, prev_data);

cpb.set_message(format!(
"\nCreated checkpoint #{} with data hash (salted) {}",
"\nCreated checkpoint #{} with data hash {}",
(current_iteration + 1).to_string().cyan(),
format!("0x{}", hex::encode(hash)).cyan()
));
}
}

// Store the current data in order to store it in the checkpoint for future verification of the
// Store the current data in order to store it in the checkpoint for future verification of the
// parameters
if current_iteration < iterations - 1 {
prev_data.clone_from(current_data);
Expand Down Expand Up @@ -634,7 +637,7 @@ fn main() {
println!();

if let Some(out) = out {
let prev_data_guard = prev_data.lock().unwrap();
let prev_data_guard = prev_data_mutex.lock().unwrap();
let prev_data_option: Option<&[u8]> = if prev_data_guard.is_empty() {
None
} else {
Expand Down
15 changes: 7 additions & 8 deletions src/utils/checkpoints/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ impl Checkpoint {
}
}

pub fn create_checkpoint(&mut self, salt: &[u8], iteration: usize, data: &[u8], prev_data: Option<&[u8]>) {
let hash = Self::hash_checkpoint(salt, iteration, data, prev_data);
pub fn create_checkpoint(&mut self, iteration: usize, data: &[u8], prev_data: Option<&[u8]>) {
let hash = Self::hash_checkpoint(iteration, data, prev_data);
let padding = self.checkpoint_extension_padding;
let checkpoint_path = Path::new(&self.dir)
.join(Self::CHECKPOINT_PREFIX)
Expand All @@ -248,17 +248,16 @@ impl Checkpoint {
}
}

pub fn hash_checkpoint(salt: &[u8], iteration: usize, data: &[u8], prev_data: Option<&[u8]>) -> Vec<u8> {
let mut salted_data = data.to_vec();
salted_data.extend_from_slice(salt);
salted_data.extend_from_slice(&iteration.to_be_bytes());
pub fn hash_checkpoint(iteration: usize, data: &[u8], prev_data: Option<&[u8]>) -> Vec<u8> {
let mut hash_data = data.to_vec();
hash_data.extend_from_slice(&iteration.to_be_bytes());

if let Some(prev_data) = prev_data {
salted_data.extend_from_slice(prev_data);
hash_data.extend_from_slice(prev_data);
}

let mut sha256 = Sha256::new();
sha256.update(salted_data);
sha256.update(hash_data);
sha256.finalize().to_vec()
}

Expand Down

0 comments on commit 9abd01d

Please sign in to comment.