[Pkg-javascript-commits] [node-asn1.js] 31/202: der: bignum support

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:18:51 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-asn1.js.

commit bb04e2f6488a75c7fdb99440ab98469c4672c489
Author: Fedor Indutny <fedor at indutny.com>
Date:   Tue Apr 15 12:14:40 2014 +0400

    der: bignum support
---
 lib/asn1.js              |  7 +++++++
 lib/asn1/decoders/der.js | 11 +++++++++++
 lib/asn1/encoders/der.js | 17 +++++++++++++++++
 package.json             |  3 +++
 test/ping-pong-test.js   | 10 ++++++++++
 5 files changed, 48 insertions(+)

diff --git a/lib/asn1.js b/lib/asn1.js
index 18fed16..660848f 100644
--- a/lib/asn1.js
+++ b/lib/asn1.js
@@ -1,5 +1,12 @@
 var asn1 = exports;
 
+// Optional bignum
+try {
+  asn1.bignum = require('bignum');
+} catch (e) {
+  asn1.bignum = null;
+}
+
 asn1.define = require('./asn1/api').define;
 asn1.base = require('./asn1/base');
 asn1.constants = require('./asn1/constants');
diff --git a/lib/asn1/decoders/der.js b/lib/asn1/decoders/der.js
index b90f2a8..1475d66 100644
--- a/lib/asn1/decoders/der.js
+++ b/lib/asn1/decoders/der.js
@@ -2,6 +2,7 @@ var util = require('util');
 
 var asn1 = require('../../asn1');
 var base = asn1.base;
+var bignum = asn1.bignum;
 
 // Import DER constants
 var der = asn1.constants.der;
@@ -203,6 +204,15 @@ DERNode.prototype._decodeBool = function decodeBool(buffer) {
 
 DERNode.prototype._decodeInt = function decodeInt(buffer, values) {
   var res = 0;
+
+  // Bigint, return as it is (assume big endian)
+  var raw = buffer.raw();
+  if (raw.length > 3) {
+    if (bignum !== null)
+      raw = bignum.fromBuffer(raw, { endian: 'big' });
+    return raw;
+  }
+
   while (!buffer.isEmpty()) {
     res <<= 8;
     var i = buffer.readUInt8();
@@ -213,6 +223,7 @@ DERNode.prototype._decodeInt = function decodeInt(buffer, values) {
 
   if (values)
     res = values[res] || res;
+
   return res;
 };
 
diff --git a/lib/asn1/encoders/der.js b/lib/asn1/encoders/der.js
index 330838c..203b34e 100644
--- a/lib/asn1/encoders/der.js
+++ b/lib/asn1/encoders/der.js
@@ -3,6 +3,7 @@ var Buffer = require('buffer').Buffer;
 
 var asn1 = require('../../asn1');
 var base = asn1.base;
+var bignum = asn1.bignum;
 
 // Import DER constants
 var der = asn1.constants.der;
@@ -162,6 +163,22 @@ DERNode.prototype._encodeInt = function encodeInt(num, values) {
     num = values[num];
   }
 
+  // Bignum, assume big endian
+  if (bignum !== null && num instanceof bignum)
+    num = num.toBuffer({ endian: 'big' });
+
+  if (Buffer.isBuffer(num)) {
+    var size = num.length;
+    if (num.length === 0)
+      size++;
+
+    var out = new Buffer(size);
+    num.copy(out);
+    if (num.length === 0)
+      out[0] = 0
+    return this._createEncoderBuffer(out);
+  }
+
   if (num < 0x100)
     return this._createEncoderBuffer(num);
 
diff --git a/package.json b/package.json
index b2a61ba..6165feb 100644
--- a/package.json
+++ b/package.json
@@ -22,5 +22,8 @@
   "homepage": "https://github.com/indutny/asn1.js",
   "devDependencies": {
     "mocha": "~1.14.0"
+  },
+  "optionalDependencies": {
+    "bignum": "~0.6.2"
   }
 }
diff --git a/test/ping-pong-test.js b/test/ping-pong-test.js
index 8537096..7a82364 100644
--- a/test/ping-pong-test.js
+++ b/test/ping-pong-test.js
@@ -19,6 +19,16 @@ describe('asn1.js ping/pong', function() {
       this.int();
     }, 0);
 
+    if (asn1.bignum !== null) {
+      test('bigint', function() {
+        this.int();
+      }, new asn1.bignum('0102030405060708', 16));
+    } else {
+      test('bigint', function() {
+        this.int();
+      }, new Buffer('0102030405060708', 'hex'));
+    }
+
     test('enum', function() {
       this.enum({ 0: 'hello', 1: 'world' });
     }, 'world');

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-asn1.js.git



More information about the Pkg-javascript-commits mailing list