1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
//! 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 std::path::PathBuf;
use structopt::StructOpt;
/// Encrypt a chunk.
#[derive(Debug, StructOpt)]
pub struct EncryptChunk {
/// The name of the file containing the cleartext chunk.
#[structopt(parse(from_os_str))]
filename: PathBuf,
/// Name of file where to write the encrypted chunk.
#[structopt(parse(from_os_str))]
output: PathBuf,
/// Chunk metadata as JSON.
#[structopt()]
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, StructOpt)]
pub struct DecryptChunk {
/// Name of file containing encrypted chunk.
#[structopt(parse(from_os_str))]
filename: PathBuf,
/// Name of file where to write the cleartext chunk.
#[structopt(parse(from_os_str))]
output: PathBuf,
/// Chunk metadata as JSON.
#[structopt()]
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(())
}
}
|