Skip to content

Commit

Permalink
fixed & improved integer code
Browse files Browse the repository at this point in the history
  • Loading branch information
killerstorm committed Nov 30, 2017
1 parent a8e7f04 commit 9724d8a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/asn1/decoders/der.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
11 changes: 6 additions & 5 deletions lib/asn1/encoders/der.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 9724d8a

Please sign in to comment.