1

How to create the Certificate in Dart.

I used to create the following certificate in java, Can we convert the following Java code to Dart?

Where the "certBytes" is a byte array in which I will be getting certificate data.

byte[] certBytes = new byte[]{(byte) 0x00, (byte) 0x01, (byte) 0x22, (byte) 0x7E};

InputStream in = new ByteArrayInputStream(certBytes);

X509Certificate certificate = null;

try {
    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    certificate = (X509Certificate) certFactory.generateCertificate(in);
}
Uday
  • 1,619
  • 3
  • 23
  • 48

1 Answers1

1

Take a look at the very last test in the package test routines.

Try the named constructor for parsing DER files.

  var f = File('test/resources/rfc5280_cert1.cer');
  var bytes = f.readAsBytesSync(); // this is the byte array
  var c = X509Certificate.fromAsn1(ASN1Parser(bytes).nextObject());
Richard Heap
  • 48,344
  • 9
  • 130
  • 112
  • I tried overriding this method ```_parseDer(bytes, type);```, which is in this class https://github.com/appsupdart/x509/blob/master/lib/src/x509_base.dart. where bytes are my DER byteArray & for type I tried empty string & "IN PUBLIC KEY". but it throws Unhandled Exception: UnimplementedError – Uday Jul 22 '20 at 15:56
  • I have a DER byte array, can I get the public Key out of it? – Uday Jul 22 '20 at 15:58
  • Use this as some sample code: https://stackoverflow.com/questions/54726406/how-can-i-do-public-key-pinning-in-flutter/54838348#54838348 – Richard Heap Jul 22 '20 at 19:11