Permalink
Browse files

Added type assertion functions.

  • Loading branch information...
1 parent ae4e47c commit bab502ae04bf584bb7a666120af6ebb009db2ff8 Dan Bornstein committed Feb 29, 2012
Showing with 66 additions and 18 deletions.
  1. +16 −9 README.md
  2. +35 −9 lib/ursa.js
  3. +15 −0 test/test.js
View
@@ -83,6 +83,18 @@ The encoding, if specified, applies to both other arguments.
See "Public Key Methods" below for more details.
+### ursa.assertKey(obj)
+
+Convenient shorthand for `assert(ursa.isKey(obj))`.
+
+### ursa.assertPrivateKey(obj)
+
+Convenient shorthand for `assert(ursa.isPrivateKey(obj))`.
+
+### ursa.assertPublicKey(obj)
+
+Convenient shorthand for `assert(ursa.isPublicKey(obj))`.
+
### ursa.createPublicKey(pem, encoding)
Create and return a public key read in from the given PEM-format file.
@@ -146,12 +158,6 @@ Note that, even though all the public key operations work on private
keys, this function only returns true if the given object is a
public key, per se.
-### ursa.createVerifier(algorithm)
-
-Create and return a verifier which can verify a hash generated with the
-named algorithm (such as `"sha256"` or `"md5"`). See "Verifier Methods" below
-for more details.
-
### ursa.sshFingerprint(sshKey, sshEncoding, outEncoding)
Return the SSH-style public key fingerprint of the given SSH-format
@@ -269,11 +275,12 @@ This method is the underlying one used as part of the implementation
of the higher-level and much friendlier `ursa.createVerifier()` and
`hashAndVerify()`.
-### unbox(unboxer)
+### ununseal(ununsealer)
This is an internal method that is used in the implementation of
-`ursa.isKey()` `ursa.isPrivateKey()` and `ursa.isPublicKey()`. When
-called externally, it should always return `undefined`.
+`ursa.isKey()` `ursa.isPrivateKey()` `ursa.isPublicKey()` and
+associated assertion functions. When called externally, it will
+always return `undefined`.
Private Key Methods
View
@@ -13,6 +13,8 @@
// Note: This also forces OpenSSL to be initialized, which is important!
var crypto = require("crypto");
+var assert = require("assert");
+
var ursaNative = require("../bin/ursaNative");
var RsaWrap = ursaNative.RsaWrap;
var textToNid = ursaNative.textToNid;
@@ -41,8 +43,8 @@ var MD5 = "md5";
var PEM_REGEX =
/^(-----BEGIN (.*) KEY-----\n[\/+=a-zA-Z0-9\n]*\n-----END \2 KEY-----\n)/m;
-/** "unboxer" key object to authenticate objects */
-var theUnboxer = [ "ursa unboxer" ];
+/** "ununsealer" key object to authenticate objects */
+var theUnunsealer = [ "ursa ununsealer" ];
/*
@@ -240,8 +242,8 @@ function PublicKey(rsa) {
return verifier.verify(self, sig, encoding);
}
- function unbox(unboxer) {
- return (unboxer === theUnboxer) ? self : undefined;
+ function ununseal(ununsealer) {
+ return (ununsealer === theUnunsealer) ? self : undefined;
}
self = {
@@ -254,7 +256,7 @@ function PublicKey(rsa) {
toPublicSsh: toPublicSsh,
toPublicSshFingerprint: toPublicSshFingerprint,
verify: verify,
- unbox: unbox
+ ununseal: ununseal
};
return self;
@@ -392,14 +394,14 @@ function isKey(obj) {
var obj2;
try {
- var unbox = obj.unbox;
- if (typeof unbox !== "function") {
+ var ununseal = obj.ununseal;
+ if (typeof ununseal !== "function") {
return false;
}
- obj2 = unbox(theUnboxer);
+ obj2 = ununseal(theUnunsealer);
} catch (ex) {
// Ignore; can't assume that other objects obey any particular
- // unboxing protocol.
+ // ununsealing protocol.
// TODO: Log?
return false;
}
@@ -423,6 +425,27 @@ function isPublicKey(obj) {
return isKey(obj) && !isPrivateKey(obj);
}
+/**
+ * Assert wrapper for isKey().
+ */
+function assertKey(obj) {
+ assert(isKey(obj));
+}
+
+/**
+ * Assert wrapper for isPrivateKey().
+ */
+function assertPrivateKey(obj) {
+ assert(isPrivateKey(obj));
+}
+
+/**
+ * Assert wrapper for isPublicKey().
+ */
+function assertPublicKey(obj) {
+ assert(isPublicKey(obj));
+}
+
/**
* Create a signer object.
*/
@@ -474,6 +497,9 @@ function createVerifier(algorithm) {
*/
module.exports = {
+ assertKey: assertKey,
+ assertPrivateKey: assertPrivateKey,
+ assertPublicKey: assertPublicKey,
createPrivateKey: createPrivateKey,
createPublicKey: createPublicKey,
createSigner: createSigner,
View
@@ -231,6 +231,21 @@ function testTypes() {
assert.equal(ursa.isPrivateKey(priv), true, msg);
assert.equal(ursa.isPrivateKey(undefined), false, msg);
assert.equal(ursa.isPrivateKey("x"), false, msg);
+
+ assert.doesNotThrow(function () { ursa.assertKey(pub); });
+ assert.doesNotThrow(function () { ursa.assertKey(priv); });
+ assert.throws(function () { ursa.assertKey(undefined); });
+ assert.throws(function () { ursa.assertKey("x"); });
+
+ assert.doesNotThrow(function () { ursa.assertPublicKey(pub); });
+ assert.throws(function () { ursa.assertPublicKey(priv); });
+ assert.throws(function () { ursa.assertPublicKey(undefined); });
+ assert.throws(function () { ursa.assertPublicKey("x"); });
+
+ assert.throws(function () { ursa.assertPrivateKey(pub); });
+ assert.doesNotThrow(function () { ursa.assertPrivateKey(priv); });
+ assert.throws(function () { ursa.assertPrivateKey(undefined); });
+ assert.throws(function () { ursa.assertPrivateKey("x"); });
}
function test_fail_createPublicKey() {

0 comments on commit bab502a

Please sign in to comment.