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
impl ChaCha
sourcepub fn new_ietf(key: &[u8; 32], nonce: &[u8; 12]) -> ChaCha
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.
sourcepub fn new_chacha20(key: &[u8; 32], nonce: &[u8; 8]) -> ChaCha
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.
sourcepub fn new_chacha12(key: &[u8; 32], nonce: &[u8; 8]) -> ChaCha
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.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)