Skip to content

Commit

Permalink
der: support negative integers
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Jul 25, 2019
1 parent 48811c6 commit 4e61ada
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/asn1/decoders/der.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ DERNode.prototype._decodeBool = function decodeBool(buffer) {
DERNode.prototype._decodeInt = function decodeInt(buffer, values) {
// Bigint, return as it is (assume big endian)
const raw = buffer.raw();
let res = new bignum(raw);
let res = new bignum(raw).fromTwos(raw.length * 8);

if (values)
res = values[res.toString(10)] || res;
Expand Down
6 changes: 3 additions & 3 deletions lib/asn1/encoders/der.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ DERNode.prototype._encodeInt = function encodeInt(num, values) {
if (!BN.isBN(num)) {
num = new BN(num);
}
const numArray = num.toArray();
if (!num.sign && numArray[0] & 0x80) {
numArray.unshift(0);
const numArray = num.toTwos(num.byteLength() * 8).toArray();
if (num.isNeg() !== Boolean(numArray[0] & 0x80)) {
numArray.unshift(num.isNeg() ? 0xff : 0);
}
num = new Buffer(numArray);
}
Expand Down

0 comments on commit 4e61ada

Please sign in to comment.