Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add type-general key reader function, createKey().

  • Loading branch information...
commit 2c5900183727fbd5233f7ac41ac6e880c36be163 1 parent 4ccc448
Dan Bornstein authored
Showing with 33 additions and 1 deletion.
  1. +18 −0 lib/ursa.js
  2. +1 −1  package.json
  3. +14 −0 test/test.js
View
18 lib/ursa.js
@@ -375,6 +375,23 @@ function generatePrivateKey(modulusBits, exponent) {
}
/**
+ * Create a key object from a PEM format file, either a private or
+ * public key depending on what kind of file is passed in. If given
+ * a private key file, it must not be encrypted.
+ */
+function createKey(pem, encoding) {
+ pem = decodeString(pem, encoding);
+
+ if (isPublicKeyPem(pem)) {
+ return createPublicKey(pem);
+ } else if (isPrivateKeyPem(pem)) {
+ return createPrivateKey(pem);
+ } else {
+ throw new Error("Not a key.");
+ }
+}
+
+/**
* Return the SSH-style public key fingerprint of the given SSH-format
* public key.
*/
@@ -540,6 +557,7 @@ module.exports = {
assertKey: assertKey,
assertPrivateKey: assertPrivateKey,
assertPublicKey: assertPublicKey,
+ createKey: createKey,
createPrivateKey: createPrivateKey,
createPublicKey: createPublicKey,
createSigner: createSigner,
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "ursa",
- "version": "0.6.4",
+ "version": "0.6.5",
"keywords": [
"crypto", "key", "openssl", "private", "public", "rsa", "sign",
"signature", "verify", "verification", "hash", "digest"
View
14 test/test.js
@@ -248,6 +248,19 @@ function testTypes() {
assert.throws(function () { ursa.assertPrivateKey("x"); });
}
+function test_createKey() {
+ var priv = ursa.createKey(fixture.PRIVATE_KEY);
+ assert(ursa.isPrivateKey(priv), true);
+
+ var pub = ursa.createKey(fixture.PUBLIC_KEY);
+ assert(ursa.isPublicKey(pub), true);
+
+ function f1() {
+ ursa.createKey("yo there");
+ }
+ assert.throws(f1, /Not a key\./);
+}
+
function test_fail_createPublicKey() {
// This is mostly tested at the native level. This just tests the
// extra failures added at the high level.
@@ -390,6 +403,7 @@ require("./native").test();
testBasics();
testTypes();
+test_createKey();
test_fail_createPublicKey();
test_fail_createPrivateKey();
testPublicKey();

0 comments on commit 2c59001

Please sign in to comment.
Something went wrong with that request. Please try again.