summaryrefslogtreecommitdiff
path: root/src/cmd/chunk.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/chunk.rs')
-rw-r--r--src/cmd/chunk.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/cmd/chunk.rs b/src/cmd/chunk.rs
new file mode 100644
index 0000000..293de20
--- /dev/null
+++ b/src/cmd/chunk.rs
@@ -0,0 +1,70 @@
+//! The `encrypt-chunk` and `decrypt-chunk` subcommands.
+
+use crate::chunk::DataChunk;
+use crate::chunkmeta::ChunkMeta;
+use crate::cipher::CipherEngine;
+use crate::config::ClientConfig;
+use crate::error::ObnamError;
+use clap::Parser;
+use std::path::PathBuf;
+
+/// Encrypt a chunk.
+#[derive(Debug, Parser)]
+pub struct EncryptChunk {
+ /// The name of the file containing the cleartext chunk.
+ filename: PathBuf,
+
+ /// Name of file where to write the encrypted chunk.
+ output: PathBuf,
+
+ /// Chunk metadata as JSON.
+ json: String,
+}
+
+impl EncryptChunk {
+ /// Run the command.
+ 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(())
+ }
+}
+
+/// Decrypt a chunk.
+#[derive(Debug, Parser)]
+pub struct DecryptChunk {
+ /// Name of file containing encrypted chunk.
+ filename: PathBuf,
+
+ /// Name of file where to write the cleartext chunk.
+ output: PathBuf,
+
+ /// Chunk metadata as JSON.
+ json: String,
+}
+
+impl DecryptChunk {
+ /// Run the command.
+ 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(())
+ }
+}