From 2541ca8a9f4de1ba6865a31160b226f079f5c3ee Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 1 Aug 2016 04:44:55 -0400 Subject: [PATCH] document signJws --- README.md | 50 +++++++++++++++++++++++++++++++++++++++++++ node.js | 4 +++- tests/generate-sig.js | 2 +- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 28f3026..bd3b40d 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ API * `RSA.exportPublicPem(keypair)` * `RSA.exportPrivateJwk(keypair)` * `RSA.exportPublicJwk(keypair)` +* `RSA.signJws(keypair, payload, nonce)` `keypair` can be any object with any of these keys `publicKeyPem, privateKeyPem, publicKeyJwk, privateKeyJwk` @@ -94,3 +95,52 @@ RSA.generateKeypair(1024, 65537, { pem: false, public: false, internal: false }, , fingerprint: false // NOT IMPLEMENTED (RSA key fingerprint) } ``` + +### RSA.export*(keypair) + +You put in an object like `{ privateKeyPem: '...' }` or `{ publicKeyJwk: {} }` +and you get back the keys in the format you requested. + +Note: + +* Private keys **can** be used to export both private and public keys +* Public keys can **NOT** be used to generate private keys + +Example: + +```javascript +var keypair = { privateKeyPem: '...' }; + +keypair.publicKeyJwk = RSA.exportPublicJwk(keypair); + +console.log(keypair); +``` + +### RSA.signJws(keypair, payload, nonce) + +Generates a signature in JWS format. + +```javascript +var message = "Hello, World!" +var nonce = crypto.randomBytes(16).toString('hex'); +var jws = RSA.signJws(keypair, message, nonce); + +console.log(jws); +``` + +The result looks like this: + +```javascript +{ "header": { + "alg": "RS256", + "jwk": { + "kty": "RSA", + "n": "AMJubTfOtAarnJytLE8fhNsEI8wnpjRvBXGK/Kp0675J10ORzxyMLqzIZF3tcrUkKBrtdc79u4X0GocDUgukpfkY+2UPUS/GxehUYbYrJYWOLkoJWzxn7wfoo9X1JgvBMY6wHQnTKvnzZdkom2FMhGxkLaEUGDSfsNznTTZNBBg9", + "e": "AQAB" + } + }, + "protected": "eyJub25jZSI6IjhlZjU2MjRmNWVjOWQzZWYifQ", + "payload": "JLzF1NBNCV3kfbJ5sFaFyX94fJuL2H-IzaoBN-ciiHk", + "signature": "Wb2al5SDyh5gjmkV79MK9m3sfNBBPjntSKor-34BBoGwr6n8qEnBmqB1Y4zbo-5rmvsoPmJsnRlP_hRiUY86zSAQyfbisTGrGBl0IQ7ditpkfYVm0rBWJ8WnYNqYNp8K3qcD7NW72tsy-XoWEjNlz4lWJeRdEG2Nt4CJgnREH4Y" +} +``` diff --git a/node.js b/node.js index 142b18e..e1f51b5 100644 --- a/node.js +++ b/node.js @@ -126,7 +126,9 @@ function create(deps) { return sig64; }; - RSA.generateSignatureJwk = function (keypair, payload, nonce) { + + RSA.signJws = RSA.generateJws = RSA.generateSignatureJws = RSA.generateSignatureJwk = + function (keypair, payload, nonce) { keypair = RSA._internal.import(keypair); keypair = RSA._internal.importForge(keypair); keypair.publicKeyJwk = RSA.exportPublicJwk(keypair); diff --git a/tests/generate-sig.js b/tests/generate-sig.js index 048d21e..188c698 100644 --- a/tests/generate-sig.js +++ b/tests/generate-sig.js @@ -45,7 +45,7 @@ var forgeResult = { }; */ -var jws = RSA.generateSignatureJwk( +var jws = RSA.signJws( keypair , new Buffer('24bcc5d4d04d095de47db279b05685c97f787c9b8bd87f88cdaa0137e7228879', 'hex') , '8ef5624f5ec9d3ef'