Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Nov 29, 2023
1 parent b1d9fd7 commit 7ecd7a9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 50 deletions.
32 changes: 15 additions & 17 deletions crates/abcrypt/examples/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,24 @@
#![warn(clippy::cargo, clippy::nursery, clippy::pedantic)]

#[cfg(feature = "std")]
use anyhow::Context;
#[cfg(feature = "std")]
use clap::Parser;

#[cfg(feature = "std")]
#[derive(Debug, Parser)]
#[derive(Debug, clap::Parser)]
#[command(version, about)]
struct Opt {
/// File to decrypt.
#[arg(value_name("INFILE"))]
/// Input file.
#[arg(value_name("FILE"))]
input: std::path::PathBuf,

/// File to write the result to.
#[arg(value_name("OUTFILE"))]
output: std::path::PathBuf,
}

#[cfg(feature = "std")]
fn main() -> anyhow::Result<()> {
use std::fs;
use std::{
fs,
io::{self, Write},
};

use abcrypt::{Decryptor, Error};
use anyhow::Context;
use clap::Parser;
use dialoguer::{theme::ColorfulTheme, Password};

let opt = Opt::parse();
Expand All @@ -47,13 +43,15 @@ fn main() -> anyhow::Result<()> {
.context("could not read passphrase")?;
let cipher = match Decryptor::new(&ciphertext, passphrase) {
c @ Err(Error::InvalidHeaderMac(_)) => c.context("passphrase is incorrect"),
c => c.with_context(|| format!("the header in {} is invalid", opt.input.display())),
c => c.context("the header in the encrypted data is invalid"),
}?;
let plaintext = cipher
.decrypt_to_vec()
.with_context(|| format!("{} is corrupted", opt.input.display()))?;
fs::write(opt.output, plaintext)
.with_context(|| format!("could not write the result to {}", opt.input.display()))?;
.context("the encrypted data is corrupted")?;

io::stdout()
.write_all(&plaintext)
.context("could not write data to stdout")?;
Ok(())
}

Expand Down
33 changes: 15 additions & 18 deletions crates/abcrypt/examples/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@
#![warn(clippy::cargo, clippy::nursery, clippy::pedantic)]

#[cfg(feature = "std")]
use anyhow::Context;
#[cfg(feature = "std")]
use clap::Parser;

#[cfg(feature = "std")]
#[derive(Debug, Parser)]
#[derive(Debug, clap::Parser)]
#[command(version, about)]
struct Opt {
/// Set the memory size in KiB.
Expand All @@ -32,20 +27,21 @@ struct Opt {
#[arg(short, long, default_value("1"), value_name("NUM"))]
parallelism: u32,

/// File to encrypt.
#[arg(value_name("INFILE"))]
/// Input file.
#[arg(value_name("FILE"))]
input: std::path::PathBuf,

/// File to write the result to.
#[arg(value_name("OUTFILE"))]
output: std::path::PathBuf,
}

#[cfg(feature = "std")]
fn main() -> anyhow::Result<()> {
use std::fs;
use std::{
fs,
io::{self, Write},
};

use abcrypt::{argon2::Params, Encryptor};
use abcrypt::argon2::Params;
use anyhow::Context;
use clap::Parser;
use dialoguer::{theme::ColorfulTheme, Password};

let opt = Opt::parse();
Expand All @@ -59,10 +55,11 @@ fn main() -> anyhow::Result<()> {
.interact()
.context("could not read passphrase")?;
let params = Params::new(opt.memory_size, opt.iterations, opt.parallelism, None)?;
let cipher = Encryptor::with_params(&plaintext, passphrase, params)?;
let ciphertext = cipher.encrypt_to_vec();
fs::write(opt.output, ciphertext)
.with_context(|| format!("could not write the result to {}", opt.input.display()))?;
let ciphertext = abcrypt::encrypt_with_params(plaintext, passphrase, params)?;

io::stdout()
.write_all(&ciphertext)
.context("could not write data to stdout")?;
Ok(())
}

Expand Down
22 changes: 7 additions & 15 deletions crates/abcrypt/examples/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@
#![warn(clippy::cargo, clippy::nursery, clippy::pedantic)]

#[cfg(feature = "std")]
use anyhow::Context;
#[cfg(feature = "std")]
use clap::Parser;

#[cfg(feature = "std")]
#[derive(Debug, Parser)]
#[derive(Debug, clap::Parser)]
#[command(version, about)]
struct Opt {
/// File to print the Argon2 parameters.
/// Input file.
#[arg(value_name("FILE"))]
input: std::path::PathBuf,
}
Expand All @@ -30,20 +25,17 @@ fn main() -> anyhow::Result<()> {
use std::fs;

use abcrypt::Params;
use anyhow::Context;
use clap::Parser;

let opt = Opt::parse();

let contents = fs::read(&opt.input)
let ciphertext = fs::read(&opt.input)
.with_context(|| format!("could not read data from {}", opt.input.display()))?;

let params = Params::new(contents).with_context(|| {
format!(
"{} is not a valid Argon2 encrypted file",
opt.input.display()
)
})?;
let params = Params::new(ciphertext).context("data is not a valid abcrypt encrypted file")?;
println!(
"Parameters used: m = {}; t = {}; p = {};",
"Parameters used: m_cost = {}; t_cost = {}; p_cost = {};",
params.m_cost(),
params.t_cost(),
params.p_cost()
Expand Down

0 comments on commit 7ecd7a9

Please sign in to comment.