使用 openssl 进行哈希计算

版本:OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)

SHAx 系列

如果对象完全存储在内存中,可以使用以下函数:

#include 

unsigned char *SHA1(const unsigned char *data, size_t count, unsigned char *md_buf);
unsigned char *SHA224(const unsigned char *data, size_t count, unsigned char *md_buf);
unsigned char *SHA256(const unsigned char *data, size_t count, unsigned char *md_buf);
unsigned char *SHA384(const unsigned char *data, size_t count, unsigned char *md_buf);
unsigned char *SHA512(const unsigned char *data, size_t count, unsigned char *md_buf);

使用示例:

int i = 0;
const char *data = "Hello World";
unsigned char sha1[SHA_DIGEST_LENGTH];

SHA1(data, strlen(data), sha1);

printf("%s sha1 = ", data);
for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
    printf("%02x", sha1[i]);
}

SHAx_Init 系列

如果对象未完全存储在内存中,可以使用以下函数(注意:以下函数已经在 3.0 版本中废弃):

int SHA1_Init(SHA_CTX *c);
int SHA1_Update(SHA_CTX *c, const void *data, size_t len);
int SHA1_Final(unsigned char *md, SHA_CTX *c);

int SHA224_Init(SHA256_CTX *c);
int SHA224_Update(SHA256_CTX *c, const void *data, size_t len);
int SHA224_Final(unsigned char *md, SHA256_CTX *c);

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);

int SHA384_Init(SHA512_CTX *c);
int SHA384_Update(SHA512_CTX *c, const void *data, size_t len);
int SHA384_Final(unsigned char *md, SHA512_CTX *c);

int SHA512_Init(SHA512_CTX *c);
int SHA512_Update(SHA512_CTX *c, const void *data, size_t len);
int SHA512_Final(unsigned char *md, SHA512_CTX *c);

使用示例:


#define OPENSSL_API_COMPAT 0x10100000L /* 在 >= 3.0 版本中需要定义该宏才能使用 SHAx_Init 系列函数,该宏详情见 https://www.openssl.org/docs/man3.2/man7/openssl_user_macros.html */

SHA_CTX c;
int i = 0;
const char *data = "Hello World";
unsigned char sha1[SHA_DIGEST_LENGTH];

SHA1_Init(&c);
SHA1_Update(&c, data, strlen(data));
SHA1_Final(sha1, &c);

printf("%s sha1 = ", data);
for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
    printf("%02x", sha1[i]);
}

 

#define OPENSSL_API_COMPAT 0x10100000L /* 在 >= 3.0 版本中需要定义该宏才能使用 SHAx_Init 系列函数,该宏详情见 https://www.openssl.org/docs/man3.2/man7/openssl_user_macros.html */

int sha1_file(char *file)
{
#define READ_SIZE 128

    if (!file)
        return 0;

    int i = 0;
    int n = 0;
    SHA_CTX c;

    FILE *fp = NULL;
    unsigned char temp[READ_SIZE];
    unsigned char sha1[SHA_DIGEST_LENGTH];

    fp = fopen(file, "r");
    if (!fp)
        return -1;

    SHA1_Init(&c);

    do
    {
        memset(temp, 0, sizeof(temp));
        n = fread(temp, 1, READ_SIZE, fp);
        if (n > 0)
            SHA1_Update(&c, temp, n);
    } while (n > 0);

    SHA1_Final(sha1, &c);
    printf("file [%s] sha1 = ", file);
    for (i = 0; i < SHA_DIGEST_LENGTH; i++)
    {
        printf("%02x", sha1[i]);
    }
    fclose(fp);
    return 0;
}

3.0 版本

3.0 版本推荐使用用 EVP_DigestInit_ex(3), EVP_DigestUpdate(3) , EVP_DigestFinal_ex(3)

使用示例:

int i = 0;
EVP_MD_CTX *ctx = NULL;
const char *data = "Hello World";
unsigned char sha1[SHA_DIGEST_LENGTH];

ctx = EVP_MD_CTX_new();
EVP_DigestInit_ex(ctx, EVP_sha1(), NULL);
EVP_DigestUpdate(ctx, data, strlen(data));
EVP_DigestFinal_ex(ctx, sha1, NULL);
EVP_MD_CTX_free(ctx);

printf("%s sha1 = ", data);
for (i = 0; i < SHA_DIGEST_LENGTH; i++)
{
    printf("%02x", sha1[i]);
}

快速哈希功能

EVP_Q_digest(3)

int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
                 const void *data, size_t datalen,
                 unsigned char *md, size_t *mdlen);

 使用示例:

const char *data = "Hello World";
unsigned char sha1[SHA_DIGEST_LENGTH];

EVP_Q_digest(NULL, "SHA1", NULL, data, strlen(data), sha1, NULL)

你可能感兴趣的:(openssl,哈希算法,openssl)