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 1e2f7a8 commit e03e551
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 20 deletions.
7 changes: 7 additions & 0 deletions 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}/v0.8.5\...HEAD[Unreleased]

=== Changed

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

== {compare-url}/v0.8.4\...v0.8.5[0.8.5] - 2023-11-29

=== Changed
Expand Down
33 changes: 25 additions & 8 deletions 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 anyhow::Context;
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 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 work parameter N to 2^<VALUE>.
#[arg(long, default_value("17"), value_name("VALUE"))]
log_n: u8,
Expand All @@ -28,15 +32,17 @@ struct Opt {
p: 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 anyhow::Context;
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.log_n, opt.r, opt.p, Params::RECOMMENDED_LEN)?;
let ciphertext = scryptenc::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 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 anyhow::Context;
use clap::Parser;
use scryptenc::Params;

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 scrypt encrypted file")?;
println!(
Expand Down

0 comments on commit e03e551

Please sign in to comment.