create_write

Function create_write 

Source
pub fn create_write<W: CryptoInnerWriter + Send + Sync + 'static>(
    writer: W,
    cipher: Cipher,
    key: &SecretVec<u8>,
) -> impl CryptoWrite<W>
Expand description

Creates an encrypted writer

Examples found in repository?
examples/crypto_speed.rs (line 89)
76fn stream_speed(
77    path_in: &str,
78    path_out: &str,
79    cipher: Cipher,
80    key: &Arc<SecretVec<u8>>,
81) -> Result<()> {
82    println!("stream speed");
83    let _ = fs::remove_file(path_out);
84    let mut file_in = File::open(path_in)?;
85    let file_out = File::create(path_out)?;
86    let path_out2 = Path::new(&path_out).to_path_buf().with_extension("dec");
87    let _ = fs::remove_file(path_out2.clone());
88    let mut file_out2 = File::create(path_out2.clone())?;
89    let mut writer = crypto::create_write(file_out, cipher, key);
90    let size = file_in.metadata()?.len();
91    let f = || crypto::create_read(File::open(path_out).unwrap(), cipher, key);
92    test_speed(&mut file_in, &mut writer, &mut file_out2, size, f)?;
93    file_in.seek(io::SeekFrom::Start(0))?;
94    check_hash(&mut file_in, &mut f())?;
95    fs::remove_file(path_out)?;
96    fs::remove_file(path_out2)?;
97    Ok(())
98}
99
100fn file_speed(path_in: &str, path_out: &str, cipher: Cipher, key: &SecretVec<u8>) -> Result<()> {
101    println!("file speed");
102    let _ = fs::remove_file(path_out);
103    let mut file_in = File::open(path_in)?;
104    let mut writer = crypto::create_write(File::create(Path::new(path_out))?, cipher, key);
105    let path_out2 = Path::new(&path_out).to_path_buf().with_extension("dec");
106    let _ = fs::remove_file(path_out2.clone());
107    let mut file_out2 = File::create(path_out2.clone())?;
108    let size = file_in.metadata()?.len();
109    let f = || crypto::create_read(File::open(path_out).unwrap(), cipher, key);
110    test_speed(&mut file_in, &mut writer, &mut file_out2, size, f)?;
111    file_in.seek(io::SeekFrom::Start(0)).unwrap();
112    check_hash(&mut file_in, &mut f())?;
113    fs::remove_file(path_out)?;
114    fs::remove_file(path_out2)?;
115    Ok(())
116}
More examples
Hide additional examples
examples/crypto_write_read.rs (line 35)
15fn main() -> Result<()> {
16    tracing_subscriber::fmt().init();
17
18    let cipher = Cipher::ChaCha20Poly1305;
19    let mut key = vec![0; cipher.key_len()];
20    crypto::create_rng().fill_bytes(key.as_mut_slice());
21    let key = SecretVec::from(key);
22
23    let mut args = args();
24    // skip the program name
25    let _ = args.next();
26    // will encrypt this file
27    let path_in = args.next().expect("path_in is missing");
28    // will save it in the same directory with .enc suffix
29    let out = Path::new(&path_in).to_path_buf().with_extension("enc");
30    if out.exists() {
31        std::fs::remove_file(&out)?;
32    }
33
34    let mut file = File::open(path_in.clone())?;
35    let mut writer = crypto::create_write(File::create(out.clone())?, cipher, &key);
36    info!("encrypt file");
37    io::copy(&mut file, &mut writer).unwrap();
38    writer.finish()?;
39
40    let mut reader = crypto::create_read(File::open(out)?, cipher, &key);
41    info!("read file and compare hash to original one");
42    let hash1 = crypto::hash_reader(&mut File::open(path_in)?)?;
43    let hash2 = crypto::hash_reader(&mut reader)?;
44    assert_eq!(hash1, hash2);
45
46    Ok(())
47}