-3

C++ where can i get the code example sha256 openssl in C++

I search everywhere, i only get part of the code.

I can do it by at my terminal typing this

echo -n "compute sha256" | openssl sha256

However, i want to perform sha256 in .cpp file, how can i do it.

I know i need to include openssl library.

4

1 回答 1

2

SHA256 函数位于<openssl/sha.h>

int SHA256_Init(SHA256_CTX *c);
int SHA256_Update(SHA256_CTX *c, const void *data, size_t len);
int SHA256_Final(unsigned char *md, SHA256_CTX *c);

它们的使用是不言自明的(省略了错误处理):

SHA256_CTX ctx = { 0 };

// Initialize the SHA256 context
SHA256_Init(&ctx);

// Run all of the data through calls to SHA256_Update() using the context object, `ctx`
// ...

// Compute the SHA256 digest.
unsigned char md[32];
SHA256_Final(md, &ctx);
于 2012-08-19T20:38:46.233 回答