tp1
This commit is contained in:
1
tp1/ex3/fichier1.txt
Normal file
1
tp1/ex3/fichier1.txt
Normal file
@@ -0,0 +1 @@
|
||||
je ne sais pa
|
BIN
tp1/ex3/hachage
Executable file
BIN
tp1/ex3/hachage
Executable file
Binary file not shown.
62
tp1/ex3/hachage.c
Normal file
62
tp1/ex3/hachage.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DELTA 0x9e3779b9
|
||||
|
||||
void xtea_encrypt(uint32_t v[2], uint32_t const key[4]) {
|
||||
uint32_t v0 = v[0], v1 = v[1], sum = 0;
|
||||
for (int i = 0; i < 32; i++) {
|
||||
v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + key[sum & 3]);
|
||||
sum += DELTA;
|
||||
v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + key[(sum >> 11) & 3]);
|
||||
}
|
||||
v[0] = v0;
|
||||
v[1] = v1;
|
||||
}
|
||||
|
||||
void hash_file(const char *filename) {
|
||||
FILE *f = fopen(filename, "rb");
|
||||
if (!f) {
|
||||
perror("Erreur ouverture fichier");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t buffer[24];
|
||||
uint64_t final_hash[1] = {0};
|
||||
size_t bytes_read;
|
||||
|
||||
while ((bytes_read = fread(buffer, 1, 24, f)) > 0) {
|
||||
if (bytes_read < 24) {
|
||||
memset(buffer + bytes_read, 24 - bytes_read, 24 - bytes_read);
|
||||
}
|
||||
|
||||
uint32_t x[2];
|
||||
memcpy(x, buffer, 8);
|
||||
|
||||
uint32_t key[4];
|
||||
memcpy(key, buffer + 8, 16);
|
||||
|
||||
uint32_t x_orig[2] = {x[0], x[1]};
|
||||
xtea_encrypt(x, key);
|
||||
|
||||
x[0] ^= x_orig[0];
|
||||
x[1] ^= x_orig[1];
|
||||
|
||||
final_hash[0] ^= ((uint64_t)x[0] << 32) | x[1];
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
|
||||
printf("Empreinte (hachage) : %016llx\n", (unsigned long long)final_hash[0]);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc != 2) {
|
||||
printf("Usage : %s <fichier>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
hash_file(argv[1]);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user