Permalink
Browse files

return the public key string

  • Loading branch information...
1 parent 6a58b30 commit c3f929120ea80266c60f11a75a971cb573b2d7b8 @ryanramage committed May 31, 2012
Showing with 58 additions and 3 deletions.
  1. +40 −0 lib/base64.js
  2. +1 −1 lib/rsa.js
  3. +15 −1 rsa.js
  4. +2 −1 tests/amd-rsa-test.js
View
@@ -71,3 +71,43 @@ exports.b64toBA = function (s) {
}
return a;
}
+
+exports.b64to16 = function(s) {
+ var ret = "";
+ var i;
+ var k = 0;
+ var slop;
+ for (i = 0; i < s.length; ++i)
+ {
+ if (s.charAt(i) == "=") break;
+ v = b64map.indexOf(s.charAt(i));
+ if (v < 0) continue;
+ if (k == 0)
+ {
+ ret += jsbn.int2char(v >> 2);
+ slop = v & 3;
+ k = 1;
+ }
+ else if (k == 1)
+ {
+ ret += jsbn.int2char((slop << 2) | (v >> 4));
+ slop = v & 0xf;
+ k = 2;
+ }
+ else if (k == 2)
+ {
+ ret += jsb.int2char(slop);
+ ret += jsbn.int2char(v >> 2);
+ slop = v & 3;
+ k = 3;
+ }
+ else
+ {
+ ret += jsbn.int2char((slop << 2) | (v >> 4));
+ ret += jsbn.int2char(v & 0xf);
+ k = 0;
+ }
+ }
+ if (k == 1) ret += jsbn.int2char(slop << 2);
+ return ret;
+}
View
@@ -780,5 +780,5 @@ RSAKey.prototype.readPrivateKeyFromPEMString = _rsapem_readPrivateKeyFromPEMStri
-exports.RSAKey = RSAKey;
+ exports.RSAKey = RSAKey;
exports.Math = Math; // so we can seed the rng.
View
16 rsa.js
@@ -4,7 +4,7 @@ if (typeof define !== 'function') {
define(function(require) {
var rsa = require('./lib/rsa');
-
+ var base64 = require('./lib/base64')
var my = {};
my.generate = function(passphrase, options, callback) {
@@ -29,7 +29,21 @@ define(function(require) {
return key;
}
+ my.publicKeyFromString = function(string)
+ {
+ var N = base64.b64to16(string.split("|")[0]);
+ var E = "03";
+ var rsa = new rsa.RSAKey();
+ rsa.setPublic(N, E);
+ return rsa
+ }
+ // Returns the ascii-armored version of the public key.
+ my.publicKeyString = function(rsakey)
+ {
+ pubkey = base64.hex2b64(rsakey.n.toString(16));
+ return pubkey;
+ }
return my;
});
View
@@ -20,7 +20,8 @@ test('Read Private RSA key', function(t) {
console.log(data.toString());
var private = rsa.privateFromPEM(data.toString());
console.log(private);
-
+ var public = rsa.publicKeyString(private);
+ console.log(public);
t.end();
})

0 comments on commit c3f9291

Please sign in to comment.