Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added base64 support to biginteger, started adding dispatching based …

…on version
  • Loading branch information...
commit e929cf7d30608df3d6e859651712de1b49c3c243 1 parent 71ed59a
@benadida benadida authored
View
6 bin/convert-bigint.js
@@ -28,6 +28,9 @@ switch (argv.i) {
case 'hex':
the_bigint = new BigInteger(argv._[0], 16);
break;
+ case 'b64':
+ the_bigint = BigInteger.fromBase64(argv._[0]);
+ break;
}
var output;
@@ -39,6 +42,9 @@ switch (argv.o) {
case 'hex':
output = the_bigint.toString(16);
break;
+ case 'b64':
+ output = the_bigint.toBase64();
+ break;
}
console.log(output);
View
16 lib/algs/index.js
@@ -6,6 +6,8 @@
* baseline objects for all algorithms
*/
+const version = require("../version");
+
function NotImplementedException(message) {
this.message = message;
this.toString = function() { return "Not implemented: "+this.message; };
@@ -107,9 +109,21 @@ SecretKey.deserialize = function(str) {
return SecretKey.fromSimpleObject(obj);
};
+// this looks at the method name
+function dispatchOnDataFormatVersion(coreFunctionName) {
+ return function() {
+ var currentVersionString = version.getDataFormatVersion() || 'LEGACY';
+ currentVersionString = currentVersionString.replace(/\./g, '');
+ var methodName = "_" + currentVersionString + "_" + coreFunctionName;
+
+ // invoke
+ return this[methodName].apply(this, arguments);
+ };
+}
+
exports.ALGS = ALGS;
exports.PublicKey = PublicKey;
exports.SecretKey = SecretKey;
exports.KeyPair = KeyPair;
exports.NotImplementedException = NotImplementedException;
-
+exports.dispatchOnDataFormatVersion = dispatchOnDataFormatVersion;
View
51 lib/algs/rs.js
@@ -2,11 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-var libs = require("../../libs/all"),
- algs = require("./index"),
- version = require("../version");
-
-var BigInteger = libs.BigInteger;
+const libs = require("../../libs/all"),
+ algs = require("./index"),
+ version = require("../version"),
+BigInteger = libs.BigInteger;
// supported keysizes
var KEYSIZES = {
@@ -110,28 +109,52 @@ SecretKey.prototype.sign = function(message, rng, progressCB, doneCB) {
doneCB(signature);
};
-SecretKey.prototype.serializeToObject = function(obj) {
- // XXX branch on version.DATA_FORMAT_VERSION
-
- // obj.value = this.rsa.serializePrivateASN1();
+//
+// support for different data formats
+//
+
+SecretKey.prototype._LEGACY_serializeToObject = function(obj) {
+ obj.version = undefined;
+
obj.n = this.rsa.n.toString();
obj.e = this.rsa.e.toString();
obj.d = this.rsa.d.toString();
-};
+}
-SecretKey.prototype.deserializeFromObject = function(obj) {
- // XXX branch on version.DATA_FORMAT_VERSION
+SecretKey.prototype._20120815_serializeToObject = function(obj) {
+ obj.version = '2012.08.15';
+
+ obj.modulus = this.rsa.n.toBase64();
+ obj.exponent = new BigInteger(this.rsa.e.toString(), 10).toBase64();
+ obj.secretExponent = this.rsa.d.toBase64();
+}
+
+SecretKey.prototype.serializeToObject = algs.dispatchOnDataFormatVersion('serializeToObject');
+SecretKey.prototype._LEGACY_deserializeFromObject = function(obj) {
this.rsa = new libs.RSAKey();
- // this.rsa.readPrivateKeyFromPEMString(obj.value);
+
this.rsa.n = new BigInteger(obj.n, 10);
this.rsa.e = new BigInteger(obj.e, 10);
this.rsa.d = new BigInteger(obj.d, 10);
this.keysize = _getKeySizeFromRSAKeySize(this.rsa.n.bitLength());
- return this;
+ return this;
};
+SecretKey.prototype._20120815_deserializeFromObject = function(obj) {
+ this.rsa = new libs.RSAKey();
+
+ this.rsa.n = new BigInteger(obj.n, 10);
+ this.rsa.e = new BigInteger(obj.e, 10);
+ this.rsa.d = new BigInteger(obj.d, 10);
+
+ this.keysize = _getKeySizeFromRSAKeySize(this.rsa.n.bitLength());
+ return this;
+};
+
+SecretKey.prototype.deserializeFromObject = algs.dispatchOnDataFormatVersion('deserializeFromObject');
+
// register this stuff
algs.register("RS", {
generate: generate,
View
8 libs/bigint-patch.js
@@ -86,9 +86,17 @@ try {
},
isProbablePrime: function(reps) {
return this._bigint.probPrime(reps);
+ },
+ toBase64: function() {
+ return this._bigint.toBuffer().toString('base64');
}
};
+ BigInteger.fromBase64 = function(b64_string) {
+ var bi = bigint.fromBuffer(new Buffer(b64_string, 'base64'));
+ return Bigint._from_bigint(bi);
+ };
+
nativeBigInteger = BigInteger;
} catch (x) {
// oh well, we use normal JS
Please sign in to comment.
Something went wrong with that request. Please try again.