Skip to content

Commit d5971e4

Browse files
authored
update: Create a Key without first creating a KeyStore (#170)
1 parent 309c8a4 commit d5971e4

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,17 @@ keystore.remove(key);
238238

239239
### Importing and Exporting a Single Key ###
240240

241+
To create a single "stand alone" key:
242+
243+
```javascript
244+
jose.JWK.createKey("oct", 256, { alg: "A256GCM" }).
245+
then(function(result) {
246+
// {result} is a jose.JWK.Key
247+
// {result.keystore} is a unique jose.JWK.KeyStore
248+
});
249+
```
250+
251+
241252
To import a single Key:
242253

243254
```javascript

lib/jwk/keystore.js

+17
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,23 @@ JWKStore.isKey = function(obj) {
656656
return true;
657657
};
658658

659+
/**
660+
* Creates a new key with the given properties. This method is a convenience
661+
* to calling `JWK.createKeyStore()` then `generate()` on the returned keystore.
662+
*
663+
* @param {String} kty The type of generated key
664+
* @param {String|Number} [size] The size of the generated key
665+
* @param {Object} [props] Additional properties to apply to the generated
666+
* key.
667+
* @returns {Promise} The promise for the generated Key
668+
* @throws {Error} If {kty} is not supported
669+
* @see JWKStore#generate
670+
*/
671+
JWKStore.createKey = function(kty, size, props) {
672+
var ks = JWKStore.createKeyStore();
673+
return ks.generate(kty, size, props);
674+
}
675+
659676
/**
660677
* Coerces the given object into a Key. If {key} is an instance of JWK.Key,
661678
* it is returned directly. Otherwise, this method first creates a new

test/jwk/keystore-test.js

+12
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,18 @@ describe("jwk/keystore", function() {
495495
assert.equal(JWK.store.KeyStore.isKey(), false);
496496
});
497497
});
498+
describe("KeyStore.createKey", function() {
499+
it("creates a new Key", function() {
500+
var p;
501+
p = JWK.store.KeyStore.createKey("oct", 256);
502+
p = p.then(function (result) {
503+
assert.ok(JWK.store.KeyStore.isKey(result));
504+
assert.strictEqual(result.kty, "oct");
505+
assert.strictEqual(result.length, 256);
506+
});
507+
return p;
508+
});
509+
});
498510
describe("KeyStore.asKey", function() {
499511
var props = {
500512
kty: "oct",

0 commit comments

Comments
 (0)