1
|
# ASN1.js
|
2
|
|
3
|
ASN.1 DER Encoder/Decoder and DSL.
|
4
|
|
5
|
## Example
|
6
|
|
7
|
Define model:
|
8
|
|
9
|
```javascript
|
10
|
var asn = require('asn1.js');
|
11
|
|
12
|
var Human = asn.define('Human', function() {
|
13
|
this.seq().obj(
|
14
|
this.key('firstName').octstr(),
|
15
|
this.key('lastName').octstr(),
|
16
|
this.key('age').int(),
|
17
|
this.key('gender').enum({ 0: 'male', 1: 'female' }),
|
18
|
this.key('bio').seqof(Bio)
|
19
|
);
|
20
|
});
|
21
|
|
22
|
var Bio = asn.define('Bio', function() {
|
23
|
this.seq().obj(
|
24
|
this.key('time').gentime(),
|
25
|
this.key('description').octstr()
|
26
|
);
|
27
|
});
|
28
|
```
|
29
|
|
30
|
Encode data:
|
31
|
|
32
|
```javascript
|
33
|
var output = Human.encode({
|
34
|
firstName: 'Thomas',
|
35
|
lastName: 'Anderson',
|
36
|
age: 28,
|
37
|
gender: 'male',
|
38
|
bio: [
|
39
|
{
|
40
|
time: +new Date('31 March 1999'),
|
41
|
description: 'freedom of mind'
|
42
|
}
|
43
|
]
|
44
|
}, 'der');
|
45
|
```
|
46
|
|
47
|
Decode data:
|
48
|
|
49
|
```javascript
|
50
|
var human = Human.decode(output, 'der');
|
51
|
console.log(human);
|
52
|
/*
|
53
|
{ firstName: <Buffer 54 68 6f 6d 61 73>,
|
54
|
lastName: <Buffer 41 6e 64 65 72 73 6f 6e>,
|
55
|
age: 28,
|
56
|
gender: 'male',
|
57
|
bio:
|
58
|
[ { time: 922820400000,
|
59
|
description: <Buffer 66 72 65 65 64 6f 6d 20 6f 66 20 6d 69 6e 64> } ] }
|
60
|
*/
|
61
|
```
|
62
|
|
63
|
### Partial decode
|
64
|
|
65
|
Its possible to parse data without stopping on first error. In order to do it,
|
66
|
you should call:
|
67
|
|
68
|
```javascript
|
69
|
var human = Human.decode(output, 'der', { partial: true });
|
70
|
console.log(human);
|
71
|
/*
|
72
|
{ result: { ... },
|
73
|
errors: [ ... ] }
|
74
|
*/
|
75
|
```
|
76
|
|
77
|
#### LICENSE
|
78
|
|
79
|
This software is licensed under the MIT License.
|
80
|
|
81
|
Copyright Fedor Indutny, 2013.
|
82
|
|
83
|
Permission is hereby granted, free of charge, to any person obtaining a
|
84
|
copy of this software and associated documentation files (the
|
85
|
"Software"), to deal in the Software without restriction, including
|
86
|
without limitation the rights to use, copy, modify, merge, publish,
|
87
|
distribute, sublicense, and/or sell copies of the Software, and to permit
|
88
|
persons to whom the Software is furnished to do so, subject to the
|
89
|
following conditions:
|
90
|
|
91
|
The above copyright notice and this permission notice shall be included
|
92
|
in all copies or substantial portions of the Software.
|
93
|
|
94
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
95
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
96
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
97
|
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
98
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
99
|
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
100
|
USE OR OTHER DEALINGS IN THE SOFTWARE.
|