generate static method

KeyPair generate({
  1. Curve? curve,
})

Generate a keypair for the given curve

Implementation

static common.KeyPair generate({common.Curve? curve}) {
  if (curve == null) {
    curve = common.Curve.getDefaultCurve();
  } else if (!curve.supported) {
    throw UnsupportedError('Curve ${curve.name} is not supported');
  }

  pc.SecureRandom secureRandom = pc.SecureRandom("Fortuna");
  Random random = Random.secure();
  List<int> seeds = [];
  for (int i = 0; i < 32; i++) {
    seeds.add(random.nextInt(255));
  }
  secureRandom.seed(pc.KeyParameter(Uint8List.fromList(seeds)));

  pc.ECDomainParameters domainParams = pc.ECDomainParameters(curve.name!);
  pc.ECKeyGeneratorParameters ecParams =
      pc.ECKeyGeneratorParameters(domainParams);
  pc.ParametersWithRandom params =
      pc.ParametersWithRandom<pc.ECKeyGeneratorParameters>(
          ecParams, secureRandom);

  pc.KeyGenerator keyGenerator = pc.KeyGenerator("EC");
  keyGenerator.init(params);

  pc.AsymmetricKeyPair keyPair = keyGenerator.generateKeyPair();
  pc.ECPrivateKey privateKey = keyPair.privateKey as pc.ECPrivateKey;
  pc.ECPublicKey publicKey = keyPair.publicKey as pc.ECPublicKey;
  return common.KeyPair(
      common.PrivateKey(
          common.PDUtil.encodeBigInt(privateKey.d!, endian: Endian.big),
          curve),
      common.PublicKey(
          common.PDUtil.encodeBigInt(publicKey.Q!.x!.toBigInteger()!,
              endian: Endian.big),
          common.PDUtil.encodeBigInt(publicKey.Q!.y!.toBigInteger()!,
              endian: Endian.big),
          curve));
}