# Encrypted Packets

Краен срок
17.12.2019 17:00

Срокът за предаване на решения е отминал

# Encrypted Packets

Така и така сме тръгнали да пращаме пакети напред-назад, защо и да не ги криптираме, да сме сигурни, че ако някой по средата ги прихване, няма да разбере за какво си говорим. Разбира се, трябва да използваме супер-сигурен алгоритъм, така че ще XOR-нем всеки байт с някакво число (note: това е шега, не "криптирайте" неща с XOR-ване).

Криптирането работи така:

• Имаме някакъв slice от байтове ("payload").
• Имаме 32-битов шифър ("cypher").
• Разбиваме 32-битовото число на 4 байта, big endian.
• Вземаме първите 4 байта на payload-a.
• XOR-ваме байтовете на шифъра с тези на payload-а, един по един
• Вземаме току-що криптираните 4 байта (резултата от криптирането) и използваме тях за нов шифър
• И така докрай

Ако в който и да е момент няма 4 байта в остатъка от payload-а -- колкото има.

За декриптирането единствено ще ви кажем, че XOR-ването два пъти с едно и също число се неутрализира, тоест (a ^ b) ^ b ви дава просто a.

Интерфейса очакваме да изглежда така:

pub struct Packet {
}

impl Packet {
pub fn new(source: &[u8]) -> Self {
}

pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
unimplemented!()
}
}

pub struct EncryptedPacket {
}

impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
unimplemented!()
}
}


Вече нямаме дължини и чексуми за опростяване, приемаме, че ги оправихме тия работи вече в домашното. Примерно извикване може да изглежда така:

let packet = Packet::new(b"Secret message, plz don't hack");


## Решения

• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Андрей
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let mut cypher_bytes: Vec<u8> = cypher.to_be_bytes().into_iter().cloned().collect();
let encrypted_chunk = chunk.into_iter().zip(cypher_bytes.into_iter()).
map(|(pb, cb)| *pb ^ cb).
collect();
result.extend(&encrypted_chunk);
cypher_bytes = encrypted_chunk;
}
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let mut cypher_bytes: Vec<u8> = cypher.to_be_bytes().into_iter().cloned().collect();
let decrypted_chunk = chunk.into_iter().zip(cypher_bytes.into_iter()).
map(|(pb, cb)| *pb ^ cb).
collect::<Vec<u8>>();
result.extend(&decrypted_chunk);
cypher_bytes = chunk.to_vec();
}
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-xaogs4/solution)
Finished dev [unoptimized + debuginfo] target(s) in 1.93s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Стефан Чолаков
pub struct Packet {
}
pub fn helper(payload: &Vec<u8>, cypher:u32, encrypt:bool)-> Vec<u8>{
let mut cypher_as_bytes = cypher.to_be_bytes();
let mut result: Vec<u8> = Vec::new();
let mut cypher_index = 0;
result.push(current_byte);
if encrypt {
cypher_as_bytes[cypher_index] = current_byte;
}else{
}
cypher_index+=1;
if cypher_index==4 { cypher_index = 0; }
}
result
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-1ft4wft/solution)
Finished dev [unoptimized + debuginfo] target(s) in 1.94s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Антонио Миндов
use std::cmp::min;
fn xor_encryption(bytes: &[u8], cypher: u32, decrypt: bool) -> Vec<u8> {
let mut result = Vec::with_capacity(bytes.len());
let mut current_cypher: Vec<u8> = cypher.to_be_bytes().to_vec();
let mut bytes = bytes;
while bytes.len() > 0 {
let (current_bytes, rest) = bytes.split_at(min(4, bytes.len()));
let encrypted_bytes: Vec<u8> = current_bytes.iter().zip(current_cypher)
.map(|bytes| bytes.0 ^ bytes.1)
.collect();
current_cypher = if decrypt { current_bytes.to_vec() }
else { encrypted_bytes.clone() };
result.extend(encrypted_bytes);
bytes = rest;
}
result
}
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-wmvwr6/solution)
Finished dev [unoptimized + debuginfo] target(s) in 2.14s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Петър Петров
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let mut count=0;
let mut b1 : u8 = ((cypher >> 24) & 0xff) as u8;
let mut b2 : u8 = ((cypher >> 16) & 0xff) as u8;
let mut b3 : u8 = ((cypher >> 8) & 0xff) as u8;
let mut b4 : u8 = (cypher & 0xff) as u8;
let mut vec = Vec::new();
loop {
if len >= 4 {
b1=x1;
vec.push(x1);
count+=1;
b2=x2;
vec.push(x2);
count+=1;
b3=x3;
vec.push(x3);
count+=1;
b4=x4;
vec.push(x4);
count+=1;
len=len-4;
}
else
{
break;
}
}
if len > 0 {
if len == 1 {
b1=x1;
vec.push(x1);
}
else if len == 2 {
b1=x1;
vec.push(x1);
count+=1;
b2=x2;
vec.push(x2);
count+=1;
}
else {
b1=x1;
vec.push(x1);
count+=1;
b2=x2;
vec.push(x2);
count+=1;
b3=x3;
vec.push(x3);
count+=1;
}
}
}
}
pub fn toVector(payload:&Vec<u8>,cypher: u32) -> Vec<u8> {
let mut count=0;
let mut b1 : u8 = ((cypher >> 24) & 0xff) as u8;
let mut b2 : u8 = ((cypher >> 16) & 0xff) as u8;
let mut b3 : u8 = ((cypher >> 8) & 0xff) as u8;
let mut b4 : u8 = (cypher & 0xff) as u8;
let mut vec = Vec::new();
loop {
if len >= 4 {
vec.push(x1);
count+=1;
vec.push(x2);
count+=1;
vec.push(x3);
count+=1;
vec.push(x4);
count+=1;
len=len-4;
}
else
{
break;
}
}
if len > 0 {
if len == 1 {
b1=x1;
vec.push(x1);
}
else if len == 2 {
b1=x1;
vec.push(x1);
count+=1;
b2=x2;
vec.push(x2);
count+=1;
}
else {
b1=x1;
vec.push(x1);
count+=1;
b2=x2;
vec.push(x2);
count+=1;
b3=x3;
vec.push(x3);
count+=1;
}
}
vec
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-6aljaj/solution)
warning: value assigned to b1 is never read
--> src/lib.rs:114:17
|
114 |                 b1=x1;
|                 ^^
|
= note: #[warn(unused_assignments)] on by default
= help: maybe it is overwritten before being read?

warning: value assigned to b1 is never read
--> src/lib.rs:119:16
|
119 |                b1=x1;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b2 is never read
--> src/lib.rs:123:16
|
123 |                b2=x2;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to count is never read
--> src/lib.rs:125:16
|
125 |                count+=1;
|                ^^^^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b1 is never read
--> src/lib.rs:129:16
|
129 |                b1=x1;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b2 is never read
--> src/lib.rs:133:16
|
133 |                b2=x2;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b3 is never read
--> src/lib.rs:137:16
|
137 |                b3=x3;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to count is never read
--> src/lib.rs:139:16
|
139 |                count+=1;
|                ^^^^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b1 is never read
--> src/lib.rs:46:17
|
46 |                 b1=x1;
|                 ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b1 is never read
--> src/lib.rs:51:16
|
51 |                b1=x1;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b2 is never read
--> src/lib.rs:55:16
|
55 |                b2=x2;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to count is never read
--> src/lib.rs:57:16
|
57 |                count+=1;
|                ^^^^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b1 is never read
--> src/lib.rs:61:16
|
61 |                b1=x1;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b2 is never read
--> src/lib.rs:65:16
|
65 |                b2=x2;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b3 is never read
--> src/lib.rs:69:16
|
69 |                b3=x3;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to count is never read
--> src/lib.rs:71:16
|
71 |                count+=1;
|                ^^^^^
|
= help: maybe it is overwritten before being read?

warning: variable does not need to be mutable
--> src/lib.rs:150:12
|
|            ----^^^
|            |
|            help: remove this mut
|
= note: #[warn(unused_mut)] on by default

warning: function toVector should have a snake case name
--> src/lib.rs:78:8
|
78 | pub fn toVector(payload:&Vec<u8>,cypher: u32) -> Vec<u8> {
|        ^^^^^^^^ help: convert the identifier to snake case: to_vector
|
= note: #[warn(non_snake_case)] on by default

warning: value assigned to b1 is never read
--> src/lib.rs:114:17
|
114 |                 b1=x1;
|                 ^^
|
= note: #[warn(unused_assignments)] on by default
= help: maybe it is overwritten before being read?

warning: value assigned to b1 is never read
--> src/lib.rs:119:16
|
119 |                b1=x1;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b2 is never read
--> src/lib.rs:123:16
|
123 |                b2=x2;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to count is never read
--> src/lib.rs:125:16
|
125 |                count+=1;
|                ^^^^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b1 is never read
--> src/lib.rs:129:16
|
129 |                b1=x1;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b2 is never read
--> src/lib.rs:133:16
|
133 |                b2=x2;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b3 is never read
--> src/lib.rs:137:16
|
137 |                b3=x3;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to count is never read
--> src/lib.rs:139:16
|
139 |                count+=1;
|                ^^^^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b1 is never read
--> src/lib.rs:46:17
|
46 |                 b1=x1;
|                 ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b1 is never read
--> src/lib.rs:51:16
|
51 |                b1=x1;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b2 is never read
--> src/lib.rs:55:16
|
55 |                b2=x2;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to count is never read
--> src/lib.rs:57:16
|
57 |                count+=1;
|                ^^^^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b1 is never read
--> src/lib.rs:61:16
|
61 |                b1=x1;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b2 is never read
--> src/lib.rs:65:16
|
65 |                b2=x2;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to b3 is never read
--> src/lib.rs:69:16
|
69 |                b3=x3;
|                ^^
|
= help: maybe it is overwritten before being read?

warning: value assigned to count is never read
--> src/lib.rs:71:16
|
71 |                count+=1;
|                ^^^^^
|
= help: maybe it is overwritten before being read?

warning: variable does not need to be mutable
--> src/lib.rs:150:12
|
|            ----^^^
|            |
|            help: remove this mut
|
= note: #[warn(unused_mut)] on by default

warning: function toVector should have a snake case name
--> src/lib.rs:78:8
|
78 | pub fn toVector(payload:&Vec<u8>,cypher: u32) -> Vec<u8> {
|        ^^^^^^^^ help: convert the identifier to snake case: to_vector
|
= note: #[warn(non_snake_case)] on by default

Finished dev [unoptimized + debuginfo] target(s) in 1.73s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Димитър Иринков
use std::mem::size_of;
pub struct Packet {
}
pub struct EncryptedPacket {
}
fn encrypt_step(bytes: &mut [u8], cypher: &mut [u8;4]) {
for (a,b) in bytes.iter_mut().zip(cypher.iter_mut()) {
*a = *a ^ *b;
*b = *a;
}
}
fn decrypt_step(bytes: &mut [u8], cypher: &mut [u8;4]) {
for (a,b) in bytes.iter_mut().zip(cypher.iter_mut()) {
let temp = *a;
*a = *a ^ *b;
*b = temp;
}
}
fn de_en_crypt(payload: &[u8], cypher: u32, step: fn(&mut [u8], &mut [u8;4])) -> Vec<u8> {
let mut cypher = cypher.to_be_bytes();
v
.chunks_mut(size_of::<u32>())
.for_each(|bytes| {
step(bytes, &mut cypher);
});
v
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let encrypted = de_en_crypt(&self.payload, cypher, encrypt_step);
}
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let decrypted = de_en_crypt(&self.payload, cypher, decrypt_step);
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-7mtap4/solution)
Finished dev [unoptimized + debuginfo] target(s) in 1.76s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Йоан Стоянов
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let mut bytes = cypher.to_be_bytes();
let mut encrypted: Vec<u8> = Vec::new();;
let mut current;
let mut i:usize = 0;
current = bytes[i]^element;
encrypted.push(current);
bytes[i]=current;
i+=1; i=i%4;
}
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let mut bytes = cypher.to_be_bytes();
let mut encrypted: Vec<u8> = Vec::new();;
let mut current;
let mut i:usize = 0;
current = bytes[i]^element;
encrypted.push(current);
bytes[i]=*element;
i+=1; i=i%4;
}
}
}
#[test]
fn test_basic_iteration() {
let packet = Packet::new(b"Secret message, plz don't hack");
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-t7rc18/solution)
warning: unnecessary trailing semicolon
--> src/lib.rs:12:49
|
12 |         let mut encrypted: Vec<u8> = Vec::new();;
|                                                 ^ help: remove this semicolon
|
= note: #[warn(redundant_semicolon)] on by default

warning: unnecessary trailing semicolon
--> src/lib.rs:32:49
|
32 |         let mut encrypted: Vec<u8> = Vec::new();;
|                                                 ^ help: remove this semicolon

warning: unnecessary trailing semicolon
--> src/lib.rs:12:49
|
12 |         let mut encrypted: Vec<u8> = Vec::new();;
|                                                 ^ help: remove this semicolon
|
= note: #[warn(redundant_semicolon)] on by default

warning: unnecessary trailing semicolon
--> src/lib.rs:32:49
|
32 |         let mut encrypted: Vec<u8> = Vec::new();;
|                                                 ^ help: remove this semicolon

Finished dev [unoptimized + debuginfo] target(s) in 1.90s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 1 test
test test_basic_iteration ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Стоян Ефтимов
pub struct Packet {
}
impl Packet {
const CIPHER_SIZE: u8 = 4;
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cipher: u32) -> EncryptedPacket {
let mut cipher = cipher.to_be_bytes();
let mut encrypted_payload: Vec<u8> = vec![];
loop {
let n = payload.len().min(Packet::CIPHER_SIZE as usize);
if n == 0 {
}
let ciphered: Vec<u8> = bytes.iter().
zip(cipher.iter()).
map(|(byte, cipher)| byte ^ cipher).
collect();
for (i, &byte) in ciphered.iter().enumerate() {
cipher[i] = byte;
}
}
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cipher: u32) -> Packet {
let mut cipher = cipher.to_be_bytes();
let mut payload: Vec<u8> = vec![];
loop {
let n = encrypted_payload.len().min(Packet::CIPHER_SIZE as usize);
if n == 0 {
}
let decrypted: Vec<u8> = bytes.iter().
zip(cipher.iter()).
map(|(byte, cipher)| byte ^ cipher).
collect();
for (i, &byte) in bytes.iter().enumerate() {
cipher[i] = byte;
}
}
}
}
#[test]
fn basic_test() {
let packet = Packet::new(b"Secret message, please don't hack");
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-gzl0ac/solution)
Finished dev [unoptimized + debuginfo] target(s) in 2.41s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 1 test
test basic_test ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Любослав Карев
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let mut cypher_as_bytes: [u8; 4] = cypher.to_be_bytes();
let mut result: EncryptedPacket = EncryptedPacket { payload: vec![] };
for i in 0..4 {
let current_bytes_xor = self.payload[(offset*4) + i] ^ cypher_as_bytes[i];
cypher_as_bytes[i] = current_bytes_xor;
}
}
for i in 0..self.payload.len() % 4 {
///Малко е нелогично, но така взимаме последните байтове
let offset = ((self.payload.len() / 4) * 4);
let current_bytes_xor = self.payload[offset + i] ^ cypher_as_bytes[i];
cypher_as_bytes[i] = current_bytes_xor;
}
result
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let mut cypher_as_bytes: [u8;4] = cypher.to_be_bytes();
let mut result: Packet = Packet{payload: vec![]};
for i in 0..4{
let current_bytes_xor = self.payload[(offset*4) + i] ^ cypher_as_bytes[i];
}
}
for i in 0..self.payload.len() % 4 {
///Малко е нелогично, но така взимаме последните байтове
let offset = ((self.payload.len() / 4) * 4);
let current_bytes_xor = self.payload[offset + i] ^ cypher_as_bytes[i];
cypher_as_bytes[i] = current_bytes_xor;
}
result
}
}
#[cfg(test)]
mod tests {
use crate::Packet;
#[test]
fn basic_test_1(){
let packet = Packet::new(b"a");
let encrypted_packet = packet.encrypt(10);
let decrypted_packet = encrypted_packet.decrypt(10);
}
#[test]
fn basic_test_2(){
let packet = Packet::new(b"Secret message, plz don't hack");
}
#[test]
fn basic_test_3(){
let packet = Packet::new(b"abcde");
let encrypted_packet = packet.encrypt(10);
let decrypted_packet = encrypted_packet.decrypt(10);
}
#[test]
fn basic_test_4(){
let packet = Packet::new(b"Hello, this is a non-hackable message");
let encrypted_packet = packet.encrypt('1' as u32);
let decrypted_packet = encrypted_packet.decrypt('2' as u32);
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-hrhm42/solution)
warning: unused doc comment
--> src/lib.rs:24:13
|
24 |             ///Малко е нелогично, но така взимаме последните байтове
|             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25 |             let offset = ((self.payload.len() / 4) * 4);
|             -------------------------------------------- rustdoc does not generate documentation for statements
|
= note: #[warn(unused_doc_comments)] on by default

warning: unused doc comment
--> src/lib.rs:50:13
|
50 |             ///Малко е нелогично, но така взимаме последните байтове
|             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51 |             let offset = ((self.payload.len() / 4) * 4);
|             -------------------------------------------- rustdoc does not generate documentation for statements

warning: unnecessary parentheses around assigned value
--> src/lib.rs:25:26
|
25 |             let offset = ((self.payload.len() / 4) * 4);
|                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses
|
= note: #[warn(unused_parens)] on by default

warning: unnecessary parentheses around assigned value
--> src/lib.rs:51:26
|
51 |             let offset = ((self.payload.len() / 4) * 4);
|                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses

warning: unused doc comment
--> src/lib.rs:24:13
|
24 |             ///Малко е нелогично, но така взимаме последните байтове
|             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25 |             let offset = ((self.payload.len() / 4) * 4);
|             -------------------------------------------- rustdoc does not generate documentation for statements
|
= note: #[warn(unused_doc_comments)] on by default

warning: unused doc comment
--> src/lib.rs:50:13
|
50 |             ///Малко е нелогично, но така взимаме последните байтове
|             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51 |             let offset = ((self.payload.len() / 4) * 4);
|             -------------------------------------------- rustdoc does not generate documentation for statements

warning: unnecessary parentheses around assigned value
--> src/lib.rs:25:26
|
25 |             let offset = ((self.payload.len() / 4) * 4);
|                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses
|
= note: #[warn(unused_parens)] on by default

warning: unnecessary parentheses around assigned value
--> src/lib.rs:51:26
|
51 |             let offset = ((self.payload.len() / 4) * 4);
|                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses

Finished dev [unoptimized + debuginfo] target(s) in 2.17s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 4 tests
test tests::basic_test_1 ... ok
test tests::basic_test_2 ... ok
test tests::basic_test_3 ... ok
test tests::basic_test_4 ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Николай Захаров
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
Self {
}
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let cypher = cypher.to_be_bytes().to_vec();
let mut encrypted_message = Vec::<u8>::new();
EncryptedPacket {
}
}
fn encrypt_recursive(remaining_message: &[u8], cypher: Vec<u8>, encrypted: &mut Vec<u8>) {
if remaining_message.len() >= 4 {
let xored = xor_slices(&remaining_message[..4], &cypher);
encrypted.extend(&xored);
Self::encrypt_recursive(&remaining_message[4..], xored, encrypted);
} else {
let xored = xor_slices(&remaining_message, &cypher[..remaining_message.len()]);
encrypted.extend(&xored);
}
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let first_element = match iterator_cpy.next() {
Some(x) => x,
None => return Packet::new(&[]), // just in case
};
.chunks(4)
.zip(iterator_cpy)
.flat_map(|(a, b)| xor_slices(b, a))
.collect();
let result = vec![
xor_slices(first_element, &cypher.to_be_bytes()),
remaining_elements,
]
.concat();
}
}
fn xor_slices(slice1: &[u8], slice2: &[u8]) -> Vec<u8> {
slice1
.iter()
.zip(slice2.iter())
.map(|(a, b)| a ^ b)
.collect()
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-k2yzf7/solution)
Finished dev [unoptimized + debuginfo] target(s) in 2.04s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Некоректно
• 3 успешни тест(а)
• 1 неуспешни тест(а)
Антон Чернев
use std::convert::TryInto;
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, mut cypher: u32) -> EncryptedPacket {
let mut ciphertext: Vec<u8> = Vec::new();
let length = plaintext.len();
while plaintext.len() > 0 {
let mut bytes = [0u8; 4];
for i in 0..4 {
bytes[i] = match plaintext.split_first() {
Some((first, rest)) => {
plaintext = rest;
*first
},
None => 0
}
}
let encrypted = u32::from_be_bytes(bytes) ^ cypher;
ciphertext.extend_from_slice(
&(encrypted).to_be_bytes()
);
cypher = encrypted;
}
EncryptedPacket { payload: ciphertext, length }
}
}
pub struct EncryptedPacket {
length: usize
}
impl EncryptedPacket {
pub fn decrypt(&self, mut cypher: u32) -> Packet {
let mut plaintext = Vec::new();
while ciphertext.len() > 0 {
let (bytes, rest) = ciphertext.split_at(4);
ciphertext = rest;
let encrypted = u32::from_be_bytes(bytes.try_into().unwrap());
plaintext.extend_from_slice(
&(encrypted ^ cypher).to_be_bytes()
);
cypher = encrypted;
}
plaintext.truncate(self.length);
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-1am3ifd/solution)
Finished dev [unoptimized + debuginfo] target(s) in 1.76s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... FAILED

failures:

---- solution_test::test_several_iterations stdout ----
thread 'main' panicked at 'assertion failed: (left == right)
left: [0, 0, 0, 0, 160, 15, 241, 85, 84, 15, 241, 85],
right: [0, 0, 0, 0, 160, 15, 241, 85, 84]', tests/solution_test.rs:49:5
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace.

failures:
solution_test::test_several_iterations

test result: FAILED. 3 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

error: test failed, to rerun pass '--test solution_test'
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Мирослав Лалев
#[derive(Debug, PartialEq)]
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
Self {
}
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let be_bytes = cypher.to_be_bytes();
EncryptedPacket {
}
}
let mut result = Vec::new();
loop {
.by_ref()
.enumerate()
.collect::<Vec<_>>();
result.extend(&bytes);
break;
}
}
result
}
}
#[derive(Debug, PartialEq)]
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let be_bytes = cypher.to_be_bytes();
Packet {
}
}
let mut result = Vec::new();
loop {
.by_ref()
.map(|&v| v)
.collect::<Vec<_>>();
break;
}
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
// [1, 1, 1, 1]
const TEST_CYPHER: u32 = (1 << 24) | (1 << 16) | (1 << 8) | (1 << 0);
#[test]
fn test_encrypt_decrypt() {
let v = (1..=10000)
.collect::<Vec<u32>>()
.iter()
.map(|&v| (v % 256) as u8)
.collect::<Vec<u8>>();
assert_eq!(
Packet::new(&v).encrypt(TEST_CYPHER).decrypt(TEST_CYPHER),
);
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-eug13b/solution)
Finished dev [unoptimized + debuginfo] target(s) in 2.69s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 1 test
test tests::test_encrypt_decrypt ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Иво Стефанов
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let mut cypher_bytes = cypher.to_be_bytes();
let mut count = 0;
for i in 0..encrypted_vec.len(){
encrypted_vec[i] ^= cypher_bytes[count];
count+=1;
if count == 4 {
cypher_bytes = encrypted_vec[i];
cypher_bytes = encrypted_vec[i-1];
cypher_bytes = encrypted_vec[i-2];
cypher_bytes = encrypted_vec[i-3];
count = 0;
}
}
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let mut cypher_bytes = cypher.to_be_bytes();
let mut next_cypher_bytes: Vec<u8> = Vec::with_capacity(4);
let mut count = 0;
for i in 0..decrypted_vec.len(){
next_cypher_bytes.push(decrypted_vec[i]);
decrypted_vec[i] ^= cypher_bytes[count];
count+=1;
if count == 4 {
for k in 0..cypher_bytes.len(){
cypher_bytes[k] = next_cypher_bytes[k];
}
next_cypher_bytes = Vec::with_capacity(4);
count = 0;
}
}
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-10y1va0/solution)
Finished dev [unoptimized + debuginfo] target(s) in 1.79s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Ангел Беширов
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let mut encrypted_result = Vec::new();
let mut payload_index: usize = 0;
let mut encryption_index: usize;
let mut cypher_bytes: [u8; 4] = cypher.to_be_bytes();
cypher_bytes[encryption_index] = encrypted_byte;
encrypted_result.push(encrypted_byte);
}
EncryptedPacket::new(&encrypted_result)
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn new(source: &[u8]) -> Self {
}
pub fn decrypt(&self, cypher: u32) -> Packet {
let mut decrypted_result = Vec::new();
let mut payload_index: usize = 0;
let mut decryption_index: usize;
let mut cypher_bytes: [u8; 4] = cypher.to_be_bytes();
decrypted_result.push(decrypted_byte);
}
Packet::new(&decrypted_result)
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-120lf7g/solution)
Finished dev [unoptimized + debuginfo] target(s) in 1.73s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Парашкев Катерски
pub struct Packet {
}
fn ivancho(payload: &Vec<u8>, cypher: u32, decrypt: bool) -> Vec<u8> {
(Vec::<u8>::new(), cypher.to_be_bytes().to_vec()),
|(mut acc, cypher), x| {
let res = x
.iter()
.zip(cypher.iter())
.fold(Vec::<u8>::new(), |mut acc, (x, y)| {
acc.push(x ^ y);
acc
});
acc.extend(&res);
(acc, if decrypt { x.to_vec() } else { res })
},
);
acc
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-4vx477/solution)
Finished dev [unoptimized + debuginfo] target(s) in 1.74s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Христина Христова
pub struct Packet
{
}
impl Packet
{
pub fn new(source: &[u8]) -> Self
{
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket
{
let mut c = cypher.to_be_bytes();
let mut b:u8;
let mut vec = Vec::new();
let mut i=0;
{
if i==4
{
i=0;
}
b=*item^c[i];
c[i]=b;
vec.push(b);
i+=1;
}
}
}
pub struct EncryptedPacket
{
}
impl EncryptedPacket
{
pub fn decrypt(&self, cypher: u32) -> Packet
{
let mut c = cypher.to_be_bytes();
let mut b:u8;
let mut vec = Vec::new();
let mut i=0;
{
if i==4
{
i=0;
}
b=*item^c[i];
c[i]=*item;
vec.push(b);
i+=1;
}
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-1ndrl7w/solution)
Finished dev [unoptimized + debuginfo] target(s) in 1.67s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Станимира Влаева
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let mut cypher_bytes = cypher.to_be_bytes();
let mut encrypted_payload: Vec<u8> = Vec::new();
for (i, byte) in chunk.iter().enumerate() {
let encrypted_byte = byte ^ cypher_bytes[i];
cypher_bytes[i] = encrypted_byte;
}
}
return EncryptedPacket {
};
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let mut cypher_bytes = cypher.to_be_bytes();
let mut decrypted_payload: Vec<u8> = Vec::new();
for (i, byte) in chunk.iter().enumerate() {
let decrypted_byte = byte ^ cypher_bytes[i];
cypher_bytes[i] = *byte;
}
}
return Packet {
};
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-hqsdbu/solution)
Finished dev [unoptimized + debuginfo] target(s) in 1.85s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Иван Велков
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let mut byte_cypher = cypher.to_be_bytes();
let mut result: Vec<u8> = Vec::new();
for it in chunk.iter().zip(byte_cypher.iter_mut()) {
let (ai, bi) = it;
*bi = *ai ^ *bi;
result.push(*bi);
}
}
}
}
pub struct EncryptedPacket {
}
// kogato nqmash vreme i copy/paste-vash kato retard....
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let mut byte_cypher = cypher.to_be_bytes();
let mut result: Vec<u8> = Vec::new();
for it in chunk.iter().zip(byte_cypher.iter_mut()) {
let (ai, bi) = it;
result.push(*ai ^ *bi);
*bi = *ai;
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
let packet = Packet::new(b"Secret message, plz don't hack");
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-1qzso9a/solution)
Finished dev [unoptimized + debuginfo] target(s) in 2.01s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 1 test
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Моника Киркова
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let mut encrypted = Vec::new();
let mut newCypher = cypher.to_be_bytes();
let mut first;
if self.payload.len() < (i + 4){
}else{
}
newCypher.iter_mut()
.zip(first.iter())
.for_each(|(x1, x2)| *x1 ^= *x2);
for i in 0..first.len(){
encrypted.push(*newCypher.get(i).unwrap());
}
}
let encryptedPacket = EncryptedPacket {
};
(encryptedPacket)
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let mut decrypted = Vec::new();
let mut newCypher = cypher.to_be_bytes();
let mut first;
if &self.payload.len() < &(i + 4){
}else{
}
newCypher.iter_mut()
.zip(first.iter())
.for_each(|(x1, x2)| *x1 ^= *x2);
for i in 0..first.len(){
decrypted.push(*newCypher.get(i).unwrap());
}
newCypher.iter_mut()
.zip(first.iter())
.for_each(|(x1, x2)| *x1 = *x2);
}
let packet = Packet {
};
(packet)
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-12cmlq5/solution)
warning: variable newCypher should have a snake case name
--> src/lib.rs:12:17
|
12 |         let mut newCypher = cypher.to_be_bytes();
|                 ^^^^^^^^^ help: convert the identifier to snake case: new_cypher
|
= note: #[warn(non_snake_case)] on by default

warning: variable encryptedPacket should have a snake case name
--> src/lib.rs:28:13
|
28 |         let encryptedPacket = EncryptedPacket {
|             ^^^^^^^^^^^^^^^ help: convert the identifier to snake case: encrypted_packet

warning: variable newCypher should have a snake case name
--> src/lib.rs:43:17
|
43 |         let mut newCypher = cypher.to_be_bytes();
|                 ^^^^^^^^^ help: convert the identifier to snake case: new_cypher

warning: variable newCypher should have a snake case name
--> src/lib.rs:12:17
|
12 |         let mut newCypher = cypher.to_be_bytes();
|                 ^^^^^^^^^ help: convert the identifier to snake case: new_cypher
|
= note: #[warn(non_snake_case)] on by default

warning: variable encryptedPacket should have a snake case name
--> src/lib.rs:28:13
|
28 |         let encryptedPacket = EncryptedPacket {
|             ^^^^^^^^^^^^^^^ help: convert the identifier to snake case: encrypted_packet

warning: variable newCypher should have a snake case name
--> src/lib.rs:43:17
|
43 |         let mut newCypher = cypher.to_be_bytes();
|                 ^^^^^^^^^ help: convert the identifier to snake case: new_cypher

Finished dev [unoptimized + debuginfo] target(s) in 2.21s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
• Коректно
• 4 успешни тест(а)
• 0 неуспешни тест(а)
Георги Петков
type FourBytes = [u8; 4];
pub struct Packet {
}
impl Packet {
pub fn new(source: &[u8]) -> Self {
}
pub fn encrypt(&self, cypher: u32) -> EncryptedPacket {
let mut v: Vec<u8> = Vec::new();
let mut prev: FourBytes = cypher.to_be_bytes();
for (pos, e) in self.payload.iter().enumerate() {
let relative = pos%4;
let encrypted: u8 = prev[relative] ^ *e;
prev[relative] = encrypted;
v.push(encrypted);
}
}
}
pub struct EncryptedPacket {
}
impl EncryptedPacket {
pub fn decrypt(&self, cypher: u32) -> Packet {
let mut v: Vec<u8> = Vec::new();
let mut prev: FourBytes = cypher.to_be_bytes();
for (pos, e) in self.payload.iter().enumerate() {
let relative = pos%4;
let decrypted: u8 = prev[relative] ^ *e;
prev[relative] = *e;
v.push(decrypted);
}
}
}
Compiling solution v0.1.0 (/tmp/d20191217-2173579-mhsno4/solution)
Finished dev [unoptimized + debuginfo] target(s) in 1.77s
Running target/debug/deps/solution-28d77f5ef5bfec2e

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

running 4 tests
test solution_test::test_exact_cyphers ... ok
test solution_test::test_full_roundtrip ... ok
test solution_test::test_roundtrip_with_specific_length ... ok
test solution_test::test_several_iterations ... ok

test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Doc-tests solution

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out