Struct RefWriteBuffer

Source
pub struct RefWriteBuffer<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> RefWriteBuffer<'a>

Source

pub fn new(buff: &mut [u8]) -> RefWriteBuffer<'_>

Examples found in repository?
examples/symmetriccipher.rs (line 35)
17fn encrypt(data: &[u8], key: &[u8], iv: &[u8]) -> Result<Vec<u8>, symmetriccipher::SymmetricCipherError> {
18
19    // Create an encryptor instance of the best performing
20    // type available for the platform.
21    let mut encryptor = aes::cbc_encryptor(
22            aes::KeySize::KeySize256,
23            key,
24            iv,
25            blockmodes::PkcsPadding);
26
27    // Each encryption operation encrypts some data from
28    // an input buffer into an output buffer. Those buffers
29    // must be instances of RefReaderBuffer and RefWriteBuffer
30    // (respectively) which keep track of how much data has been
31    // read from or written to them.
32    let mut final_result = Vec::<u8>::new();
33    let mut read_buffer = buffer::RefReadBuffer::new(data);
34    let mut buffer = [0; 4096];
35    let mut write_buffer = buffer::RefWriteBuffer::new(&mut buffer);
36
37    // Each encryption operation will "make progress". "Making progress"
38    // is a bit loosely defined, but basically, at the end of each operation
39    // either BufferUnderflow or BufferOverflow will be returned (unless
40    // there was an error). If the return value is BufferUnderflow, it means
41    // that the operation ended while wanting more input data. If the return
42    // value is BufferOverflow, it means that the operation ended because it
43    // needed more space to output data. As long as the next call to the encryption
44    // operation provides the space that was requested (either more input data
45    // or more output space), the operation is guaranteed to get closer to
46    // completing the full operation - ie: "make progress".
47    //
48    // Here, we pass the data to encrypt to the enryptor along with a fixed-size
49    // output buffer. The 'true' flag indicates that the end of the data that
50    // is to be encrypted is included in the input buffer (which is true, since
51    // the input data includes all the data to encrypt). After each call, we copy
52    // any output data to our result Vec. If we get a BufferOverflow, we keep
53    // going in the loop since it means that there is more work to do. We can
54    // complete as soon as we get a BufferUnderflow since the encryptor is telling
55    // us that it stopped processing data due to not having any more data in the
56    // input buffer.
57    loop {
58        let result = try!(encryptor.encrypt(&mut read_buffer, &mut write_buffer, true));
59
60        // "write_buffer.take_read_buffer().take_remaining()" means:
61        // from the writable buffer, create a new readable buffer which
62        // contains all data that has been written, and then access all
63        // of that data as a slice.
64        final_result.extend(write_buffer.take_read_buffer().take_remaining().iter().map(|&i| i));
65
66        match result {
67            BufferResult::BufferUnderflow => break,
68            BufferResult::BufferOverflow => { }
69        }
70    }
71
72    Ok(final_result)
73}
74
75// Decrypts a buffer with the given key and iv using
76// AES-256/CBC/Pkcs encryption.
77//
78// This function is very similar to encrypt(), so, please reference
79// comments in that function. In non-example code, if desired, it is possible to
80// share much of the implementation using closures to hide the operation
81// being performed. However, such code would make this example less clear.
82fn decrypt(encrypted_data: &[u8], key: &[u8], iv: &[u8]) -> Result<Vec<u8>, symmetriccipher::SymmetricCipherError> {
83    let mut decryptor = aes::cbc_decryptor(
84            aes::KeySize::KeySize256,
85            key,
86            iv,
87            blockmodes::PkcsPadding);
88
89    let mut final_result = Vec::<u8>::new();
90    let mut read_buffer = buffer::RefReadBuffer::new(encrypted_data);
91    let mut buffer = [0; 4096];
92    let mut write_buffer = buffer::RefWriteBuffer::new(&mut buffer);
93
94    loop {
95        let result = try!(decryptor.decrypt(&mut read_buffer, &mut write_buffer, true));
96        final_result.extend(write_buffer.take_read_buffer().take_remaining().iter().map(|&i| i));
97        match result {
98            BufferResult::BufferUnderflow => break,
99            BufferResult::BufferOverflow => { }
100        }
101    }
102
103    Ok(final_result)
104}

Trait Implementations§

Source§

impl<'a> WriteBuffer for RefWriteBuffer<'a>

Source§

fn is_empty(&self) -> bool

Source§

fn is_full(&self) -> bool

Source§

fn remaining(&self) -> usize

Source§

fn capacity(&self) -> usize

Source§

fn rewind(&mut self, distance: usize)

Source§

fn reset(&mut self)

Source§

fn peek_read_buffer(&mut self) -> RefReadBuffer<'_>

Source§

fn take_next(&mut self, count: usize) -> &mut [u8]

Source§

fn take_read_buffer(&mut self) -> RefReadBuffer<'_>

Source§

fn position(&self) -> usize

Source§

fn take_remaining(&mut self) -> &mut [u8]

Auto Trait Implementations§

§

impl<'a> Freeze for RefWriteBuffer<'a>

§

impl<'a> RefUnwindSafe for RefWriteBuffer<'a>

§

impl<'a> Send for RefWriteBuffer<'a>

§

impl<'a> Sync for RefWriteBuffer<'a>

§

impl<'a> Unpin for RefWriteBuffer<'a>

§

impl<'a> !UnwindSafe for RefWriteBuffer<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.