Skip to content

Commit 822b137

Browse files
[C] closed out file for task 3, renamed folder for C .enc; [RS] created .enc ref files, table-testing for task 3
1 parent e39e9fb commit 822b137

20 files changed

Lines changed: 368 additions & 7 deletions

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
tests/output*/*
2-
./tema3
32
.vscode/

C-method/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
./ac-sql
2+

C-method/ac-sql

24 Bytes
Binary file not shown.

C-method/checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def check_task_3():
134134
continue
135135
print("Test: " + database)
136136
output_test = "../tests/output-C-method/task3/" + database.split(".")[0] + ".db.enc"
137-
ref_test = "../tests/ref/task3/" + database.split(".")[0] + ".db.enc.ref"
137+
ref_test = "../tests/ref/task3-C-method/" + database.split(".")[0] + ".db.enc.ref"
138138

139139
if os.system(f"cmp {output_test} {ref_test}") != 0:
140140
print(f"{database} - Rezultat incorect!")

C-method/src/task3.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,7 @@ void cripteaza_studenti(secretariat *secretariat, void *key, size_t key_len,
143143
for (size_t j = 0; j < block_length; j++)
144144
fwrite(&blocks[i][j], sizeof(unsigned char), 1, fout);
145145
}
146+
147+
fclose(fout);
146148
elibereaza_blocuri(&blocks);
147149
}

rust-method/Cargo.lock

Lines changed: 262 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust-method/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ path = "src/task2.rs"
99

1010

1111

12-
[dependencies]
12+
[dev-dependencies]
13+
rstest = "0.26.1"

rust-method/src/task2.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::queries::delete::delete;
33
use crate::queries::update::update;
44
use crate::task1::citeste_secretariat;
55

6+
67
pub mod structuri;
78
pub mod task1;
89
pub mod task3;
@@ -14,8 +15,9 @@ pub mod queries {
1415
pub mod update;
1516
}
1617

17-
pub mod tests {
18+
mod tests {
1819
pub mod test_task1;
20+
pub mod test_task3;
1921
}
2022

2123
use std::{env, process::exit};

rust-method/src/task3.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
use crate::structuri::*;
2-
2+
use std::fs::{File, create_dir_all};
3+
use std::io::Write;
4+
use std::path::Path;
35

46
const NR_BLOCKS: usize = 4;
57

8+
#[allow(dead_code)]
9+
fn print_hex_blocks(blocks: &[Vec<u8>]) {
10+
for i in 0..blocks.len() {
11+
print!("Blocul {}: ", i);
12+
for j in 0..blocks[i].len() {
13+
print!("0x{:02x}", blocks[i][j]);
14+
if j != blocks[i].len() - 1 {
15+
print!(" ");
16+
} else {
17+
println!("")
18+
}
19+
}
20+
}
21+
}
22+
623
fn xor(block: &[u8], key: &[u8]) -> Vec<u8> {
724
block
825
.iter()
@@ -66,7 +83,42 @@ fn split_into_blocks(secretariat: &Secretariat) -> Vec<Vec<u8>> {
6683
}
6784

6885

69-
pub fn cripteaza_studenti(secretariat: &Secretariat, key: &str, iv: &str, _cale_output: &str) {
86+
87+
fn fwrite_blocks(blocks: &[Vec<u8>], cale_output: &str) {
88+
let path = Path::new(cale_output);
89+
90+
// Creeaza directoarele parinte daca nu exista
91+
if let Some(parent) = path.parent() {
92+
if let Err(e) = create_dir_all(parent) {
93+
eprintln!("[EROARE] la crearea directoarelor {}: {}", parent.display(), e);
94+
return;
95+
}
96+
}
97+
98+
// Deschide fisierul pentru scriere
99+
let mut file = match File::create(&path) {
100+
Ok(f) => f,
101+
Err(e) => {
102+
eprintln!("[EROARE] la deschiderea fisierului {}: {}", path.display(), e);
103+
return;
104+
}
105+
};
106+
107+
// Scrie fiecare bloc, byte cu byte
108+
for block in blocks {
109+
if let Err(e) = file.write_all(&block) {
110+
eprintln!("[EROARE] la scrierea unui bloc: {}", e);
111+
return;
112+
}
113+
}
114+
115+
// Flush pentru a te asigura ca tot ce e scris ajunge pe disc
116+
if let Err(e) = file.flush() {
117+
eprintln!("[EROARE] la scrierea in fisier: {}", e);
118+
}
119+
}
120+
121+
pub fn cripteaza_studenti(secretariat: &Secretariat, key: &str, iv: &str, cale_output: &str) {
70122
let mut blocks: Vec<Vec<u8>> = split_into_blocks(secretariat);
71123

72124
let bytes_key: Vec<u8> = key.as_bytes().to_vec();
@@ -81,4 +133,6 @@ pub fn cripteaza_studenti(secretariat: &Secretariat, key: &str, iv: &str, _cale_
81133
blocks[i] = xor(&blocks[i], &bytes_key);
82134
blocks[i] = p_box(&blocks[i]);
83135
}
84-
}
136+
137+
fwrite_blocks(&blocks, cale_output);
138+
}

0 commit comments

Comments
 (0)