Skip to content

Commit

Permalink
A lot of cleanup (#64)
Browse files Browse the repository at this point in the history
- Made a number of APIs not-nullable
- Made a number of fields final where they could be
- Added types to a few public APIs
- Enabled and fixed a bunch of new lints
- Moved part files to the lib/src dir
- Require Dart 3
  • Loading branch information
kevmoo committed Jul 26, 2023
1 parent 7debac9 commit bea77eb
Show file tree
Hide file tree
Showing 50 changed files with 233 additions and 260 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
strategy:
fail-fast: false
matrix:
sdk: [2.18.0, dev]
sdk: [3.0.0, dev]
steps:
- uses: actions/checkout@v2
- uses: dart-lang/setup-dart@v1.3
Expand All @@ -47,7 +47,7 @@ jobs:
matrix:
# Add macos-latest and/or windows-latest if relevant for this package.
os: [ubuntu-latest]
sdk: [2.18.0, dev]
sdk: [3.0.0, dev]
steps:
- uses: actions/checkout@v2
- uses: dart-lang/setup-dart@v1.3
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

### 1.5.0

- Make a number of fields `final` that should not be changed.
- Add types to a few members.
- Remove nullability from a field fields.
- Require Dart 3.0

### 1.4.1

- Fix null assertion when accessing valueBytes before encodedBytes.
Expand All @@ -20,6 +27,7 @@
### 1.2.2

- Fix #62. Allow context specific tags to be cast to an asn1 sequence.

### 1.2.1

- Fix #57
Expand Down
4 changes: 3 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
include: package:lints/recommended.yaml
include: package:dart_flutter_team_lints/analysis_options.yaml

# This package was created before these constant rules were best practice.
linter:
rules:
constant_identifier_names: false
lines_longer_than_80_chars: false
non_constant_identifier_names: false
use_super_parameters: true
3 changes: 2 additions & 1 deletion example/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:asn1lib/asn1lib.dart';

final certificateDER = decodePEM('''-----BEGIN CERTIFICATE-----
Expand Down Expand Up @@ -44,7 +45,7 @@ void main() {
print(seq.valueBytes().length);
}

Uint8List decodePEM(pem) {
Uint8List decodePEM(String pem) {
var startsWith = [
'-----BEGIN PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----',
Expand Down
58 changes: 26 additions & 32 deletions lib/asn1lib.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
library asn1lib;

import 'dart:typed_data';
import 'dart:convert';
import 'dart:typed_data';

part 'asn1integer.dart';
part 'asn1enumerated.dart';

part 'asn1object.dart';
part 'asn1application.dart';

part 'asn1util.dart';
part 'asn1octetstring.dart';
part 'asn1exception.dart';
part 'asn1sequence.dart';
part 'asn1length.dart';
part 'asn1parser.dart';
part 'asn1constants.dart';
part 'asn1null.dart';
part 'asn1boolean.dart';
part 'asn1set.dart';

part 'asn1objectidentifier.dart';
part 'asn1bitstring.dart';
part 'asn1printablestring.dart';
part 'asn1numericstring.dart';
part 'asn1utf8string.dart';
part 'asn1ia5string.dart';
part 'asn1utctime.dart';
part 'asn1bmpstring.dart';
part 'asn1generalizedtime.dart';
part 'asn1teletextstring.dart';

part 'asn1ipaddress.dart';
part 'src/asn1application.dart';
part 'src/asn1bitstring.dart';
part 'src/asn1bmpstring.dart';
part 'src/asn1boolean.dart';
part 'src/asn1constants.dart';
part 'src/asn1enumerated.dart';
part 'src/asn1exception.dart';
part 'src/asn1generalizedtime.dart';
part 'src/asn1ia5string.dart';
part 'src/asn1integer.dart';
part 'src/asn1ipaddress.dart';
part 'src/asn1length.dart';
part 'src/asn1null.dart';
part 'src/asn1numericstring.dart';
part 'src/asn1object.dart';
part 'src/asn1objectidentifier.dart';
part 'src/asn1octetstring.dart';
part 'src/asn1parser.dart';
part 'src/asn1printablestring.dart';
part 'src/asn1sequence.dart';
part 'src/asn1set.dart';
part 'src/asn1teletextstring.dart';
part 'src/asn1utctime.dart';
part 'src/asn1utf8string.dart';
part 'src/asn1util.dart';
14 changes: 7 additions & 7 deletions lib/asn1application.dart → lib/src/asn1application.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
part of asn1lib;
part of '../asn1lib.dart';

// Represent an ASN1 APPLICATION type. An Application is a
// custom ASN1 object that delegates the interpretation to the
// consumer.
class ASN1Application extends ASN1Object {
ASN1Application({int tag = APPLICATION_CLASS}) : super(tag: tag);
ASN1Application({super.tag = APPLICATION_CLASS});

ASN1Application.fromBytes(Uint8List bytes) : super.fromBytes(bytes) {
ASN1Application.fromBytes(super.bytes) : super.fromBytes() {
// check that this really is an application type
if (!isApplication(tag)) {
throw ASN1Exception('tag $tag is not an ASN1 Application class');
Expand All @@ -18,9 +18,9 @@ class ASN1Application extends ASN1Object {
// custom ASN1 object that delegates the interpretation to the
// consumer.
class ASN1Private extends ASN1Object {
ASN1Private({int tag = PRIVATE_CLASS}) : super(tag: tag);
ASN1Private({super.tag = PRIVATE_CLASS});

ASN1Private.fromBytes(Uint8List bytes) : super.fromBytes(bytes) {
ASN1Private.fromBytes(super.bytes) : super.fromBytes() {
// check that this really is an Private type
if (!isPrivate(tag)) {
throw ASN1Exception('tag $tag is not an ASN1 Private class');
Expand All @@ -32,9 +32,9 @@ class ASN1Private extends ASN1Object {
// custom ASN1 object that delegates the interpretation to the
// consumer.
class ASN1ContextSpecific extends ASN1Object {
ASN1ContextSpecific({int tag = PRIVATE_CLASS}) : super(tag: tag);
ASN1ContextSpecific({super.tag = PRIVATE_CLASS});

ASN1ContextSpecific.fromBytes(Uint8List bytes) : super.fromBytes(bytes) {
ASN1ContextSpecific.fromBytes(super.bytes) : super.fromBytes() {
// check that this really is an Private type
if (!isContextSpecific(tag)) {
throw ASN1Exception('tag $tag is not an ASN1 Context specific class');
Expand Down
15 changes: 7 additions & 8 deletions lib/asn1bitstring.dart → lib/src/asn1bitstring.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
part of asn1lib;
part of '../asn1lib.dart';

///
/// An ASN1 Octet String. This is an array of character codes.
///
class ASN1BitString extends ASN1Object {
/// The decoded string value
late List<int> stringValue;
int unusedbits = 0;
late final List<int> stringValue;
late final int unusedbits;

@override
Uint8List contentBytes() => Uint8List.fromList(stringValue);
Expand All @@ -15,25 +15,24 @@ class ASN1BitString extends ASN1Object {
/// Create an [ASN1BitString] initialized with String value. Optionally override the tag.
///
ASN1BitString(this.stringValue,
{this.unusedbits = 0, int tag = BIT_STRING_TYPE})
: super(tag: tag);
{this.unusedbits = 0, super.tag = BIT_STRING_TYPE});

///
/// Create an [ASN1OctetString] from an encoded list of bytes
///
ASN1BitString.fromBytes(Uint8List bytes) : super.fromBytes(bytes) {
ASN1BitString.fromBytes(super.bytes) : super.fromBytes() {
unusedbits = valueBytes()[0];
stringValue = valueBytes().sublist(1);
}

@override
Uint8List? _encode() {
Uint8List _encode() {
var valBytes = [unusedbits];
valBytes.addAll(stringValue);
_valueByteLength = valBytes.length;
_encodeHeader();
_setValueBytes(valBytes);
return _encodedBytes;
return _encodedBytes!;
}

static String decodeOctetString(Uint8List bytes) =>
Expand Down
13 changes: 6 additions & 7 deletions lib/asn1bmpstring.dart → lib/src/asn1bmpstring.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of asn1lib;
part of '../asn1lib.dart';

///
/// An ASN1 BMP String.
Expand All @@ -7,19 +7,18 @@ part of asn1lib;
///
class ASN1BMPString extends ASN1Object {
/// The decoded string value
late String stringValue;
late final String stringValue;

///
/// Create an [ASN1BMPString] initialized with String value.
/// Optionally override the tag
///
ASN1BMPString(this.stringValue, {int tag = BMP_STRING_TYPE})
: super(tag: tag);
ASN1BMPString(this.stringValue, {super.tag = BMP_STRING_TYPE});

///
/// Create an [ASN1BMPString] from an encoded list of bytes
///
ASN1BMPString.fromBytes(Uint8List bytes) : super.fromBytes(bytes) {
ASN1BMPString.fromBytes(super.bytes) : super.fromBytes() {
var octets = valueBytes();
var mergedOctets = <int>[];

Expand All @@ -33,7 +32,7 @@ class ASN1BMPString extends ASN1Object {
}

@override
Uint8List? _encode() {
Uint8List _encode() {
var octets = utf8.encode(stringValue);
var doubleOctets = <int>[];

Expand All @@ -45,7 +44,7 @@ class ASN1BMPString extends ASN1Object {
_valueByteLength = doubleOctets.length;
_encodeHeader();
_setValueBytes(doubleOctets);
return _encodedBytes;
return _encodedBytes!;
}

@override
Expand Down
16 changes: 7 additions & 9 deletions lib/asn1boolean.dart → lib/src/asn1boolean.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
part of asn1lib;
part of '../asn1lib.dart';

///
/// An ASN1 Boolean
///
class ASN1Boolean extends ASN1Object {
bool? _boolValue;
late final bool booleanValue;

static final ASN1Boolean ASN1TrueBoolean = ASN1Boolean(true);
static final ASN1Boolean ASN1FalseBoolean = ASN1Boolean(false);

bool? get booleanValue => _boolValue;

// ASN1Boolean(this._boolValue,{tag: BOOLEAN_TYPE}):super(tag:BOOLEAN_TYPE) {
ASN1Boolean(this._boolValue, {tag = BOOLEAN_TYPE}) : super(tag: tag) {
ASN1Boolean(this.booleanValue, {super.tag = BOOLEAN_TYPE}) {
_valueByteLength = 1;
}

ASN1Boolean.fromBytes(Uint8List bytes) : super.fromBytes(bytes) {
var b = bytes[_valueStartPosition];
_boolValue = (b == BOOLEAN_TRUE_VALUE);
booleanValue = (b == BOOLEAN_TRUE_VALUE);
}

@override
Uint8List? _encode() {
Uint8List _encode() {
super._encodeHeader();
super._setValueBytes(
[_boolValue == true ? BOOLEAN_TRUE_VALUE : BOOLEAN_FALSE_VALUE]);
return _encodedBytes;
[booleanValue ? BOOLEAN_TRUE_VALUE : BOOLEAN_FALSE_VALUE]);
return _encodedBytes!;
}
}
2 changes: 1 addition & 1 deletion lib/asn1constants.dart → lib/src/asn1constants.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of asn1lib;
part of '../asn1lib.dart';

// Sere https://luca.ntop.org/Teaching/Appunti/asn1.html
// tag bytes for various ASN1 BER objects
Expand Down
4 changes: 2 additions & 2 deletions lib/asn1enumerated.dart → lib/src/asn1enumerated.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
part of asn1lib;
part of '../asn1lib.dart';

///
/// An enum is encoded as an Integer.
///
class ASN1Enumerated extends ASN1Integer {
ASN1Enumerated(int i, {tag = ENUMERATED_TYPE})
ASN1Enumerated(int i, {int tag = ENUMERATED_TYPE})
: super(BigInt.from(i), tag: tag);
}
2 changes: 1 addition & 1 deletion lib/asn1exception.dart → lib/src/asn1exception.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of asn1lib;
part of '../asn1lib.dart';

///
/// An ASN1 Exception
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of asn1lib;
part of '../asn1lib.dart';

///
/// An ASN1 GeneralizedTime value.
Expand All @@ -10,20 +10,19 @@ part of asn1lib;
///
class ASN1GeneralizedTime extends ASN1Object {
// The decoded date value
late DateTime dateTimeValue;
late final DateTime dateTimeValue;

///
/// Create an [ASN1GeneralizedTime] initialized with DateTime value.
///
/// Optionally override the tag
///
ASN1GeneralizedTime(this.dateTimeValue, {int tag = GENERALIZED_TIME})
: super(tag: tag);
ASN1GeneralizedTime(this.dateTimeValue, {super.tag = GENERALIZED_TIME});

///
/// Create an [ASN1GeneralizedTime] from an encoded list of bytes
///
ASN1GeneralizedTime.fromBytes(Uint8List bytes) : super.fromBytes(bytes) {
ASN1GeneralizedTime.fromBytes(super.bytes) : super.fromBytes() {
var octets = valueBytes();
var stringValue = ascii.decode(octets);
var year = stringValue.substring(0, 4);
Expand All @@ -42,7 +41,7 @@ class ASN1GeneralizedTime extends ASN1Object {
}

@override
Uint8List? _encode() {
Uint8List _encode() {
var utc = dateTimeValue.toUtc();
var year = utc.year.toString();
var month = utc.month.toString();
Expand All @@ -57,7 +56,7 @@ class ASN1GeneralizedTime extends ASN1Object {
_valueByteLength = valBytes.length;
_encodeHeader();
_setValueBytes(valBytes);
return _encodedBytes;
return _encodedBytes!;
}

@override
Expand Down

0 comments on commit bea77eb

Please sign in to comment.