java实现sha256电子签名,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.

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)?

I include code below:

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...

解决方案

A signature cannot be created by SHA256 alone.

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.

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; ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值