[C++] 纯文本查看 复制代码
#include <openssl/sha.h>
int SHA1_Init(SHA_CTX *c);
int SHA1_Update(SHA_CTX *c, const void *data,
unsigned long len);
int SHA1_Final(unsigned char *md, SHA_CTX *c);
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
static const char hex_chars[] = "0123456789abcdef";
void convert_hex(unsigned char *md, unsigned char *mdstr)
{
int i;
int j = 0;
unsigned int c;
for (i = 0; i < 20; i++) {
c = (md >> 4) & 0x0f;
mdstr[j++] = hex_chars[c];
mdstr[j++] = hex_chars[md & 0x0f];
}
mdstr[40] = '\0';
}
int main(int argc, char **argv)
{
SHA_CTX shactx;
char data[] = "hello groad.net";
char md[SHA_DIGEST_LENGTH];
char mdstr[40];
SHA1_Init(&shactx);
SHA1_Update(&shactx, data, 6);
SHA1_Update(&shactx, data+6, 9);
SHA1_Final(md, &shactx);
convert_hex(md, mdstr);
printf ("Result of SHA1 : %s\n", mdstr);
return 0;
}