refactor: Hashing function now streams data into hasher instead of doing it in one go.
parent
4c9b429eaa
commit
b6d97df1bc
|
@ -123,20 +123,21 @@ vieter_package_error vieter_package_read_archive(vieter_package *pkg, const char
|
|||
|
||||
void vieter_package_sha256sum(vieter_package *pkg, char *res) {
|
||||
FILE *f = fopen(pkg->path, "r");
|
||||
fseek(f, 0, SEEK_END);
|
||||
size_t size = ftell(f);
|
||||
rewind(f);
|
||||
unsigned char *in = malloc(size);
|
||||
fread(in, 1, size, f);
|
||||
fclose(f);
|
||||
// Try to read 100KiB at a time
|
||||
unsigned char *in = malloc(102400);
|
||||
// Actual number of bytes read
|
||||
size_t read_size;
|
||||
|
||||
unsigned char hash[32];
|
||||
SHA256_CTX *ctx = malloc(sizeof(SHA256_CTX));
|
||||
|
||||
sha256_init(ctx);
|
||||
sha256_update(ctx, in, size);
|
||||
while ((read_size = fread(in, 1, 102400, f)) != 0) {
|
||||
sha256_update(ctx, in, read_size);
|
||||
}
|
||||
unsigned char hash[SHA256_BLOCK_SIZE];
|
||||
|
||||
sha256_final(ctx, hash);
|
||||
|
||||
fclose(f);
|
||||
free(in);
|
||||
free(ctx);
|
||||
|
||||
|
@ -147,8 +148,8 @@ void vieter_package_sha256sum(vieter_package *pkg, char *res) {
|
|||
unsigned int half_byte = 0;
|
||||
int j = 0;
|
||||
|
||||
// We advance in the string 2 bytes for every one byte of the hash
|
||||
for (int i = 0; i < 32; i++) {
|
||||
// We advance 2 bytes in the string for every one byte of the hash
|
||||
for (int i = 0; i < SHA256_BLOCK_SIZE; i++) {
|
||||
// We transform the first half byte into the second character to keep
|
||||
// each byte from becoming reversed in the final string
|
||||
half_byte = hash[i] & 0b1111;
|
||||
|
@ -206,11 +207,11 @@ char *vieter_package_to_description(vieter_package *pkg) {
|
|||
ADD_STRING("\n\n%%CSIZE%%\n%ld", csize);
|
||||
ADD_STRING("\n\n%%ISIZE%%\n%ld", size);
|
||||
|
||||
char checksum[65];
|
||||
char checksum[SHA256_BLOCK_SIZE * 2 + 1];
|
||||
vieter_package_sha256sum(pkg, checksum);
|
||||
|
||||
snprintf(aux, SMALL_BUFF_SIZE, "\n\n%%SHA256SUM%%\n%s", checksum);
|
||||
if (buff_size < strlen(description) + SMALL_BUFF_SIZE + 1) {
|
||||
snprintf(aux, small_buff_size, "\n\n%%SHA256SUM%%\n%s", checksum);
|
||||
if (buff_size < strlen(description) + small_buff_size + 1) {
|
||||
description = realloc(description, buff_size * 2);
|
||||
buff_size *= 2;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue