diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-06-04 06:35:14 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-06-04 06:35:14 +0000 |
commit | f2a274ee1291531c1154176bca5b9a47e9c234bd (patch) | |
tree | 6721b515739c6e5f9236ffd6f4ecc2dfecc471d2 /src/cmd/chunk.rs | |
parent | cb33088dbedf4b772013f83b8226047cc4355dd2 (diff) | |
parent | 9c2590d2428f0d3de882686ec2ec5832e7123c62 (diff) | |
download | obnam2-f2a274ee1291531c1154176bca5b9a47e9c234bd.tar.gz |
Merge branch 'aead' into 'main'
add encryption of individual chunks
Closes #110
See merge request larswirzenius/obnam!146
Diffstat (limited to 'src/cmd/chunk.rs')
-rw-r--r-- | src/cmd/chunk.rs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/cmd/chunk.rs b/src/cmd/chunk.rs new file mode 100644 index 0000000..e0e91b1 --- /dev/null +++ b/src/cmd/chunk.rs @@ -0,0 +1,64 @@ +use crate::chunk::DataChunk; +use crate::chunkmeta::ChunkMeta; +use crate::cipher::CipherEngine; +use crate::config::ClientConfig; +use crate::error::ObnamError; +use std::path::PathBuf; +use structopt::StructOpt; + +#[derive(Debug, StructOpt)] +pub struct EncryptChunk { + #[structopt(parse(from_os_str))] + filename: PathBuf, + + #[structopt(parse(from_os_str))] + output: PathBuf, + + #[structopt()] + json: String, +} + +impl EncryptChunk { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let pass = config.passwords()?; + let cipher = CipherEngine::new(&pass); + + let meta = ChunkMeta::from_json(&self.json)?; + + let cleartext = std::fs::read(&self.filename)?; + let chunk = DataChunk::new(cleartext, meta); + let encrypted = cipher.encrypt_chunk(&chunk)?; + + std::fs::write(&self.output, encrypted.ciphertext())?; + + Ok(()) + } +} + +#[derive(Debug, StructOpt)] +pub struct DecryptChunk { + #[structopt(parse(from_os_str))] + filename: PathBuf, + + #[structopt(parse(from_os_str))] + output: PathBuf, + + #[structopt()] + json: String, +} + +impl DecryptChunk { + pub fn run(&self, config: &ClientConfig) -> Result<(), ObnamError> { + let pass = config.passwords()?; + let cipher = CipherEngine::new(&pass); + + let meta = ChunkMeta::from_json(&self.json)?; + + let encrypted = std::fs::read(&self.filename)?; + let chunk = cipher.decrypt_chunk(&encrypted, &meta.to_json_vec())?; + + std::fs::write(&self.output, chunk.data())?; + + Ok(()) + } +} |