computeSharedSecret method

Uint8List computeSharedSecret(
  1. Uint8List private,
  2. Uint8List public,
  3. EcdhCurve curve
)

Compute a shared secret basd on local private key and remote public key on the curve

Implementation

Uint8List computeSharedSecret(
  Uint8List private,
  Uint8List public,
  EcdhCurve curve,
) {
  final _curve = curve.getNative(_ecc);
  final privateSize = _ecc.uECC_curve_private_key_size(_curve);
  final publicSize = _ecc.uECC_curve_public_key_size(_curve);

  final privateKey = calloc.allocate(privateSize).cast<Uint8>();
  final publicKey = calloc.allocate(publicSize).cast<Uint8>();
  final secretKey = calloc.allocate(privateSize).cast<Uint8>();

  privateKey.asTypedList(privateSize).setAll(0, private);
  publicKey.asTypedList(publicSize).setAll(0, public);

  bool err =
      _ecc.uECC_shared_secret(publicKey, privateKey, secretKey, _curve) == 0;

  final Uint8List secret = Uint8List(privateSize);

  secret.setAll(0, secretKey.asTypedList(privateSize));

  calloc.free(privateKey);
  calloc.free(publicKey);
  calloc.free(secretKey);

  if (err) {
    throw Exception("Failed to compute shared secret");
  }

  return secret;
}