forked from KolibriOS/kolibrios
77 lines
1.9 KiB
C
77 lines
1.9 KiB
C
#include <conio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
#include <cryptal/sha1.h>
|
|
#include <cryptal/sha256.h>
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
if (con_init())
|
|
return 1; // Failed to initialize console
|
|
|
|
BYTE *file_buffer;
|
|
size_t bytes_read;
|
|
int read_status;
|
|
const char *program_path = argv[0];
|
|
(*con_set_title)("Hash task");
|
|
|
|
(*con_printf)("Calculating SHA hashes of the file: %s\n", program_path);
|
|
|
|
FILE *file = fopen(program_path, "rb");
|
|
|
|
fseek(file, 0, SEEK_END);
|
|
bytes_read = ftell(file);
|
|
fseek(file, 0, SEEK_SET);
|
|
|
|
file_buffer = (BYTE *)malloc(bytes_read);
|
|
if (!file_buffer)
|
|
{
|
|
(*con_printf)("Failed to allocate memory for file buffer\n");
|
|
fclose(file);
|
|
(*con_exit)(false);
|
|
return 1;
|
|
}
|
|
read_status = fread(file_buffer, 1, bytes_read, file);
|
|
if (read_status != bytes_read)
|
|
{
|
|
(*con_printf)("Failed to read file: %s\n", program_path);
|
|
free(file_buffer);
|
|
fclose(file);
|
|
(*con_exit)(false);
|
|
return 1;
|
|
}
|
|
fclose(file);
|
|
|
|
SHA1_CTX sha1_ctx;
|
|
BYTE sha1_hash[SHA1_BLOCK_SIZE];
|
|
sha1_init(&sha1_ctx);
|
|
sha1_update(&sha1_ctx, file_buffer, bytes_read);
|
|
sha1_final(&sha1_ctx, sha1_hash);
|
|
|
|
(*con_write_asciiz)("SHA1 hash of the program file:\n");
|
|
for (int i = 0; i < SHA1_BLOCK_SIZE; i++)
|
|
{
|
|
(*con_printf)("%02x", sha1_hash[i]);
|
|
}
|
|
(*con_write_asciiz)("\n");
|
|
|
|
SHA256_CTX sha256_ctx;
|
|
BYTE sha256_hash[SHA256_BLOCK_SIZE];
|
|
sha256_init(&sha256_ctx);
|
|
sha256_update(&sha256_ctx, file_buffer, bytes_read);
|
|
sha256_final(&sha256_ctx, sha256_hash);
|
|
free(file_buffer);
|
|
|
|
(*con_write_asciiz)("SHA256 hash of the program file:\n");
|
|
for (int i = 0; i < SHA256_BLOCK_SIZE; i++)
|
|
{
|
|
(*con_printf)("%02x", sha256_hash[i]);
|
|
}
|
|
(*con_write_asciiz)("\n");
|
|
|
|
(*con_exit)(false);
|
|
return 0;
|
|
}
|