#![feature(test)]
extern crate test;
extern crate cipher;
use cipher::feistel_encrypt;
use cipher::feistel_decrypt;
#[test]
fn it_works() {
let ciphertext = feistel_encrypt("keenan", 19, 4);
let plaintext = feistel_decrypt(ciphertext, 19, 4);
assert_eq!("keenan", plaintext);
let odd_ciphertext = feistel_encrypt("ricky", 110, 2);
let odd_plaintext = feistel_decrypt(odd_ciphertext, 110, 2);
assert_eq!("ricky", odd_plaintext);
let odd_round_ciphertext = feistel_encrypt("julian", 92, 5);
let odd_round_plaintext = feistel_decrypt(odd_round_ciphertext, 92, 5);
assert_eq!("julian", odd_round_plaintext);
}
#[test]
fn it_works_larger() {
let ciphertext = feistel_encrypt("Larger string", 10091993, 32);
let plaintext = feistel_decrypt(ciphertext, 10091993, 32);
assert_eq!("Larger string", plaintext);
let odd_rounds_even_ciphertext = feistel_encrypt(
"Some odd rounds even str", 564738291, 25);
let odd_rounds_even_plaintext = feistel_decrypt(
odd_rounds_even_ciphertext, 564738291, 25);
assert_eq!("Some odd rounds even str", odd_rounds_even_plaintext);
let odd_rounds_ciphertext = feistel_encrypt(
"Some odd rounds", 564738291, 25);
let odd_rounds_plaintext = feistel_decrypt(
odd_rounds_ciphertext, 564738291, 25);
assert_eq!("Some odd rounds", odd_rounds_plaintext);
}
#[test]
fn max_key_rounds() {
let even_ciphertext = feistel_encrypt(
"The quick blue fox", 4294967295, 255);
let even_plaintext = feistel_decrypt(
even_ciphertext, 4294967295, 255);
assert_eq!("The quick blue fox", even_plaintext);
let odd_ciphertext = feistel_encrypt(
"The quick brown fox", 4294967295, 255);
let odd_plaintext = feistel_decrypt(odd_ciphertext, 4294967295, 255);
assert_eq!("The quick brown fox", odd_plaintext);
}
#[test]
fn variance() {
let plain_ayes = "abcdefghijklmnopqrstuvwxyz";
let cipher_ayes = feistel_encrypt(plain_ayes, 186220, 217);
let mean: f32= (cipher_ayes.iter().fold(0, |sum, x| sum + x)
/ cipher_ayes.len() as u32) as f32;
let mut variance: f32 = 0_f32;
for x in cipher_ayes.iter() {
variance += (*x as f32 - mean).powi(2);
}
variance = variance / cipher_ayes.len() as f32;
let standard_deviation = variance.sqrt();
let teir_1: f32 = 0.40;
let teir_2: f32 = 0.05;
assert!(standard_deviation / mean > teir_1 ||
standard_deviation / mean < teir_2);
}