diff --git a/package.json b/package.json index 206f2c7..86f493b 100644 --- a/package.json +++ b/package.json @@ -53,4 +53,4 @@ "contributors": [ "Friedel Ziegelmayer " ] -} \ No newline at end of file +} diff --git a/src/ephemeral-keys.js b/src/ephemeral-keys.js index 53fa808..64ee2cc 100644 --- a/src/ephemeral-keys.js +++ b/src/ephemeral-keys.js @@ -30,7 +30,7 @@ module.exports = (curveName) => { } return { - key: priv.getPublic(), + key: new Buffer(priv.getPublic('hex'), 'hex'), genSharedKey } } diff --git a/src/keys/rsa.js b/src/keys/rsa.js index b127831..32a3a54 100644 --- a/src/keys/rsa.js +++ b/src/keys/rsa.js @@ -19,7 +19,11 @@ class RsaPublicKey { verify (data, sig) { const md = forge.md.sha256.create() - md.update(data, 'utf8') + if (Buffer.isBuffer(data)) { + md.update(data.toString('binary'), 'binary') + } else { + md.update(data) + } return this._key.verify(md.digest().bytes(), sig) } @@ -60,9 +64,13 @@ class RsaPrivateKey { sign (message) { const md = forge.md.sha256.create() - md.update(message, 'utf8') - - return this._privateKey.sign(md) + if (Buffer.isBuffer(message)) { + md.update(message.toString('binary'), 'binary') + } else { + md.update(message) + } + const raw = this._privateKey.sign(md, 'RSASSA-PKCS1-V1_5') + return new Buffer(raw, 'binary') } get public () { diff --git a/test/rsa.spec.js b/test/rsa.spec.js index 52fe924..eac20cf 100644 --- a/test/rsa.spec.js +++ b/test/rsa.spec.js @@ -112,4 +112,26 @@ describe('RSA', () => { }) }) }) + + it('sign and verify', () => { + const data = new Buffer('hello world') + const sig = key.sign(data) + + expect( + key.public.verify(data, sig) + ).to.be.eql( + true + ) + }) + + it('does fails to verify for different data', () => { + const data = new Buffer('hello world') + const sig = key.sign(data) + + expect( + key.public.verify(new Buffer('hello'), sig) + ).to.be.eql( + false + ) + }) })