Skip to content

Node incompatibility for "sha256" algorithm #84

Open
@zenshixd

Description

Hello,

there seems to be some differences between this library and Node.js, consider following code:

const bCrypto = require('./browser');
const nCrypto = require('crypto');

const key = `-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAvvjcmEFSc9CYHZ8gN5phm9xBDxEXbM/8K62Rw5TpAENI3RZr
7Ua570EL3GEJBFM5IfWjn4zlEoXdLIM4Rj4mC9TQdxsmRYr9zGL8r9/jDTwwPTTk
ApGTZWJg5og2IrVaKByXnJHQGtHEkgNS0ay3JnL8TEOUr75WujU8YRPoBCnSnFtN
vhGRgrp+4QOROtP5pW85FmQKTkYK1s0bdlJM4YCDyicFPuqo8d2+pn8uXHcmHq7C
2QJwVEOSESpSEKwwVpZ2jQws7QRb5+ELaRdH2QQQ239FZRhVrEMsNf3AOSXcNULl
W+Mp6rbwY9+xM84g5pKgGJ80i3LDqsPvdvMAcwIDAQABAoIBAB8JvHSkfT2kg+yg
jbBrz3xw9kP4H6oIPbBto9i5TYtE5EVGnSDx00tu83oMbVY+HnWGAmNul1aE2jcf
VpiYBj/7BSyuhXBdEDXVSsNc7H6cryxreblvJpkePaiKL95BQldtmjiQvFV3jW67
yoyYeSXvH1FX1IUhtt/Jwjf278kpKIhfTmshlRaSUavjZWYv4kj7qOURUTkXuZrF
yfy3jyid1lRmLzOOGsNs9EMjNMdbThLYOmhrkrMr4dM7jWby850MMTXMJxIcTlAJ
SlhPONvvJfS2D49dKVPBxMMh3DoBGsOcd6lZiy3VSRBrUHXFyI98pH7Y+2NHCjjw
SApfDI0CgYEA+raJ+kkLooZ+Sf+0chVCRlr/6XmEcvJk6VDMV2OP/2AJpWLOZLVY
R2fkC4qtnW4MTBM191l+qarPooWU2ZvAcVmP/A9341kTcawdg1NPB7GDsNEum8yL
9JVc8FU0cF4UU39U7JjoQ1rwcMhFVg8qVogyOZ/qvBY5ddJncEzFGl8CgYEAwv/Q
g40OrTxx2FHMpS7vh8rHU7qrsclhX2AdnNlvt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QXh0a0N3U2pKUEN1OWN4WmdoU0dYWFFEM3dncm8wYl9vbmN2d1V1cjhaeG10U09pRmJQVElOZWUwUVlMWEg3ak12cVhKN3VzMTcxSU5jNDBhOXU1WDJoa3hGMVFBeWdOYmx2RVJxWHo2V2otNjR3TEhIMnBZNk9QaVFST2Rub0VlQ29DNEhXMjNqNlNDckZXTlNOei1DcUxTZWtYM0Z3TGZUMk55Z3l5N1pEeWFPZHhobFRPa0NxdUlJVGFHMF81Qjl1a1ZHRW9yQ0E5RmhYb20yRk50b1J3c0I3eUFtdkpDQ3B4Q1lUTmFuZ3VWYmVmU3luYS14VWR1ZHcifX0.eyJ0ZXJtc09mU2VydmljZUFncmVlZCI6dHJ1ZSwiY29udGFjdCI6WyJtYWlsdG86ZW1haWxAZXhhbXBsZS5jb20iXX0";

let nData = nCrypto.createSign('sha256WithRSAEncryption').update(txt).sign(p);
console.log('node', nData);
let bData = bCrypto.createSign('sha256WithRSAEncryption').update(txt).sign(p);
console.log('browserify-sign', bData);
console.log('isEqual', nData.toString('hex') === bData.toString('hex'));

nData = nCrypto.createSign('sha256').update(txt).sign(p);
console.log('node', nData);
bData = bCrypto.createSign('sha256').update(txt).sign(p);
console.log('browserify-sign', bData);
console.log('isEqual', nData.toString('hex') === bData.toString('hex'));

For sha256WithRSAEncryption both Node.js and browserify-sign are correctly signing the text using provided private key, but for sha256 only Node.js is able to sign a text, while browserify-sign` throws an error:

Error: wrong private key type
    at sign (/home/ubuntu/Projects/browserify-sign/browser/sign.js:24:63)
    at Sign.signMethod [as sign] (/home/ubuntu/Projects/browserify-sign/browser/index.js:43:13)
    at Object.<anonymous> (/home/ubuntu/Projects/browserify-sign/test-bsign.js:45:50)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

From my initial investigation, its because sha256 algorithm expects only ECDSA key, but we are providing RSA key instead.

Is it expected? Since browserify-sign wants to duplicate node behaviour, I think its a bug.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions