raw property

  1. @override
Uint8List get raw
override

Returns the key's raw bytes

Implementation

@override
Uint8List get raw {
  // Encode the private key in ASN.1 DER format according to PKCS#1
  final asn1Sequence = pc.ASN1Sequence();
  asn1Sequence.add(pc.ASN1Integer(BigInt.from(0))); // version
  asn1Sequence.add(pc.ASN1Integer(_key.modulus!));

  // For the public exponent, we need to extract it from the public key
  asn1Sequence.add(pc.ASN1Integer(_publicKey._key.exponent!));

  asn1Sequence.add(pc.ASN1Integer(_key.privateExponent!));
  asn1Sequence.add(pc.ASN1Integer(_key.p!));
  asn1Sequence.add(pc.ASN1Integer(_key.q!));

  // Calculate d mod (p-1)
  final dP = _key.privateExponent! % (_key.p! - BigInt.from(1));
  asn1Sequence.add(pc.ASN1Integer(dP));

  // Calculate d mod (q-1)
  final dQ = _key.privateExponent! % (_key.q! - BigInt.from(1));
  asn1Sequence.add(pc.ASN1Integer(dQ));

  // Calculate (inverse of q) mod p
  final qInv = _key.q!.modInverse(_key.p!);
  asn1Sequence.add(pc.ASN1Integer(qInv));

  return Uint8List.fromList(asn1Sequence.encode() ?? []);
}