SHA256签名与RSA-SHA256签名之间的区别 [英] Difference between signing with SHA256 vs. signing with RSA-SHA256

查看:2119
本文介绍了SHA256签名与RSA-SHA256签名之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用node.js玩数字签名。为了进行测试,我创建了一些XML数据的数字签名,首先仅使用SHA256,然后使用RSA-SHA256。

I play with digital signatures using node.js. For test purpose, I created a digital signature of some XML data, first using only SHA256, then using RSA-SHA256.

让我感到困惑的是,两种签名方式都会创建完全相同的签名。这两个签名是相同的。如果它们相同,那么为什么要使用两种不同的方法(SHA256与RSA-SHA256)?

The thing that puzzles me is that both methods of signing create exactly the same signature. Both signatures are identical. If they're identical, then why two different methods (SHA256 vs. RSA-SHA256)?

我在下面添加了代码:

var crypto = require('crypto'),
    path   = require('path'),
    fs     = require('fs'),

    pkey_path = path.normalize('private_key.pem'),
    pkey = '';

function testSignature(pkey) {
    var sign1 = crypto.createSign('RSA-SHA256'),
        sign2 = crypto.createSign('SHA256');

    fs.ReadStream('some_document.xml')
        .on('data', function (d) {
            sign1.update(d);
            sign2.update(d);
        })
        .on('end', function () {
            var s1 = sign1.sign(pkey, "base64"),
                s2 = sign2.sign(pkey, "base64");

            console.log(s1);
            console.log(s2);
        });
}

// You need to read private key into a string and pass it to crypto module.
// If the key is password protected, program execution will stop and
// a prompt will appear in console, awaiting input of password.

testSignature(fs.readFileSync(pkey_path));

上面的代码输出一些字符串,这是签名,然后再输出完全相同的字符串,这也是相同数据的签名,但使用了-据说-是不同的算法创建的,但是与先前的算法相同...

The code above outputs some string, which is the signature, and then again exactly the same string, which is also a signature of the same data, but created with - supposedly - different algorithm, yet it's identical with previous one...

推荐答案

不能单独通过SHA256创建签名。

A signature cannot be created by SHA256 alone.

SHA256是一种哈希算法;即创建短指纹号的算法,该指纹号代表任意大量的数据。为了产生签名,仍然必须以某种方式处理此指纹,以允许识别某些私人签名密钥的持有者。一种这样的处理方法是使用rsa密钥对的私钥对指纹进行加密,从而允许其他人使用关联的公钥解密结果,并验证私钥的持有者确实必须是签名者。

SHA256 is a hashing algorithm; i.e. an algorithm creating a short fingerprint number representing an arbitrary large amount of data. To produce a signature, this fingerprint still has to be treated somehow to allow identification of the holder of some private signature key. One such treatment is to encrypt the fingerprint using the private key of a rsa key pair allowing others to decrypt the result using the associated public key and so verify that the keeper of the private key indeed must have been the signer.

在加密API的上下文中,当未显式指定处理时,RSA加密方案是默认处理,或者从您用作参数的私钥中推导出处理的类型 sign 调用---如果它是RSA私钥,则使用RSA;如果它是DSA密钥,则使用DSA; ...

In the context of your crypto API that RSA encryption scheme either is the default treatment when the treatment is not explicitly named, or the kind of treatment is deduced from the private key you use as parameter in the sign call --- if it is a RSA private key, it uses RSA; if it is a DSA key, it uses DSA; ...

这篇关于SHA256签名与RSA-SHA256签名之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆