Skip to content

Commit

Permalink
v1-11-chacha20poly1305
Browse files Browse the repository at this point in the history
  • Loading branch information
Firstero committed Apr 26, 2024
1 parent 288234c commit ee549b6
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fail_fast: false
exclude: '^/fixtures/*'
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
Expand All @@ -9,6 +10,7 @@ repos:
- id: check-symlinks
- id: check-yaml
- id: end-of-file-fixer
exclude: ^fixtures/
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/psf/black
Expand Down
83 changes: 83 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = "MIT"
anyhow = "1.0.82"
base64 = "0.22.0"
blake3 = "1.5.1"
chacha20poly1305 = "0.10.1"
clap = { version = "4.5.4", features = ["derive"] }
csv = "1.3.0"
ed25519 = "2.2.3"
Expand Down
2 changes: 1 addition & 1 deletion fixtures/blake3.key
Original file line number Diff line number Diff line change
@@ -1 +1 @@
xUSZqzRNnuCE%~FWTh?axY8LuE*Mb4D4
xUSZqzRNnuCE%~FWTh?axY8LuE*Mb4D4
2 changes: 1 addition & 1 deletion fixtures/blake3.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2PVnPNxWEbfdPuLMMmjbwBL5e6B1LFBD
2PVnPNxWEbfdPuLMMmjbwBL5e6B1LFBD
1 change: 1 addition & 0 deletions fixtures/chacha20poly1305.key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%XUw4H7JwLXQZ7RdCB5nhyGWp7eZCYvJ
1 change: 1 addition & 0 deletions fixtures/chacha20poly1305.nonce
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
~4Agy2Tvb8Ja
24 changes: 24 additions & 0 deletions src/cli/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pub enum TextSubCommand {
Verify(TextVerifyOpts),
#[command(name = "generate", about = "Generate random key.")]
Generate(TextKeyGenerateOpts),
#[command(name = "encrypt", about = "Encrypt text with public key.")]
Encrypt(TextEncryptOpts),
#[command(name = "decrypt", about = "Decrypt text with private key.")]
Decrypt(TextDecryptOpts),
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -48,6 +52,26 @@ pub struct TextVerifyOpts {
pub format: TextSignFormat,
}

#[derive(Debug, Parser)]
pub struct TextEncryptOpts {
#[arg(short, long, value_parser=parse_input_file, default_value="-", help = "input file path, or '-' for stdin")]
pub input: String,
#[arg(short, long, value_parser=parse_input_file, help = "key file path, or '-' for stdin")]
pub key: String,
#[arg(short, long, value_parser=parse_input_file, help = "key file path, or '-' for stdin")]
pub nonce: String,
}

#[derive(Debug, Parser)]
pub struct TextDecryptOpts {
#[arg(short, long, value_parser=parse_input_file, default_value="-", help = "input file path, or '-' for stdin")]
pub input: String,
#[arg(short, long, value_parser=parse_input_file, help = "key file path, or '-' for stdin")]
pub key: String,
#[arg(short, long, value_parser=parse_input_file, help = "key file path, or '-' for stdin")]
pub nonce: String,
}

#[derive(Debug, Clone, Copy)]
pub enum TextSignFormat {
Blake3,
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub use cli::{
TextSubCommand,
};
pub use process::{
process_b64decode, process_b64encode, process_csv, process_generate, process_genpass,
process_sign, process_verify,
process_b64decode, process_b64encode, process_csv, process_decrypt, process_encrypt,
process_generate, process_genpass, process_sign, process_verify,
};
pub use utils::{get_content, get_reader};
27 changes: 21 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::fs;

use anyhow::Result;
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, read::DecoderReader, Engine};
use clap::Parser;
use std::fs;
use zxcvbn::zxcvbn;

use rcli::{
get_content, get_reader, process_b64decode, process_b64encode, process_csv, process_generate,
process_genpass, process_sign, process_verify, Base64SubCommand, Opts, SubCommand,
TextSignFormat, TextSubCommand,
get_content, get_reader, process_b64decode, process_b64encode, process_csv, process_decrypt,
process_encrypt, process_generate, process_genpass, process_sign, process_verify,
Base64SubCommand, Opts, SubCommand, TextSignFormat, TextSubCommand,
};

// usage:
Expand Down Expand Up @@ -83,6 +82,22 @@ fn main() -> Result<()> {
}
}
}
TextSubCommand::Encrypt(opts) => {
let mut reader = get_reader(&opts.input)?;
let key = get_content(&opts.key)?;
let nonce = get_content(&opts.nonce)?;
let encrypted = process_encrypt(&mut reader, &key, &nonce)?;
println!("{}", URL_SAFE_NO_PAD.encode(encrypted));
}
TextSubCommand::Decrypt(opts) => {
let reader = get_reader(&opts.input)?;
let mut reader = DecoderReader::new(reader, &URL_SAFE_NO_PAD);
// 创建一个新的 reader,应用 URL_SAFE_NO_PAD 解码
let key = get_content(&opts.key)?;
let nonce = get_content(&opts.nonce)?;
let decrypted = process_decrypt(&mut reader, &key, &nonce)?;
println!("{}", String::from_utf8(decrypted)?);
}
},
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pub use base64_processor::process_decode as process_b64decode;
pub use base64_processor::process_encode as process_b64encode;
pub use csv_processor::process as process_csv;
pub use genpass_processor::process as process_genpass;
pub use text::{process_generate, process_sign, process_verify};
pub use text::{process_decrypt, process_encrypt, process_generate, process_sign, process_verify};
Loading

0 comments on commit ee549b6

Please sign in to comment.