search
LoginSignup
5

More than 5 years have passed since last update.

posted at

node.jsで秘密鍵で署名して公開鍵で検証する

var cryptor = require('crypto');
var sign = cryptor.createSign('RSA-SHA256');
sign.update('some data to sign');
var privateKey = fs.readFileSync(path.resolve('./signkey/secret.key'), "utf8");
var signedData = sign.sign(privateKey, 'base64');

var verify = cryptor.createVerify('RSA-SHA256');
verify.write('some data to sign');
verify.end();
var publicKey = fs.readFileSync(path.resolve('./signkey/public.key'), "utf8");
console.log(verify.verify(publicKey, signedData, 'base64'));

sign時とverify時でフォーマットをbase64ならbase64で合わせないと検証に失敗します。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
5