我目前正在尝试使用Java Security API和BouncyCastle生成主题哈希。
这是我在使用Openssl库时所做的事情:
openssl x509 -in /Users/Sn0wfreezeDev/Downloads/Test.pem -hash
这会产生一个短的8位哈希 1817886a
这是我的Java代码
X509Certificate cert = CertManager.getCertificate(number, c);
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
System.out.println(" Subject " + cert.getSubjectDN());
System.out.println(" Issuer " + cert.getIssuerDN());
sha1.update(cert.getSubjectDN().getName().getBytes());
String hexString = bytesToHex(sha1.digest());
System.out.println(" sha1 " + hexString);
System.out.println();
答案 0 :(得分:5)
这会产生一个短的8位数哈希1817886a
OpenSSL有两种形式:
$ cd openssl-1.0.2-src
$ grep -R X509_subject_name_hash *
...
crypto/x509/x509.h:unsigned long X509_subject_name_hash(X509 *x);
crypto/x509/x509.h:unsigned long X509_subject_name_hash_old(X509 *x);
crypto/x509/x509_cmp.c:unsigned long X509_subject_name_hash(X509 *x)
crypto/x509/x509_cmp.c:unsigned long X509_subject_name_hash_old(X509 *x)
...
在Java中生成X509Certificate的主题哈希...
以下是crypto/x509/x509_cmp.c
的来源:
unsigned long X509_subject_name_hash(X509 *x)
{
return (X509_NAME_hash(x->cert_info->subject));
}
#ifndef OPENSSL_NO_MD5
unsigned long X509_subject_name_hash_old(X509 *x)
{
return (X509_NAME_hash_old(x->cert_info->subject));
}
#endif
最后:
unsigned long X509_NAME_hash(X509_NAME *x)
{
unsigned long ret = 0;
unsigned char md[SHA_DIGEST_LENGTH];
/* Make sure X509_NAME structure contains valid cached encoding */
i2d_X509_NAME(x, NULL);
if (!EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, EVP_sha1(),
NULL))
return 0;
ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
) & 0xffffffffL;
return (ret);
}
#ifndef OPENSSL_NO_MD5
unsigned long X509_NAME_hash_old(X509_NAME *x)
{
EVP_MD_CTX md_ctx;
unsigned long ret = 0;
unsigned char md[16];
/* Make sure X509_NAME structure contains valid cached encoding */
i2d_X509_NAME(x, NULL);
EVP_MD_CTX_init(&md_ctx);
EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
if (EVP_DigestInit_ex(&md_ctx, EVP_md5(), NULL)
&& EVP_DigestUpdate(&md_ctx, x->bytes->data, x->bytes->length)
&& EVP_DigestFinal_ex(&md_ctx, md, NULL))
ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) |
((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)
) & 0xffffffffL;
EVP_MD_CTX_cleanup(&md_ctx);
return (ret);
}
#endif
i2d_X509_NAME
使用RFC 2459(和其他地方)将X509_NAME
编码为标准表示。例如,它用于证书主题和发行人名称。
您可以使用openssl x509 -in <cert> -text -noout
等命令查看OpenSSL用于名称字符串的内容。它看起来与C=US, ST=California, L=Mountain View, O=Google Inc, CN=www.google.com
类似(取自Google证书)。
在Java中生成X509Certificate的主题哈希...
在大图中,您正在生成Subject的Distinguished Name字符串的哈希并返回unsigned long。 unsigned long实际上是一个截断的哈希值。
X509_subject_name_hash
使用SHA-1,X509_subject_name_hash_old
使用MD5。
(评论)......他们如何使用sha1哈希来生成短哈希
OpenSSL提供截断哈希的十六进制编码。整个哈希值在md
中。 md
将是16个字节(MD5)或20个字节(SHA-1)。
通过选择字节[0,3]
以及md[0]
,md[1]
,md[2]
和md[3]
上的位操作进行截断。
8位数字来自16位字节的十六进制编码。