rsaPrivateKeyFromString function

RSAPrivateKey rsaPrivateKeyFromString(
  1. String privateKeyString
)

Create an RSAPrivateKey from the given String.

Implementation

RSAPrivateKey rsaPrivateKeyFromString(String privateKeyString) {
  List<int> privateKeyDER = base64Decode(privateKeyString);
  var asn1Parser = ASN1Parser(privateKeyDER as Uint8List);
  final topLevelSeq = asn1Parser.nextObject() as ASN1Sequence;
  final privateKey = topLevelSeq.elements[2];

  asn1Parser = ASN1Parser(privateKey.contentBytes());
  final pkSeq = asn1Parser.nextObject() as ASN1Sequence;

  final modulus = pkSeq.elements[1] as ASN1Integer;
  final privateExponent = pkSeq.elements[3] as ASN1Integer;
  final p = pkSeq.elements[4] as ASN1Integer;
  final q = pkSeq.elements[5] as ASN1Integer;

  return RSAPrivateKey(modulus.valueAsBigInteger, privateExponent.valueAsBigInteger, p.valueAsBigInteger, q.valueAsBigInteger);
}