Struct chacha::ChaCha

source ·
pub struct ChaCha { /* private fields */ }
Expand description

A ChaCha keystream.

After being initialized with a key and nonce, a ChaCha instance will generate a long stream of bytes that is indistinguishable from random for anyone not knowing the key and nonce.

§Examples

use chacha::{ChaCha, KeyStream};

let secret_key = [
    0x29, 0xfa, 0x35, 0x60, 0x88, 0x45, 0xc6, 0xf9, 
    0xd8, 0xfe, 0x65, 0xe3, 0x22, 0x0e, 0x5b, 0x05, 
    0x03, 0x4a, 0xa0, 0x9f, 0x9e, 0x27, 0xad, 0x0f, 
    0x6c, 0x90, 0xa5, 0x73, 0xa8, 0x10, 0xe4, 0x94, 
];
let nonce = [0u8; 8];
let mut stream = ChaCha::new_chacha20(&secret_key, &nonce);

let mut buffer = *b"abcdef";
stream.xor_read(&mut buffer[..]).expect("hit end of stream far too soon");
let expected_ciphertext = [0xde, 0x87, 0xa5, 0xbe, 0x1d, 0x77];
assert_eq!(buffer, expected_ciphertext);

Implementations§

source§

impl ChaCha

source

pub fn new_ietf(key: &[u8; 32], nonce: &[u8; 12]) -> ChaCha

Create a ChaCha stream conforming to the IETF’s RFC 7539. The stream takes a 12-byte nonce and has a length of 238 bytes, or 256 GiB.

source

pub fn new_chacha20(key: &[u8; 32], nonce: &[u8; 8]) -> ChaCha

Create a ChaCha stream with an 8-byte nonce and has a length of 270 bytes. This is compatible with libsodium’s ChaCha20 implementation and Daniel Bernstein’s original specification.

source

pub fn new_chacha12(key: &[u8; 32], nonce: &[u8; 8]) -> ChaCha

Create a ChaCha stream with an 8-byte nonce and has a length of 270 bytes. This is compatible with libsodium’s ChaCha12 implementation. ChaCha12 decreases security margin relative to ChaCha20 in favor of speed.

source

pub fn new_chacha8(key: &[u8; 32], nonce: &[u8; 8]) -> ChaCha

Create a ChaCha stream with an 8-byte nonce and has a length of 270 bytes. This is compatible with libsodium’s ChaCha12 implementation. ChaCha8 decreases security margin relative to ChaCha20 in favor of speed.

source

pub fn new_xchacha20(key: &[u8; 32], nonce: &[u8; 24]) -> ChaCha

Create a ChaCha stream with a 24-byte nonce and a length of 270 bytes. This stream’s initialization relates to ChaCha20 in the same way that that XSalsa20 relates to Salsa20.

Trait Implementations§

source§

impl Clone for ChaCha

source§

fn clone(&self) -> ChaCha

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl KeyStream for ChaCha

source§

fn xor_read(&mut self, dest: &mut [u8]) -> Result<(), Error>

XORs keystream bytes with dest. Read more
source§

impl SeekableKeyStream for ChaCha

source§

fn seek_to(&mut self, byte_offset: u64) -> Result<(), Error>

Seeks to a position, with byte resolution, in the keystream. Read more

Auto Trait Implementations§

§

impl Freeze for ChaCha

§

impl RefUnwindSafe for ChaCha

§

impl Send for ChaCha

§

impl Sync for ChaCha

§

impl Unpin for ChaCha

§

impl UnwindSafe for ChaCha

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> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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.