On Tue, Oct 06, 2009, Reid Thompson wrote: > On Tue, 2009-10-06 at 10:44 -0500, Dwight Schauer wrote: > > http://stackoverflow.com/questions/918676/generate-sha-hash-in-openssl > > > > Replace SHA1 with SHA256. > > Replace 20 with SHA256_DIGEST_LENGTH. > > > > > > > Could someone point me to an example C program, docs that show how to > > generate a sha-256 digest for a buffer? > > Thanks, > I also ran across this > https://www.mirbsd.org/htman/i386/man3/sha2.htm > which has an example that works with minor tweaks > > > EXAMPLES > > > The following code fragment will calculate the SHA-256 digest for > the string "abc", which is > > "0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad". > > SHA256_CTX ctx; > u_int8_t results[SHA256_DIGEST_LENGTH]; > char *buf; > int n; > > buf = "abc"; > n = strlen(buf); > SHA256_Init(&ctx); > SHA256_Update(&ctx, (u_int8_t *)buf, n); > SHA256_Final(results, &ctx); > > /* Print the digest as one long hex value */ > printf("0x"); > for (n = 0; n < SHA256_DIGEST_LENGTH; n++) > printf("%02x", results[n]); > putchar('\n'); >
Both of these use the low level APIs which are deprecated. The approved technique is using EVP. http://www.openssl.org/docs/crypto/EVP_DigestInit.html#EXAMPLE Steve. -- Dr Stephen N. Henson. OpenSSL project core developer. Commercial tech support now available see: http://www.openssl.org ______________________________________________________________________ OpenSSL Project http://www.openssl.org User Support Mailing List openssl-users@openssl.org Automated List Manager majord...@openssl.org