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参数就是输入的明文数据了
本文揭示了直接使用md5(), sha1(), sha256()函数的误区,强调了EVP_Digest系列函数在实际应用中的重要性,并介绍了RSA_sign(), RSA_verify()的正确用法,涉及EVP_DigestSignInit和EVP_DigestVerifyInit等高级API。
4109

被折叠的 条评论
为什么被折叠?



