SHA
SHA1
SHA224
SHA256
SHA384
SHA512
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <openssl/sha.h>
#include <openssl/crypto.h>
static char *s1 = "abc";
static int s1_len = 3;
char *ret_sha = "0164b8a914cd2a5e74c4f7ff082c4d97f1edf880";
char *ret_sha1 = "a9993e364706816aba3e25717850c26c9cd0d89d";
char *ret_sha224 = "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7";
char *ret_sha256 =
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
char *ret_sha384 =
"cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7";
char *ret_sha512 =
"ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
static char *pt(unsigned char *md, int len)
{
int i;
static char buf[256];
for (i = 0; i < len; i++)
sprintf(&(buf[i * 2]), "%02x", md[i]);
return (buf);
}
void sha(void)
{
SHA_CTX c;
unsigned char md[SHA_DIGEST_LENGTH];
char *p;
SHA((unsigned char *)s1, s1_len, md);
p = pt(md, SHA_DIGEST_LENGTH);
printf("SHA : %s\n", p);
SHA_Init(&c);
SHA_Update(&c, s1, s1_len);
SHA_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
p = pt(md, SHA_DIGEST_LENGTH);
printf("SHA : %s\n", p);
}
void sha1(void)
{
SHA_CTX c;
unsigned char md[SHA_DIGEST_LENGTH];
char *p;
SHA1((unsigned char *)s1, s1_len, md);
p = pt(md, SHA_DIGEST_LENGTH);
printf("SHA1 : %s\n", p);
SHA1_Init(&c);
SHA1_Update(&c, s1, s1_len);
SHA1_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
p = pt(md, SHA_DIGEST_LENGTH);
printf("SHA1 : %s\n", p);
}
void sha224(void)
{
SHA256_CTX c;
unsigned char md[SHA224_DIGEST_LENGTH];
char *p;
SHA224((unsigned char *)s1, s1_len, md);
p = pt(md, SHA224_DIGEST_LENGTH);
printf("SHA224 : %s\n", p);
SHA224_Init(&c);
SHA224_Update(&c, s1, s1_len);
SHA224_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
p = pt(md, SHA224_DIGEST_LENGTH);
printf("SHA224 : %s\n", p);
}
void sha256(void)
{
SHA256_CTX c;
unsigned char md[SHA256_DIGEST_LENGTH];
char *p;
SHA256((unsigned char *)s1, s1_len, md);
p = pt(md, SHA256_DIGEST_LENGTH);
printf("SHA256 : %s\n", p);
SHA256_Init(&c);
SHA256_Update(&c, s1, s1_len);
SHA256_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
p = pt(md, SHA256_DIGEST_LENGTH);
printf("SHA256 : %s\n", p);
}
void sha384(void)
{
SHA512_CTX c;
unsigned char md[SHA384_DIGEST_LENGTH];
char *p;
SHA384((unsigned char *)s1, s1_len, md);
p = pt(md, SHA384_DIGEST_LENGTH);
printf("SHA384 : %s\n", p);
SHA384_Init(&c);
SHA384_Update(&c, s1, s1_len);
SHA384_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
p = pt(md, SHA384_DIGEST_LENGTH);
printf("SHA384 : %s\n", p);
}
void sha512(void)
{
SHA512_CTX c;
unsigned char md[SHA512_DIGEST_LENGTH];
char *p;
SHA512((unsigned char *)s1, s1_len, md);
p = pt(md, SHA512_DIGEST_LENGTH);
printf("SHA512 : %s\n", p);
SHA512_Init(&c);
SHA512_Update(&c, s1, s1_len);
SHA512_Final(md, &c);
OPENSSL_cleanse(&c, sizeof(c));
p = pt(md, SHA512_DIGEST_LENGTH);
printf("SHA512 : %s\n", p);
}
int main(int argc, char *argv[])
{
if (argc > 1) {
s1 = argv[1];
}
s1_len = strlen(s1);
printf("calculating SHA* on '%s'\n", s1);
sha();
sha1();
sha224();
sha256();
sha384();
sha512();
return 0;
}
运行结果
$ gcc -Wall -lcrypto sha.c
$ ./a.out
calculating SHA* on 'abc'
SHA : 0164b8a914cd2a5e74c4f7ff082c4d97f1edf880
SHA : 0164b8a914cd2a5e74c4f7ff082c4d97f1edf880
SHA1 : a9993e364706816aba3e25717850c26c9cd0d89d
SHA1 : a9993e364706816aba3e25717850c26c9cd0d89d
SHA224 : 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7
SHA224 : 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7
SHA256 : ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
SHA256 : ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
SHA384 : cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7
SHA384 : cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7
SHA512 : ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f
SHA512 : ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f
$ ./a.out 123
calculating SHA* on '123'
SHA : 4f797c608cab4a00501447df8ee8833f363ea7d3
SHA : 4f797c608cab4a00501447df8ee8833f363ea7d3
SHA1 : 40bd001563085fc35165329ea1ff5c5ecbdbbeef
SHA1 : 40bd001563085fc35165329ea1ff5c5ecbdbbeef
SHA224 : 78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f
SHA224 : 78d8045d684abd2eece923758f3cd781489df3a48e1278982466017f
SHA256 : a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
SHA256 : a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
SHA384 : 9a0a82f0c0cf31470d7affede3406cc9aa8410671520b727044eda15b4c25532a9b5cd8aaf9cec4919d76255b6bfb00f
SHA384 : 9a0a82f0c0cf31470d7affede3406cc9aa8410671520b727044eda15b4c25532a9b5cd8aaf9cec4919d76255b6bfb00f
SHA512 : 3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2
SHA512 : 3c9909afec25354d551dae21590bb26e38d53f2173b8d3dc3eee4c047e7ab1c1eb8b85103e3be7ba613b31bb5c9c36214dc9f14a42fd7a2fdb84856bca5c44c2