Expand description
The kcipher2 crate is an implementation of the KCipher-2 stream cipher
as described in RFC 7008.
Cipher functionality is accessed using traits from re-exported cipher
crate.
§Examples
use hex_literal::hex;
use kcipher2::{
KCipher2,
cipher::{KeyIvInit, StreamCipher},
};
let key = [0x42; 16];
let nonce = [0x24; 16];
let plaintext = hex!("00010203 04050607 08090A0B 0C0D0E0F");
let ciphertext = hex!("471694B5 EB93E4A6 EABA73DF A6F77057");
// Key and IV must be references to the `Array` type. Here we use the `Into`
// trait to convert arrays into it.
let mut cipher = KCipher2::new(&key.into(), &nonce.into());
let mut buf = plaintext;
// Apply keystream (encrypt).
cipher.apply_keystream(&mut buf);
assert_eq!(buf, ciphertext);
let ciphertext = buf;
// Decrypt ciphertext by applying keystream again.
let mut cipher = KCipher2::new(&key.into(), &nonce.into());
cipher.apply_keystream(&mut buf);
assert_eq!(buf, plaintext);
// Stream ciphers can be used with streaming messages.
let mut cipher = KCipher2::new(&key.into(), &nonce.into());
for chunk in buf.chunks_mut(3) {
cipher.apply_keystream(chunk);
}
assert_eq!(buf, ciphertext);Re-exports§
pub use cipher;
Structs§
- KCipher2
Core - Core state of the KCipher-2 stream cipher.