2021-02-12 17:14:13 +01:00
|
|
|
import 'dart:typed_data';
|
|
|
|
import 'asn1_identifier.dart';
|
|
|
|
|
|
|
|
class ASN1DEREncoder {
|
2021-02-22 23:54:09 +01:00
|
|
|
static Uint8List encodeSequence({required Uint8List content}) {
|
|
|
|
var encoded = Uint8List.fromList([]);
|
|
|
|
encoded.add(ASN1Identifier.constructedTag |
|
|
|
|
ASN1IdentifierTagNumber.SEQUENCE.toValue());
|
|
|
|
encoded.addAll(contentLength(size: content.length));
|
|
|
|
encoded.addAll(content);
|
|
|
|
return Uint8List.fromList(encoded);
|
|
|
|
}
|
2021-02-12 17:14:13 +01:00
|
|
|
|
2021-02-22 23:54:09 +01:00
|
|
|
static Uint8List contentLength({required int size}) {
|
|
|
|
if (size >= 128) {
|
|
|
|
var lenBytes = Uint8List(size);
|
|
|
|
while (lenBytes.first == 0) {
|
|
|
|
lenBytes.removeAt(0);
|
|
|
|
}
|
|
|
|
int len = 0x80 | lenBytes.length;
|
|
|
|
return Uint8List(len)..addAll(lenBytes);
|
|
|
|
} else {
|
|
|
|
return Uint8List(size);
|
2021-02-12 17:14:13 +01:00
|
|
|
}
|
2021-02-22 23:54:09 +01:00
|
|
|
}
|
2021-02-12 17:14:13 +01:00
|
|
|
}
|