ed25519KeyIdentityFromPem function

Future<Ed25519KeyIdentity> ed25519KeyIdentityFromPem(
  1. String pem
)

Implementation

Future<Ed25519KeyIdentity> ed25519KeyIdentityFromPem(String pem) async {
  try {
    var privateKeyPem = pem
        .replaceAll("-----END PRIVATE KEY-----", "")
        .replaceAll(RegExp(r"\n+"), "")
        .replaceAll(RegExp(r"\s+"), "")
        .trim()
        .replaceAll('r[^0-9a-zA-Z/+]', '');
    var keyBytes = base64Decode(privateKeyPem);
    final ASN1Parser p = ASN1Parser(keyBytes);
    final ASN1Sequence seq = p.nextObject() as ASN1Sequence;
    final ASN1Parser p2 = ASN1Parser(seq.elements?[2].valueBytes);
    var octetStringSeq = p2.nextObject() as ASN1OctetString;
    var res = octetStringSeq.valueBytes!;
    return Ed25519KeyIdentity.generate(res);
  } catch (e) {
    rethrow;
  }
}