@@ -34,6 +34,12 @@ class ParseASN1HexString extends Operation {
34
34
"name" : "Truncate octet strings longer than" ,
35
35
"type" : "number" ,
36
36
"value" : 32
37
+ } ,
38
+ {
39
+ "name" : "Wrap Input in SEQUENCE" ,
40
+ "type" : "boolean" ,
41
+ "value" : false ,
42
+ "hint" : "Use this when there is extra data that needs to be decoded"
37
43
}
38
44
] ;
39
45
}
@@ -44,8 +50,34 @@ class ParseASN1HexString extends Operation {
44
50
* @returns {string }
45
51
*/
46
52
run ( input , args ) {
47
- const [ index , truncateLen ] = args ;
48
- return r . ASN1HEX . dump ( input . replace ( / \s / g, "" ) . toLowerCase ( ) , {
53
+ const [ index , truncateLen , addSequence ] = args ;
54
+ let hex = input . replace ( / \s / g, "" ) . toLowerCase ( ) ;
55
+ if ( addSequence ) {
56
+ let sequence = '30' ;
57
+ let len = hex . length / 2 ;
58
+ if ( len <= 127 ) {
59
+ // We can use the short form
60
+ sequence += len . toString ( 16 ) . padStart ( 2 , '0' ) ;
61
+ } else {
62
+ let bytes = 0 ;
63
+ let encoded = '' ;
64
+ // Calculate the number of bytes needed to encode the length
65
+ while ( len > 0 ) {
66
+ bytes ++ ;
67
+ // While we are at it, also build up the length
68
+ encoded = ( len & 0xff ) . toString ( 16 ) . padStart ( 2 , '0' ) + encoded ;
69
+ len >>= 8 ;
70
+ }
71
+ // encode the number of bytes needed for the length
72
+ sequence += ( bytes | 0x80 ) . toString ( 16 ) . padStart ( 2 , '0' ) ;
73
+ // add the encoded length
74
+ sequence += encoded ;
75
+ }
76
+ // Add the sequence + length in front of the original input
77
+ hex = sequence + hex ;
78
+ }
79
+
80
+ return r . ASN1HEX . dump ( hex , {
49
81
"ommit_long_octet" : truncateLen
50
82
} , index ) ;
51
83
}
0 commit comments