From 9724d8ab11352c2591a23914807b6f89a7b721ea Mon Sep 17 00:00:00 2001 From: Alex Mizrahi Date: Thu, 30 Nov 2017 19:21:27 +0200 Subject: [PATCH] fixed & improved integer code --- lib/asn1/decoders/der.js | 3 ++- lib/asn1/encoders/der.js | 11 ++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/asn1/decoders/der.js b/lib/asn1/decoders/der.js index c8ccecf..c9943dd 100644 --- a/lib/asn1/decoders/der.js +++ b/lib/asn1/decoders/der.js @@ -254,7 +254,8 @@ DERNode.prototype._decodeInt = function decodeInt(buffer, values) { const raw = buffer.raw(); let res = new bignum(raw); - if ((raw[0] & 0x80) > 0) { // negative + // check if number is negative + if ((raw[0] & 0x80) > 0) { res = res.fromTwos(raw.length * 8); } diff --git a/lib/asn1/encoders/der.js b/lib/asn1/encoders/der.js index 12df10f..da859b5 100644 --- a/lib/asn1/encoders/der.js +++ b/lib/asn1/encoders/der.js @@ -5,6 +5,7 @@ const Buffer = require('buffer').Buffer; const asn1 = require('../../asn1'); const base = asn1.base; +const bignum = asn1.bignum; // Import DER constants const der = asn1.constants.der; @@ -203,16 +204,16 @@ DERNode.prototype._encodeInt = function encodeInt(num, values) { let width = Math.ceil(bitLength / 8) * 8; // check if sign bit is occupied if ((bitLength % 8) === 0) { - // when number is of form 10..0 no correction is needed - if (num.zeroBits() + 1 !== num.bitLength()) { - width += 8; - } + // when number is of form 10..0 no correction is needed + if (num.zeroBits() + 1 !== num.bitLength()) { + width += 8; + } } numArray = num.toTwos(width).toArray(); } else { numArray = num.toArray(); if (numArray[0] & 0x80) { - numArray.unshift(0); + numArray.unshift(0); } }