decodePKCS1PrivateKey static method

RSAPrivateKey decodePKCS1PrivateKey(
  1. Uint8List bytes
)

Format:

RSAPrivateKey ::= SEQUENCE {
    version           Version,
    modulus           INTEGER,  -- n
    publicExponent    INTEGER,  -- e
    privateExponent   INTEGER,  -- d
    prime1            INTEGER,  -- p
    prime2            INTEGER,  -- q
    exponent1         INTEGER,  -- d mod (p-1)
    exponent2         INTEGER,  -- d mod (q-1)
    coefficient       INTEGER,  -- (inverse of q) mod p
    otherPrimeInfos   OtherPrimeInfos OPTIONAL
}

See: https://tools.ietf.org/html/rfc8017#appendix-A

Implementation

static RSAPrivateKey decodePKCS1PrivateKey(Uint8List bytes) {
  final parser = ASN1Parser(bytes);

  if (!parser.hasNext()) {
    throw ArgumentError('Not a ASN.1 sequence');
  }

  final seq = parser.nextObject() as ASN1Sequence;

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

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