openssl 几个函数运用注意

本文揭示了直接使用md5(), sha1(), sha256()函数的误区,强调了EVP_Digest系列函数在实际应用中的重要性,并介绍了RSA_sign(), RSA_verify()的正确用法,涉及EVP_DigestSignInit和EVP_DigestVerifyInit等高级API。

1、HASH的几个函数 md5(),sha1(),sha256()等几个函数,不能直接独立拿来用!这系列函数是库内部用的。实际运用中的HASH,要用openssl的摘要算法:

int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type)

int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t count)

int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *size)

想当然直接用了md5(),sha1(),sha256(),会坑死人,这是基础函数,错了,后面的都是错的。

openssl 文档:

Applications should use the higher level functions EVP_DigestInit(3) etc. instead of calling the hash functions directly.

2、RSA_sign(), RSA_verify()

这两个函数的声明:

 int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
    unsigned char *sigret, unsigned int *siglen, RSA *rsa);

 int RSA_verify(int type, const unsigned char *m, unsigned int m_len,
    unsigned char *sigbuf, unsigned int siglen, RSA *rsa);

用习惯了直接传入明文数据的方法,但是,这两个函数仅仅是签名和验签,不包括摘要,所以又是想当然错了。

m是摘要后的数据,不是明文数据!

自己实现签名和验签也容易:

int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
                        const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey);
 int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
 int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen);

 int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
                        const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey);
 int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
 int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, size_t siglen);

包括了摘要和签名,相关函数中的d参数就是输入的明文数据了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值