encrypt static method

ECDHResult encrypt(
  1. Uint8List value, {
  2. PublicKey? publicKey,
})

Encrypts the given data with the given public key.

Implementation

static ECDHResult encrypt(Uint8List value, {pd.PublicKey? publicKey}) {
  publicKey = publicKey == null ? pd.PublicKey.empty() : publicKey;
  KeyPair tempKey = Keys.generate(curve: publicKey.curve);
  ECDomainParameters domainParams = ECDomainParameters(publicKey.curve!.name!);
  ECPoint Q = domainParams.curve.decodePoint(publicKey.ecdh())!;
  BigInt d = PDUtil.decodeBigInt(tempKey.privateKey.ec(), endian: Endian.big);
  Uint8List sharedSecret =
      PDUtil.encodeBigInt((Q * d)!.x!.toBigInteger()!, endian: Endian.big);
  Uint8List secretKey = Sha.sha512([sharedSecret]);
  Uint8List encryptedData = AesCbcZeroPadding.encrypt(value,
      key: secretKey.sublist(0, 32), iv: Uint8List(16));

  return ECDHResult(
      isEncrypted: true,
      data: encryptedData,
      key: secretKey.sublist(32, 64),
      publicKey: tempKey.publicKey.ecdh());
}