ecPrivateKeyFromDerBytes static method

ECPrivateKey ecPrivateKeyFromDerBytes(
  1. Uint8List bytes, {
  2. bool pkcs8 = false,
})

Implementation

static ECPrivateKey ecPrivateKeyFromDerBytes(Uint8List bytes,
    {bool pkcs8 = false}) {
  var asn1Parser = ASN1Parser(bytes);
  var topLevelSeq = asn1Parser.nextObject() as ASN1Sequence;
  late String curveName;
  Uint8List x;
  if (pkcs8) {
    // Parse the PKCS8 format
    var innerSeq = topLevelSeq.elements!.elementAt(1) as ASN1Sequence;
    var b2 = innerSeq.elements!.elementAt(1) as ASN1ObjectIdentifier;
    var b2Data = b2.objectIdentifierAsString;
    var b2Curvedata = ObjectIdentifiers.getIdentifierByIdentifier(b2Data);
    if (b2Curvedata != null) {
      curveName = b2Curvedata['readableName'] as String;
    }

    var octetString = topLevelSeq.elements!.elementAt(2) as ASN1OctetString;
    asn1Parser = ASN1Parser(octetString.valueBytes);
    var octetStringSeq = asn1Parser.nextObject() as ASN1Sequence;
    var octetStringKeyData =
        octetStringSeq.elements!.elementAt(1) as ASN1OctetString;

    x = octetStringKeyData.valueBytes!;
  } else {
    // Parse the SEC1 format
    var privateKeyAsOctetString =
        topLevelSeq.elements!.elementAt(1) as ASN1OctetString;
    var choice = topLevelSeq.elements!.elementAt(2);
    var s = ASN1Sequence();
    var parser = ASN1Parser(choice.valueBytes);
    while (parser.hasNext()) {
      s.add(parser.nextObject());
    }
    var curveNameOi = s.elements!.elementAt(0) as ASN1ObjectIdentifier;
    var data = ObjectIdentifiers.getIdentifierByIdentifier(
        curveNameOi.objectIdentifierAsString);
    if (data != null) {
      curveName = data['readableName'] as String;
    }

    x = privateKeyAsOctetString.valueBytes!;
  }

  return ECPrivateKey(osp2i(x), ECDomainParameters(curveName));
}