generateRsa method

EvpPkey generateRsa(
  1. int bits
)
inherited

Gera um par de chaves RSA.

Implementation

EvpPkey generateRsa(int bits) {
  final rsa = bindings.RSA_new();
  if (rsa == nullptr) throw OpenSslException('Failed to create RSA object');

  final bne = bindings.BN_new();
  if (bne == nullptr) {
    bindings.RSA_free(rsa);
    throw OpenSslException('Failed to create BIGNUM');
  }

  try {
    if (bindings.BN_set_word(bne, 65537) != 1) {
      // 0x10001
      throw OpenSslException('BN_set_word failed');
    }

    if (bindings.RSA_generate_key_ex(rsa, bits, bne, nullptr) != 1) {
      throw OpenSslException('RSA_generate_key_ex failed');
    }

    final pkey = bindings.EVP_PKEY_new();
    if (pkey == nullptr) throw OpenSslException('EVP_PKEY_new failed');

    if (bindings.EVP_PKEY_set1_RSA(pkey, rsa) != 1) {
      bindings.EVP_PKEY_free(pkey);
      throw OpenSslException('EVP_PKEY_set1_RSA failed');
    }

    // Retorna o wrapper injetando 'this' como contexto
    // cast implicito 'this' satisfaz OpenSSL (a classe principal)
    return EvpPkey(pkey, this as dynamic);
  } finally {
    bindings.RSA_free(rsa);
    bindings.BN_free(bne);
  }
}