Skip to content

Commit

Permalink
decoding negative integers, integer decoding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
killerstorm committed Nov 30, 2017
1 parent 488e917 commit a8e7f04
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/asn1/decoders/der.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,10 @@ DERNode.prototype._decodeInt = function decodeInt(buffer, values) {
const raw = buffer.raw();
let res = new bignum(raw);

if ((raw[0] & 0x80) > 0) { // negative
res = res.fromTwos(raw.length * 8);
}

if (values)
res = values[res.toString(10)] || res;

Expand Down
17 changes: 17 additions & 0 deletions test/der-decode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const assert = require('assert');
const asn1 = require('..');
const bignum = asn1.bignum;

const Buffer = require('buffer').Buffer;

Expand Down Expand Up @@ -54,6 +55,22 @@ describe('asn1.js DER decoder', function() {
});
}

it('should decode integers', function() {
const Int = asn1.define('Int', function () { this.int(); });
const values = [
['020101', 1],
['0203008011', 0x8011],
['02020080', 128],
['0201FF', -1],
['020180', -128],
['0202FF7F', -129]
];
values.forEach( function (v) {
const decoded = Int.decode(new Buffer(v[0], 'hex'), 'der');
assert(decoded.eq(new bignum(v[1])), v[1]);
});
});

test('should decode choice', function() {
this.choice({
apple: this.bool(),
Expand Down

0 comments on commit a8e7f04

Please sign in to comment.