Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80620 views
1
var assert = require('assert');
2
var asn1 = require('..');
3
var BN = require('bn.js');
4
5
var Buffer = require('buffer').Buffer;
6
7
describe('asn1.js DER encoder', function() {
8
/*
9
* Explicit value shold be wrapped with A0 | EXPLICIT tag
10
* this adds two more bytes to resulting buffer.
11
* */
12
it('should code explicit tag as 0xA2', function() {
13
var E = asn1.define('E', function() {
14
this.explicit(2).octstr()
15
});
16
17
var encoded = E.encode('X', 'der');
18
19
// <Explicit tag> <wrapped len> <str tag> <len> <payload>
20
assert.equal(encoded.toString('hex'), 'a203040158');
21
assert.equal(encoded.length, 5);
22
})
23
24
function test(name, model_definition, model_value, der_expected) {
25
it(name, function() {
26
var Model, der_actual;
27
Model = asn1.define('Model', model_definition);
28
der_actual = Model.encode(model_value, 'der');
29
assert.deepEqual(der_actual, new Buffer(der_expected,'hex'));
30
});
31
}
32
33
test('should encode choice', function() {
34
this.choice({
35
apple: this.bool(),
36
});
37
}, { type: 'apple', value: true }, '0101ff');
38
39
test('should encode implicit seqof', function() {
40
var Int = asn1.define('Int', function() {
41
this.int();
42
});
43
this.implicit(0).seqof(Int);
44
}, [ 1 ], 'A003020101' );
45
46
test('should encode explicit seqof', function() {
47
var Int = asn1.define('Int', function() {
48
this.int();
49
});
50
this.explicit(0).seqof(Int);
51
}, [ 1 ], 'A0053003020101' );
52
53
test('should encode BN(128) properly', function() {
54
this.int();
55
}, new BN(128), '02020080');
56
57
test('should encode int 128 properly', function() {
58
this.int();
59
}, 128, '02020080');
60
61
test('should encode 0x8011 properly', function() {
62
this.int();
63
}, 0x8011, '0203008011');
64
65
test('should omit default value in DER', function() {
66
this.seq().obj(
67
this.key('required').def(false).bool(),
68
this.key('value').int()
69
);
70
}, {required: false, value: 1}, '3003020101');
71
});
72
73