Estoy intentando escribir una función C usando OpenSSL/libcrypto para calcular la suma SHA256 de un archivo. Estoy basando mi código en el ejemplo de C++ de Adam Lamer here.Calcular e imprimir el hash SHA256 de un archivo usando OpenSSL
Aquí está mi código:
int main (int argc, char** argv)
{
char calc_hash[65];
calc_sha256("file.txt", calc_hash);
}
int calc_sha256 (char* path, char output[65])
{
FILE* file = fopen(path, "rb");
if(!file) return -1;
char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
const int bufSize = 32768;
char* buffer = malloc(bufSize);
int bytesRead = 0;
if(!buffer) return -1;
while((bytesRead = fread(buffer, 1, bufSize, file)))
{
SHA256_Update(&sha256, buffer, bytesRead);
}
SHA256_Final(hash, &sha256);
sha256_hash_string(hash, output);
fclose(file);
free(buffer);
return 0;
}
void sha256_hash_string (char hash[SHA256_DIGEST_LENGTH], char outputBuffer[65])
{
int i = 0;
for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
}
outputBuffer[64] = 0;
}
está presente .... echar un vistazo a las sumas calculadas por debajo de un archivo de ejemplo El problema:
Known good SHA256: 6da032d0f859191f3ec46a89860694c61e65460d54f2f6760b033fa416b73866
Calc. by my code: 6dff32ffff59191f3eff6affff06ffff1e65460d54ffff760b033fff16ff3866
también consigo * pila destrozo detecta * cuando el código ha terminado de ejecutarse.
¿Alguien ve lo que estoy haciendo mal?
Gracias!
Tengo prototipos declarados. – dan6470
¿por qué const int bufSize se soluciona con el tamaño de 32768? podría ser más o menos –