How to create a certificate(X509Certificate) in Dart?

2,060

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());
Share:
2,060
Uday
Author by

Uday

Updated on December 22, 2022

Comments

  • Uday
    Uday over 1 year

    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);
    }
    
    • Richard Heap
      Richard Heap almost 4 years
      This package expects a PEM format but you could probably adapt it to a DER input. pub.dev/packages/x509
    • Uday
      Uday almost 4 years
      @RichardHeap do I need to convert my byte array into PEM file and pass it to that lib?
    • Uday
      Uday almost 4 years
      Any Example code will be helpful for others too.
    • Richard Heap
      Richard Heap almost 4 years
      Normally, it expects a PEM string, but it also provides a .fromAsn1 named constructor. Check out the very last test as an example.
  • Uday
    Uday almost 4 years
    I tried overriding this method _parseDer(bytes, type);, which is in this class github.com/appsupdart/x509/blob/master/lib/src/x509_base.dar‌​t. where bytes are my DER byteArray & for type I tried empty string & "IN PUBLIC KEY". but it throws Unhandled Exception: UnimplementedError
  • Uday
    Uday almost 4 years
    I have a DER byte array, can I get the public Key out of it?
  • Richard Heap
    Richard Heap almost 4 years
    Use this as some sample code: stackoverflow.com/questions/54726406/…