asn1lib 0.5.15

  • Readme
  • Changelog
  • Example
  • Installing
  • 95

ASN.1 Parser for Dart #

Encodes & decodes ASN1 using BER encoding.

There is just enough implementation here to get an LDAP client library working. Contributions welcome.

Samples #

Encoding #

import 'package:asn1lib/asn1lib.dart';

var s = ASN1Sequence();
s.add(ASN1Integer(23));
s.add(ASN1OctetString('This is a test'));
s.add(ASN1Boolean(true));

// GET the BER Stream
var bytes = s.encodedBytes;

Decoding #

import 'package:asn1lib/asn1lib.dart';

// e.g. bytes from the Encoding Example
var p = ASN1Parser(bytes);
var s2 = p.nextObject();
// s2 is a sequence...

Changelog #

0.5.15 #

  • Add businessCategory object identifier

0.5.14 #

  • Add DN for subjectAltName

0.5.13 #

  • Merged #37. Improved length computing in the ASN1Parser.
  • Added new unit tests.

0.5.12 #

  • Merged #36. Improved length computing in the ASN1Parser.

0.5.11 #

  • Merged #34. Added example

0.5.10 #

  • Merged #32. Update length in asn1 parser.

0.5.9 #

  • Merged #31. Improve fromBytes. Logic from Java Bouncy Castle.

0.5.8 #

Merged #30

  • Add IA5String
  • Add associated tests
  • Make sure parser handles IA5 and UT8 String
  • Add large test case with PEM Certificate that includes IA5 and UTF8 Strings
  • Cleanup README

0.5.7 #

Merged #29

  • Expand AS1NNull to accept tag
  • Associated test cases
  • Added David Janes as author

0.5.6 #

Merged #28

Add ASN1UTF8String

0.5.5 #

Merged #27

Updates to ASN1ObjectIdentifier to:

  • allow creation from dotted number lists
  • allow creation from dotted number strings
  • allow names to be registered, for shorthands
  • allow bulk name registration
  • commonly used names (obviously, what this is could be expanded)

0.5.4 #

Fixed #26

0.5.1 #

Add AS1Integer.fromInt factory method

0.5.0 #

  • Convert use of BigInteger to dart SDK BigInt

0.4.3 #

  • Updates for dart 2

0.4.2 #

  • Added contentBytes() getter
  • Removed Int64List dependency and use bignum's BigInteger instead

example/main.dart

import 'dart:convert';

import 'package:asn1lib/asn1lib.dart';

List<int> certificateDER = decodePEM("""-----BEGIN CERTIFICATE-----
MIIGD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ser = ASN1Parser(certificateDER);
  var seq = asn1Parser.nextObject() as ASN1Sequence;
  print(seq.valueBytes().length);
}

List<int> decodePEM(pem) {
  var startsWith = [
    "-----BEGIN PUBLIC KEY-----",
    "-----BEGIN PRIVATE KEY-----",
    "-----BEGIN CERTIFICATE-----",
  ];
  var endsWith = [
    "-----END PUBLIC KEY-----",
    "-----END PRIVATE KEY-----",
    "-----END CERTIFICATE-----"
  ];

  //HACK
  for (var s in startsWith) {
    if (pem.startsWith(s)) pem = pem.substring(s.length);
  }

  for (var s in endsWith) {
    if (pem.endsWith(s)) pem = pem.substring(0, pem.length - s.length);
  }

  //Dart base64 decoder does not support line breaks
  pem = pem.replaceAll('\n', '');
  pem = pem.replaceAll('\r', '');
  return base64.decode(pem);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  asn1lib: ^0.5.15

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:asn1lib/asn1lib.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
90
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
95
Learn more about scoring.

We analyzed this package on Dec 13, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.1+4

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.69.0 <3.0.0
Dev dependencies
convert ^2.0.1
test >=1.5.3