Skip to content

Commit 303b77a

Browse files
author
Andreas Auernhammer
committed
add docs for EncrWriter
This commit adds documentation and example code for the `EncWriter` type.
1 parent 2a911a9 commit 303b77a

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

src/lib.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,50 @@ pub const BUF_SIZE: usize = 1 << 14;
1616

1717
/// Wraps a writer and encrypts and authenticates everything written to it.
1818
///
19+
/// `EncrWriter` splits data into fixed-size fragments and encrypts and
20+
/// authenticates each fragment separately. It appends any remaining data
21+
/// to its in-memory buffer until it has gathered a complete fragment.
22+
/// Therefore, using an `std::io::BufWriter` in addition usually does not
23+
/// improve the performance of write calls. The only exception may be cases
24+
/// when the buffer size of the `BufWriter` is significantly larger than the
25+
/// fragment size of the `EncWriter`.
26+
///
27+
/// When the `EncWriter` is dropped, any buffered content will be encrypted
28+
/// as well as authenticated and written out. However, any errors that happen
29+
/// in the process of flushing the buffer when the `EncWriter` is dropped will
30+
/// be ignored. Therefore, code should call `flush` explicitly to ensure that
31+
/// all encrypted data has been written out successfully.
1932
/// # Examples
2033
///
2134
/// Let's encrypt a string and store the ciphertext in memory:
2235
///
2336
/// ```
24-
/// use std::io::{Write, Read, copy, repeat};
37+
/// use std::io::{Write, Read};
2538
/// use sio::{Key, Nonce, Aad, EncWriter};
26-
/// use sio::ring;
39+
/// use sio::ring::AES_256_GCM;
40+
///
41+
/// // Load your secret keys from a secure location or derive
42+
/// // them using a secure (password-based) key-derivation-function, like Argon2id.
43+
/// // Obviously, don't use this all-zeros key for anything real.
44+
/// let key: Key<AES_256_GCM> = Key::new([0; Key::<AES_256_GCM>::SIZE]);
45+
///
46+
/// // Make sure you use an unique key-nonce combination!
47+
/// // Reusing a nonce value for the same secret key breaks
48+
/// // the security of the encryption algorithm.
49+
/// let nonce = Nonce::new([0; Nonce::<AES_256_GCM>::SIZE]);
50+
///
51+
/// // You must be able to re-generate this aad to decrypt
52+
/// // the ciphertext again. Usually, it's stored together with
53+
/// // the encrypted data.
54+
/// let aad = Aad::from("Some authenticated but not encrypted data".as_bytes());
2755
///
28-
/// let key: Key<ring::AES_256_GCM> = Key::new([0; 32]);
29-
/// let nonce = Nonce::new([0; 8]);
30-
/// let aad = Aad::empty();
56+
/// let mut plaintext = "Some example plaintext".as_bytes();
3157
///
32-
/// let mut plaintext = repeat('y' as u8).take(5);
33-
/// let mut ciphertext: Vec<u8> = Vec::default();
58+
/// let mut ciphertext: Vec<u8> = Vec::default(); // Store the ciphertext in memory.
3459
/// let mut writer = EncWriter::new(ciphertext, &key, nonce, aad);
35-
/// copy(&mut plaintext, &mut writer).and_then(|_| writer.flush()).unwrap();
60+
///
61+
/// writer.write_all(plaintext).unwrap();
62+
/// writer.flush().unwrap(); // Complete the encryption process explicitly.
3663
/// ```
3764
pub struct EncWriter<A: Algorithm, W: Write> {
3865
inner: W,
@@ -122,6 +149,11 @@ impl<A: Algorithm, W: Write> Write for EncWriter<A, W> {
122149
Ok(n)
123150
}
124151

152+
#[inline]
153+
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
154+
self.write(buf).and(Ok(()))
155+
}
156+
125157
fn flush(&mut self) -> io::Result<()> {
126158
if self.flushed {
127159
return Err(io::Error::from(io::ErrorKind::Other));
@@ -231,6 +263,7 @@ impl<A: Algorithm, W: Write> Write for DecWriter<A, W> {
231263

232264
#[cfg(test)]
233265
mod tests {
266+
234267
use super::ring::AES_256_GCM;
235268
use super::*;
236269
use std::io::{Read, Write};

0 commit comments

Comments
 (0)