Permalink
Browse files

New coercers.

  • Loading branch information...
1 parent b25975b commit 1821b3557ffc3e5e3f7510cf74d72057f15fdce1 Dan Bornstein committed Mar 30, 2012
Showing with 199 additions and 1 deletion.
  1. +21 −0 README.md
  2. +61 −0 lib/ursa.js
  3. +1 −1 package.json
  4. +116 −0 test/test.js
View
@@ -95,6 +95,27 @@ Convenient shorthand for `assert(ursa.isPrivateKey(obj))`.
Convenient shorthand for `assert(ursa.isPublicKey(obj))`.
+### ursa.coerceKey(orig)
+
+Coerce the given key value into a key object (either public or
+private), returning it. If given a private key object, this just
+returns it as-is. If given a string or Buffer, it tries to parse it as
+PEM. Anything else will result in an error.
+
+### ursa.coercePrivateKey(orig)
+
+Coerce the given key value into a private key object, returning it. If
+given a private key object, this just returns it as-is. If given a
+string or Buffer, it tries to parse it as PEM. Anything else will
+result in an error.
+
+### ursa.coercePublicKey(orig)
+
+Coerce the given key value into a public key object, returning it. If
+given a private key object, this just returns it as-is. If given a
+string or Buffer, it tries to parse it as PEM. Anything else will
+result in an error.
+
### ursa.createPublicKey(pem, encoding)
Create and return a public key read in from the given PEM-format file.
View
@@ -33,6 +33,9 @@ var BINARY = "binary";
/** encoding constant */
var HEX = "hex";
+/** type name */
+var STRING = "string";
+
/** encoding constant */
var UTF8 = "utf8";
@@ -52,6 +55,13 @@ var theUnunsealer = [ "ursa ununsealer" ];
*/
/**
+ * Return true iff x is either a string or a Buffer.
+ */
+function isStringOrBuffer(x) {
+ return (typeof x === STRING) || Buffer.isBuffer(x);
+}
+
+/**
* Extract and identify the PEM file type represented in the given
* buffer. Returns the extracted type string or undefined if the
* buffer doesn't seem to be any sort of PEM format file.
@@ -464,6 +474,54 @@ function assertPublicKey(obj) {
}
/**
+ * Coerce the given key value into an private key object, returning
+ * it. If given a private key object, this just returns it as-is. If
+ * given a string or Buffer, it tries to parse it as PEM. Anything
+ * else is an error.
+ */
+function coercePrivateKey(orig) {
+ if (isPrivateKey(orig)) {
+ return orig;
+ } else if (isStringOrBuffer(orig)) {
+ return createPrivateKey(orig);
+ }
+
+ throw new Error("Not a private key: " + orig);
+}
+
+/**
+ * Coerce the given key value into a public key object, returning
+ * it. If given a private key object, this just returns it as-is. If
+ * given a string or Buffer, it tries to parse it as PEM. Anything
+ * else is an error.
+ */
+function coercePublicKey(orig) {
+ if (isPublicKey(orig)) {
+ return orig;
+ } else if (isStringOrBuffer(orig)) {
+ return createPublicKey(orig);
+ }
+
+ throw new Error("Not a public key: " + orig);
+}
+
+/**
+ * Coerce the given key value into a key object (either public or
+ * private), returning it. If given a private key object, this just
+ * returns it as-is. If given a string or Buffer, it tries to parse it
+ * as PEM. Anything else is an error.
+ */
+function coerceKey(orig) {
+ if (isKey(orig)) {
+ return orig;
+ } else if (isStringOrBuffer(orig)) {
+ return createKey(orig);
+ }
+
+ throw new Error("Not a key: " + orig);
+}
+
+/**
* Check whether the two objects are both keys of some sort and
* have the same public part.
*/
@@ -557,6 +615,9 @@ module.exports = {
assertKey: assertKey,
assertPrivateKey: assertPrivateKey,
assertPublicKey: assertPublicKey,
+ coerceKey: coerceKey,
+ coercePrivateKey: coercePrivateKey,
+ coercePublicKey: coercePublicKey,
createKey: createKey,
createPrivateKey: createPrivateKey,
createPublicKey: createPublicKey,
View
@@ -1,6 +1,6 @@
{
"name": "ursa",
- "version": "0.6.6",
+ "version": "0.6.7",
"keywords": [
"crypto", "key", "openssl", "private", "public", "rsa", "sign",
"signature", "verify", "verification", "hash", "digest"
View
@@ -279,6 +279,114 @@ function test_fail_createPrivateKey() {
assert.throws(f1, /Not a private key\./);
}
+function test_coerceKey() {
+ var priv = ursa.coerceKey(fixture.PRIVATE_KEY);
+ assert(ursa.isPrivateKey(priv), true);
+
+ priv = ursa.coerceKey(fixture.PRIVATE_KEY.toString());
+ assert(ursa.isPrivateKey(priv), true);
+
+ var pub = ursa.coerceKey(fixture.PUBLIC_KEY);
+ assert(ursa.isPublicKey(pub), true);
+
+ pub = ursa.coerceKey(fixture.PUBLIC_KEY.toString());
+ assert(ursa.isPublicKey(pub), true);
+
+ assert.equal(ursa.coerceKey(priv), priv);
+ assert.equal(ursa.coerceKey(pub), pub);
+}
+
+function test_coercePrivateKey() {
+ var priv = ursa.coercePrivateKey(fixture.PRIVATE_KEY);
+ assert(ursa.isPrivateKey(priv), true);
+
+ priv = ursa.coercePrivateKey(fixture.PRIVATE_KEY.toString());
+ assert(ursa.isPrivateKey(priv), true);
+
+ assert.equal(ursa.coercePrivateKey(priv), priv);
+}
+
+function test_coercePublicKey() {
+ var pub = ursa.coercePublicKey(fixture.PUBLIC_KEY);
+ assert(ursa.isPublicKey(pub), true);
+
+ pub = ursa.coercePublicKey(fixture.PUBLIC_KEY.toString());
+ assert(ursa.isPublicKey(pub), true);
+
+ assert.equal(ursa.coercePublicKey(pub), pub);
+}
+
+function test_fail_coerceKey() {
+ function f1() {
+ ursa.coerceKey("foo");
+ }
+ assert.throws(f1, /Not a key/);
+
+ function f2() {
+ ursa.coerceKey(new Buffer(200));
+ }
+ assert.throws(f2, /Not a key/);
+
+ function f3() {
+ ursa.coerceKey([]);
+ }
+ assert.throws(f3, /Not a key/);
+}
+
+function test_fail_coercePrivateKey() {
+ function f1() {
+ ursa.coercePrivateKey("foo");
+ }
+ assert.throws(f1, /Not a private key/);
+
+ function f2() {
+ ursa.coercePrivateKey(new Buffer(200));
+ }
+ assert.throws(f2, /Not a private key/);
+
+ function f3() {
+ ursa.coercePrivateKey([]);
+ }
+ assert.throws(f3, /Not a private key/);
+
+ function f4() {
+ ursa.coercePrivateKey(fixture.PUBLIC_KEY);
+ }
+ assert.throws(f4, /Not a private key/);
+
+ function f5() {
+ ursa.coercePrivateKey(fixture.PUBLIC_KEY.toString());
+ }
+ assert.throws(f5, /Not a private key/);
+}
+
+function test_fail_coercePublicKey() {
+ function f1() {
+ ursa.coercePublicKey("foo");
+ }
+ assert.throws(f1, /Not a public key/);
+
+ function f2() {
+ ursa.coercePublicKey(new Buffer(200));
+ }
+ assert.throws(f2, /Not a public key/);
+
+ function f3() {
+ ursa.coercePublicKey([]);
+ }
+ assert.throws(f3, /Not a public key/);
+
+ function f4() {
+ ursa.coercePublicKey(fixture.PRIVATE_KEY);
+ }
+ assert.throws(f4, /Not a public key/);
+
+ function f5() {
+ ursa.coercePublicKey(fixture.PRIVATE_KEY.toString());
+ }
+ assert.throws(f5, /Not a public key/);
+}
+
function testPublicKey() {
var key = ursa.createPublicKey(fixture.PUBLIC_KEY);
testPublicKeyMethods(key);
@@ -403,9 +511,17 @@ require("./native").test();
testBasics();
testTypes();
+
test_createKey();
test_fail_createPublicKey();
test_fail_createPrivateKey();
+test_coerceKey();
+test_coercePrivateKey();
+test_coercePublicKey();
+test_fail_coerceKey();
+test_fail_coercePrivateKey();
+test_fail_coercePublicKey();
+
testPublicKey();
testPrivateKey();
testGeneratedKey();

0 comments on commit 1821b35

Please sign in to comment.