When I calculate the sha256-digest of a file with the openssl-command-line, then it is generated in c ++ OpenSL-Library is completely different from digest
Command line:
openssl dgst -binary -sha256 MyFile.txt
C ++:
Bio * Bio = Bio_NU_File ("mifile .t.tt.", "RB"); If (bio! = Null) {OpenSSL_add_all_digests (); Const EVP_MD * md = EVP_sha256 (); If (md! = NULL) {EVP_MD_CTX * ctx = EVP_MD_CTX_create (); If (EVP_DigestInit_ex (ctx, md, NULL)) {const int bufLength = 4096; Unsigned four buff [bufLength]; Unsigned int lane; EVP_DigestUpdate (ctx, buf, bufLength); (bio_pread (bio, buff, buff lang)); Unsigned four digest [EVP_MAX_MD_SIZE]; Int ok = EVP_DigestFinal_ex (CTX, Digest, and LAN); // OK == 1; In the variable "digest" the digest is completely different from the calculated on the command-line) EVP_MD_CTX_cleanup (ctx); BIO_free_all (bio); }}
Compared to the calculation on the command line, why is Digest Calciate through C ++? (There is no line-break or white space in MyFile.txt)
Merciful, Matthias
With this codeback you handle wrong reading, hash is correct:
int readlen; While ((readlen = BIO_read (bio, buf, bufLength)) gt; {EVP_DigestUpdate (ctx, buf, readlen);}
In general, to make your code stronger Should deal with more errors.
Cheers,
/ Eric Alpay
Comments
Post a Comment