Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[allow(dead_code)]
- fn rc4(n: Vec<u8>) -> Vec<u8> {
- let (mut s,mut j, mut t,l) = ((0..256).collect::<Vec<usize>>(),0,0,n[0] as usize);
- let (key,msg) = n[1..].split_at(l);
- (0..256).fold(0,|a,i| {
- j = (a + s[i] + key[i % l] as usize) % 256;
- t=s[i];s[i]=s[j];s[j]=t;j
- });
- j=0;
- (1..).zip(msg.iter()).map(|(i,b)| {
- j = (j + s[i]) % 256;
- t=s[i];s[i]=s[j];s[j]=t;
- s[(s[i] + s[j]) % 256] as u8 ^ b
- }).collect::<Vec<u8>>()
- }
- #[cfg(test)] // cfg -> section will only compiled during 'cargo test'
- mod tests {
- // namespace helper
- use super::*; // bring in our functions above
- #[test] // next function will be a single test
- fn test_rc4() {
- assert!(
- rc4(b"\x0Dthis is a keythis is some data to encrypt".to_vec())
- == b"\xb5\xdb?i\x1f\x92\x96\x96e!\xf3\xae(!\xf3\xeaC\xd4\x9fS\xbd?d\x82\x84{\xcdN"
- .to_vec()
- );
- assert!(rc4(b"\x01\x00\x00\x00\x00\x00\x00".to_vec()) == b"\xde\x18\x89\x41\xa3".to_vec());
- assert!( rc4( b"Sthis is a rather long key because the value of S is 83 so the key length must matchand this is the data to be encrypted".to_vec())
- ==b"\x96\x1f,\x8f\xa3%\x9b\xa3f[mk\xdf\xbc\xac\x8b\x8e\xfa\xfe\x96B=!\xfc;\x13`c\x16q\x04\x11\xd8\x86\xee\x07".to_vec() );
- assert!( rc4( b"\x0dthis is a key\xb5\xdb?i\x1f\x92\x96\x96e!\xf3\xae(!\xf3\xeaC\xd4\x9fS\xbd?d\x82\x84{\xcdN".to_vec() )==
- b"\x74\x68\x69\x73\x20\x69\x73\x20\x73\x6f\x6d\x65\x20\x64\x61\x74\x61\x20\x74\x6f\x20\x65\x6e\x63\x72\x79\x70\x74".to_vec() );
- } // test f() by itself (unit)
- }
Add Comment
Please, Sign In to add comment