forked from vieter-v/libvieter
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) {
|
void vieter_package_sha256sum(vieter_package *pkg, char *res) {
|
||||||
FILE *f = fopen(pkg->path, "r");
|
FILE *f = fopen(pkg->path, "r");
|
||||||
fseek(f, 0, SEEK_END);
|
// Try to read 100KiB at a time
|
||||||
size_t size = ftell(f);
|
unsigned char *in = malloc(102400);
|
||||||
rewind(f);
|
// Actual number of bytes read
|
||||||
unsigned char *in = malloc(size);
|
size_t read_size;
|
||||||
fread(in, 1, size, f);
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
unsigned char hash[32];
|
|
||||||
SHA256_CTX *ctx = malloc(sizeof(SHA256_CTX));
|
SHA256_CTX *ctx = malloc(sizeof(SHA256_CTX));
|
||||||
|
|
||||||
sha256_init(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);
|
sha256_final(ctx, hash);
|
||||||
|
|
||||||
|
fclose(f);
|
||||||
free(in);
|
free(in);
|
||||||
free(ctx);
|
free(ctx);
|
||||||
|
|
||||||
|
@ -147,8 +148,8 @@ void vieter_package_sha256sum(vieter_package *pkg, char *res) {
|
||||||
unsigned int half_byte = 0;
|
unsigned int half_byte = 0;
|
||||||
int j = 0;
|
int j = 0;
|
||||||
|
|
||||||
// We advance in the string 2 bytes for every one byte of the hash
|
// We advance 2 bytes in the string for every one byte of the hash
|
||||||
for (int i = 0; i < 32; i++) {
|
for (int i = 0; i < SHA256_BLOCK_SIZE; i++) {
|
||||||
// We transform the first half byte into the second character to keep
|
// We transform the first half byte into the second character to keep
|
||||||
// each byte from becoming reversed in the final string
|
// each byte from becoming reversed in the final string
|
||||||
half_byte = hash[i] & 0b1111;
|
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%%CSIZE%%\n%ld", csize);
|
||||||
ADD_STRING("\n\n%%ISIZE%%\n%ld", size);
|
ADD_STRING("\n\n%%ISIZE%%\n%ld", size);
|
||||||
|
|
||||||
char checksum[65];
|
char checksum[SHA256_BLOCK_SIZE * 2 + 1];
|
||||||
vieter_package_sha256sum(pkg, checksum);
|
vieter_package_sha256sum(pkg, checksum);
|
||||||
|
|
||||||
snprintf(aux, SMALL_BUFF_SIZE, "\n\n%%SHA256SUM%%\n%s", checksum);
|
snprintf(aux, small_buff_size, "\n\n%%SHA256SUM%%\n%s", checksum);
|
||||||
if (buff_size < strlen(description) + SMALL_BUFF_SIZE + 1) {
|
if (buff_size < strlen(description) + small_buff_size + 1) {
|
||||||
description = realloc(description, buff_size * 2);
|
description = realloc(description, buff_size * 2);
|
||||||
buff_size *= 2;
|
buff_size *= 2;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue