convertRsaPublicKeyToAdbFormat static method

Uint8List convertRsaPublicKeyToAdbFormat(
  1. RSAPublicKey publicKey
)

Implementation

static Uint8List convertRsaPublicKeyToAdbFormat(RSAPublicKey publicKey) {
  assert(publicKey.modulus != null && publicKey.publicExponent != null, 'invalid public key');
  BigInt r32 = BigInt.zero.setBit(32);
  BigInt n = publicKey.modulus!;
  BigInt r = BigInt.zero.setBit(KEY_LENGTH_WORDS * 32);
  BigInt rr = r.modPow(BigInt.two, n);
  BigInt rem = n.remainder(r32);
  BigInt n0inv = rem.modInverse(r32);

  List<int> nDataList = List.filled(8, 0, growable: true);
  nDataList.addAll(n.toBytes());
  nDataList.addAll(rr.toBytes());
  nDataList.addAll(publicKey.publicExponent!.toBytes());
  while (nDataList.length < 524) {
    nDataList.add(0);
  }

  Uint8List nData2 = Uint8List.fromList(nDataList);

  nData2.buffer.asByteData().setUint32(0, KEY_LENGTH_WORDS, Endian.little);
  nData2.buffer.asByteData().setUint32(4, (n0inv * BigInt.from(-1)).toInt(), Endian.little);

  return nData2;
}