Skip to content

Commit

Permalink
v2.0.0 - binary message encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
jo committed Mar 21, 2022
1 parent 1394e83 commit 263fc1a
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Changelog


## v2.0.0 - Binary default
Don't base64 encode encrypted messages by default.

**Breaking change:**
* `encrypt` and `encrypt-to` do not encode its output as base64 per default anymore
* `decrypt` and `decrypt-from` do not expect its inputs base64 encoded per default anymore

**Feature:**
* `encrypt`, `decrypt`, `encrypt-to` and `decrypt-from` now take an optional parameter `--base64` (or `-b`) to encode/decode message contents as base64


## v1.0.0
Initial release
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "wcb"
description = "WebCrypto compatible encryption CLI"
version = "1.0.0"
version = "2.0.0"
edition = "2021"
documentation = "https://docs.rs/webcryptobox/latest/wcb/"
homepage = "https://github.com/jo/wcb-rs"
Expand Down
16 changes: 16 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ pub enum Commands {
/// Output filename to write base64 encoded encrypted message to. If omitted, print to STDOUT
#[clap(short, long, parse(from_os_str), value_name = "FILENAME")]
output_filename: Option<PathBuf>,

/// Base64 encode encrypted message
#[clap(short, long)]
base64: bool
},
/// Decrypt message
Decrypt {
Expand All @@ -201,6 +205,10 @@ pub enum Commands {
/// Output filename to write decrypted message to. If omitted, print to STDOUT
#[clap(short, long, parse(from_os_str), value_name = "FILENAME")]
output_filename: Option<PathBuf>,

/// Base64 decode encrypted message
#[clap(short, long)]
base64: bool
},
/// Encrypt message with key pair
EncryptTo {
Expand All @@ -224,6 +232,10 @@ pub enum Commands {
/// Output filename to write base64 encoded encrypted message to. If omitted, print to STDOUT
#[clap(short, long, parse(from_os_str), value_name = "FILENAME")]
output_filename: Option<PathBuf>,

/// Base64 encode encrypted message
#[clap(short, long)]
base64: bool
},
/// Decrypt message with key pair
DecryptFrom {
Expand All @@ -247,5 +259,9 @@ pub enum Commands {
/// Output filename to write decrypted message to. If omitted, print to STDOUT
#[clap(short, long, parse(from_os_str), value_name = "FILENAME")]
output_filename: Option<PathBuf>,

/// Base64 decode encrypted message
#[clap(short, long)]
base64: bool
},
}
41 changes: 25 additions & 16 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::cli;

use webcryptobox::*;

use std::io::{Error, Read};
use std::io::{Error, Read, Write};
use std::path::PathBuf;
use std::{fs, io};

Expand Down Expand Up @@ -45,7 +45,7 @@ fn read_base64_file_or_stdin(filename: &Option<PathBuf>) -> Vec<u8> {
fn write_file_or_stdout(filename: &Option<PathBuf>, data: &Vec<u8>) {
match &filename {
Some(path) => fs::write(path, data).expect("Unable to write file"),
None => println!("{}", std::str::from_utf8(data).unwrap()),
None => io::stdout().write_all(data).expect("Unable to write to stdout")
}
}

Expand Down Expand Up @@ -239,21 +239,30 @@ impl Wcb {
key,
filename,
output_filename,
base64,
} => {
let key = read_hex(&key);
let data = read_file_or_stdin(&filename);

let encrypted_data = encrypt(&key, &data).unwrap();

write_base64_file_or_stdout(&output_filename, &encrypted_data)
if *base64 {
write_base64_file_or_stdout(&output_filename, &encrypted_data)
} else {
write_file_or_stdout(&output_filename, &encrypted_data)
}
}
cli::Commands::Decrypt {
key,
filename,
output_filename,
base64,
} => {
let key = read_hex(&key);
let data = read_base64_file_or_stdin(&filename);
let data = match base64 {
true => read_base64_file_or_stdin(&filename),
false => read_file_or_stdin(&filename)
};

let decrypted_data = decrypt(&key, &data).unwrap();

Expand All @@ -264,6 +273,7 @@ impl Wcb {
public_key_filename,
filename,
output_filename,
base64,
} => {
let private_key_pem = read_file(&private_key_filename);
let private_key = import_private_key_pem(&private_key_pem).unwrap();
Expand All @@ -275,35 +285,34 @@ impl Wcb {

let encrypted_data = derive_and_encrypt(private_key, public_key, &data).unwrap();

write_base64_file_or_stdout(&output_filename, &encrypted_data)
if *base64 {
write_base64_file_or_stdout(&output_filename, &encrypted_data)
} else {
write_file_or_stdout(&output_filename, &encrypted_data)
}
}
cli::Commands::DecryptFrom {
private_key_filename,
public_key_filename,
filename,
output_filename,
base64,
} => {
let private_key_pem = read_file(&private_key_filename);
let private_key = import_private_key_pem(&private_key_pem).unwrap();

let public_key_pem = read_file(&public_key_filename);
let public_key = import_public_key_pem(&public_key_pem).unwrap();

let data = read_base64_file_or_stdin(&filename);
let data = match base64 {
true => read_base64_file_or_stdin(&filename),
false => read_file_or_stdin(&filename)
};

let decrypted_data = derive_and_decrypt(private_key, public_key, &data).unwrap();

write_file_or_stdout(&output_filename, &decrypted_data)
} // TODO
// encrypt-private-key <PASSWORD> [FILENAME]
// - Encrypt private key with password. Key either read from FILENAME or STDIN.
// decrypt-private-key <PASSWORD> [FILENAME]
// - Decrypt private key with password. Key either read from FILENAME or STDIN.
//
// encrypt-private-key-to <PRIVATE_KEY> <PUBLIC_KEY> [FILENAME]
// - Encrypt private key with private and public key. Private key either read from FILENAME or STDIN.
// decrypt-private-key-from <PRIVATE_KEY> <PUBLIC_KEY> [FILENAME]
// - Decrypt private key with private and public key. Private key either read from FILENAME or STDIN.
}
}

Ok(())
Expand Down

0 comments on commit 263fc1a

Please sign in to comment.