diff options
Diffstat (limited to 'src/random.rs')
-rw-r--r-- | src/random.rs | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/random.rs b/src/random.rs index e99b085..af0bc43 100644 --- a/src/random.rs +++ b/src/random.rs @@ -92,3 +92,21 @@ impl std::fmt::Debug for MersenneTwister { write!(f, " }}") } } + +pub fn mt19937_stream_cipher (ciphertext: &[u8], key: u32) -> Vec<u8> { + let mut mt = MersenneTwister::from_seed(key); + let keystream: Vec<u8> = mt.gen_iter().take(ciphertext.len()).collect(); + return ::primitives::fixed_xor(ciphertext, &keystream[..]); +} + +#[test] +fn test_mt19937_stream_cipher () { + let key = ::rand::thread_rng().gen(); + let plaintext = b"Summertime and the wind is blowing outside in lower \ + Chelsea and I don't know what I'm doing in the city, the \ + sun is always in my eyes"; + let ciphertext = mt19937_stream_cipher(&plaintext[..], key); + assert!(&plaintext[..] != &ciphertext[..]); + let plaintext2 = mt19937_stream_cipher(&ciphertext[..], key); + assert_eq!(&plaintext[..], &plaintext2[..]); +} |