【发布时间】:2015-05-05 17:06:30
【问题描述】:
我目前正在开发一个应用程序,它使用 openssl 库 (libcrypto) 来生成证书。现在我必须得到一个已经存在的证书的哈希值。
当我使用终端时,我可以使用
生成哈希值openssl x509 -hash -in cert.pem -noout
输出:01da0e2b
这是我的代码,我尝试使用 C 中的库生成哈希值。
X509 *cert = NULL;
FILE *fp = fopen(currentCert.UTF8String, "r");
PEM_read_X509(fp, &cert, NULL, NULL);
long hash = X509_subject_name_hash(cert);
char *mdString = malloc(sizeof(long));
sprintf(mdString, "%lx",hash);
printf(mdString);
输出:1817886a
但实际上我的输出是不同的。有人知道我在做什么错吗?
【问题讨论】:
-
只用
printf("0x%08lx",hash),不明白是什么原因将其序列化为char *,这也是不正确的,因为缓冲区的大小应该取决于位数
标签: c hash openssl x509 libcrypto