Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
Supports input from stdin and output to a file.
  • Loading branch information
sorairolake committed Dec 1, 2023
1 parent 7199926 commit aa75d7d
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 20 deletions.
7 changes: 7 additions & 0 deletions crates/abcrypt/CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ All notable changes to this project will be documented in this file.
The format is based on https://keepachangelog.com/[Keep a Changelog], and this
project adheres to https://semver.org/[Semantic Versioning].

== {compare-url}/abcrypt-v0.2.7\...HEAD[Unreleased]

=== Changed

* Supports input from stdin and output to a file in the examples
({pull-request-url}/154[#154])

== {compare-url}/abcrypt-v0.2.6\...abcrypt-v0.2.7[0.2.7] - 2023-11-29

=== Changed
Expand Down
33 changes: 25 additions & 8 deletions crates/abcrypt/examples/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@
#[derive(Debug, clap::Parser)]
#[command(version, about)]
struct Opt {
/// Output the result to a file.
#[arg(short, long, value_name("FILE"))]
output: Option<std::path::PathBuf>,

/// Input file.
///
/// If [FILE] is not specified, data will be read from stdin.
#[arg(value_name("FILE"))]
input: std::path::PathBuf,
input: Option<std::path::PathBuf>,
}

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

use abcrypt::{Decryptor, Error};
Expand All @@ -34,8 +40,15 @@ fn main() -> anyhow::Result<()> {

let opt = Opt::parse();

let ciphertext = fs::read(&opt.input)
.with_context(|| format!("could not read data from {}", opt.input.display()))?;
let ciphertext = if let Some(file) = opt.input {
fs::read(&file).with_context(|| format!("could not read data from {}", file.display()))
} else {
let mut buf = Vec::new();
io::stdin()
.read_to_end(&mut buf)
.context("could not read data from stdin")?;
Ok(buf)
}?;

let passphrase = Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter passphrase")
Expand All @@ -49,10 +62,14 @@ fn main() -> anyhow::Result<()> {
.decrypt_to_vec()
.context("the encrypted data is corrupted")?;

io::stdout()
.write_all(&plaintext)
.context("could not write data to stdout")?;
Ok(())
if let Some(file) = opt.output {
fs::write(&file, plaintext)
.with_context(|| format!("could not write data to {}", file.display()))
} else {
io::stdout()
.write_all(&plaintext)
.context("could not write data to stdout")
}
}

#[cfg(not(feature = "std"))]
Expand Down
33 changes: 25 additions & 8 deletions crates/abcrypt/examples/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#[derive(Debug, clap::Parser)]
#[command(version, about)]
struct Opt {
/// Output the result to a file.
#[arg(short, long, value_name("FILE"))]
output: Option<std::path::PathBuf>,

/// Set the memory size in KiB.
#[arg(short, long, default_value("19456"), value_name("NUM"))]
memory_size: u32,
Expand All @@ -28,15 +32,17 @@ struct Opt {
parallelism: u32,

/// Input file.
///
/// If [FILE] is not specified, data will be read from stdin.
#[arg(value_name("FILE"))]
input: std::path::PathBuf,
input: Option<std::path::PathBuf>,
}

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

use abcrypt::argon2::Params;
Expand All @@ -46,8 +52,15 @@ fn main() -> anyhow::Result<()> {

let opt = Opt::parse();

let plaintext = fs::read(&opt.input)
.with_context(|| format!("could not read data from {}", opt.input.display()))?;
let plaintext = if let Some(file) = opt.input {
fs::read(&file).with_context(|| format!("could not read data from {}", file.display()))
} else {
let mut buf = Vec::new();
io::stdin()
.read_to_end(&mut buf)
.context("could not read data from stdin")?;
Ok(buf)
}?;

let passphrase = Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter passphrase")
Expand All @@ -57,10 +70,14 @@ fn main() -> anyhow::Result<()> {
let params = Params::new(opt.memory_size, opt.iterations, opt.parallelism, None)?;
let ciphertext = abcrypt::encrypt_with_params(plaintext, passphrase, params)?;

io::stdout()
.write_all(&ciphertext)
.context("could not write data to stdout")?;
Ok(())
if let Some(file) = opt.output {
fs::write(&file, ciphertext)
.with_context(|| format!("could not write data to {}", file.display()))
} else {
io::stdout()
.write_all(&ciphertext)
.context("could not write data to stdout")
}
}

#[cfg(not(feature = "std"))]
Expand Down
20 changes: 16 additions & 4 deletions crates/abcrypt/examples/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,34 @@
#[command(version, about)]
struct Opt {
/// Input file.
///
/// If [FILE] is not specified, data will be read from stdin.
#[arg(value_name("FILE"))]
input: std::path::PathBuf,
input: Option<std::path::PathBuf>,
}

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

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

let opt = Opt::parse();

let ciphertext = fs::read(&opt.input)
.with_context(|| format!("could not read data from {}", opt.input.display()))?;
let ciphertext = if let Some(file) = opt.input {
fs::read(&file).with_context(|| format!("could not read data from {}", file.display()))
} else {
let mut buf = Vec::new();
io::stdin()
.read_to_end(&mut buf)
.context("could not read data from stdin")?;
Ok(buf)
}?;

let params = Params::new(ciphertext).context("data is not a valid abcrypt encrypted file")?;
println!(
Expand Down

0 comments on commit aa75d7d

Please sign in to comment.