散列 RSA 密钥的标准方法?

发布于 2024-11-17 01:34:47 字数 179 浏览 4 评论 0原文

创建 RSA 公钥的散列(sha-1 或 MD5)的算法是什么?有没有标准的方法来做到这一点?只需对两者的模数和字符串加法进行哈希处理,然后再进行哈希处理?通常使用SHA-1还是MD5?

我想用它来确保我获得正确的密钥(让发送者发送一个哈希值,然后我自己计算它),并记录所述哈希值,以便我始终知道在加密有效负载时使用的确切密钥。

What's the algorithm for creating hash (sha-1 or MD5) of an RSA public key? Is there a standard way to do this? Hash just the modulus, string addition of both and then take a hash? Is SHA-1 or MD5 usually used?

I want to use it to ensure that I got the right key (have the sender send a hash, and I calculate it myself), and log said hash so I always know which exact key I used when I encrypt the payload.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箜明 2024-11-24 01:34:47

根据 OpenSSH 源代码,为 RSA 密钥生成指纹的方式是将公钥中的 n 和 e 转换为大端二进制数据,连接数据,然后使用给定的哈希函数对该数据进行哈希处理。

部分 OpenSSH 源代码如下。添加评论是为了澄清正在发生的事情。

// from key_fingerprint_raw() in key.c
switch (k->type) {
case KEY_RSA1:
    // figure out how long n and e will be in binary form
    nlen = BN_num_bytes(k->rsa->n);
    elen = BN_num_bytes(k->rsa->e);
    len = nlen + elen;
    // allocate space for n and e and copy the binary data into blob
    blob = xmalloc(len);
    BN_bn2bin(k->rsa->n, blob);
    BN_bn2bin(k->rsa->e, blob + nlen);

...

// pick a digest to use
switch (dgst_type) {
case SSH_FP_MD5:
    md = EVP_md5();
    break;
case SSH_FP_SHA1:
    md = EVP_sha1();
    break;

...

// hash the data in blob (n and e)
EVP_DigestInit(&ctx, md);
EVP_DigestUpdate(&ctx, blob, len);
EVP_DigestFinal(&ctx, retval, dgst_raw_length);

来自 BN_bn2bin 手册页

BN_bn2bin (a, to)a 的绝对值转换为big-endian 形式并将其存储在to 中。 to 必须指向内存的 BN_num_bytes(a) 字节。

Based on the OpenSSH source code, the way that a fingerprint is generated for RSA keys is to convert n and e from the public key to big-endian binary data, concatenate the data and then hash that data with the given hash function.

Portions of the OpenSSH source code follows. The comments were added to clarify what is happening.

// from key_fingerprint_raw() in key.c
switch (k->type) {
case KEY_RSA1:
    // figure out how long n and e will be in binary form
    nlen = BN_num_bytes(k->rsa->n);
    elen = BN_num_bytes(k->rsa->e);
    len = nlen + elen;
    // allocate space for n and e and copy the binary data into blob
    blob = xmalloc(len);
    BN_bn2bin(k->rsa->n, blob);
    BN_bn2bin(k->rsa->e, blob + nlen);

...

// pick a digest to use
switch (dgst_type) {
case SSH_FP_MD5:
    md = EVP_md5();
    break;
case SSH_FP_SHA1:
    md = EVP_sha1();
    break;

...

// hash the data in blob (n and e)
EVP_DigestInit(&ctx, md);
EVP_DigestUpdate(&ctx, blob, len);
EVP_DigestFinal(&ctx, retval, dgst_raw_length);

From the BN_bn2bin manual page:

BN_bn2bin(a, to) converts the absolute value of a into big-endian form and stores it at to. to must point to BN_num_bytes(a) bytes of memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文