Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadIdentity #654

Open
Focshole opened this issue Feb 9, 2023 · 0 comments
Open

loadIdentity #654

Focshole opened this issue Feb 9, 2023 · 0 comments

Comments

@Focshole
Copy link
Contributor

Focshole commented Feb 9, 2023

Hi,
As first, thank you for making this project. It is very useful and it looks well written.
I have noticed that there is a dht::crypto::saveIdentity function but there is no dht::crypto::loadIdentity function. I was wondering if it can be added, so that the identity can be preserved through multiple dht::runner runs.

Just in case, I have already written the code to do so:

Identity loadIdentity(const std::string &path,
                        const std::string &privkey_password = "") {
  std::ifstream pkStream(path + ".pem", std::ios::in | std::ios::binary);
  std::vector<uint8_t> pkContent((std::istreambuf_iterator<char>(pkStream)),
                                 std::istreambuf_iterator<char>());
  auto key = std::make_shared<PrivateKey>(pkContent, privkey_password);
  pkStream.close();
  // Create a certificate
  gnutls_x509_crt_t gnuCert;
  if (gnutls_x509_crt_init(&gnuCert) != GNUTLS_E_SUCCESS)
    throw std::runtime_error("Failed to initialize gnutls certificate struct");
  gnutls_datum_t crtContent;
  // Read the certificate file
  gnutls_load_file((path + ".crt").c_str(), &crtContent);
  gnutls_x509_crt_import(gnuCert, &crtContent, GNUTLS_X509_FMT_PEM);
  auto cert = std::make_shared<Certificate>(gnuCert);
  return {std::move(key), std::move(cert)};
}

Feel free to review/edit/import it as you wish!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant