keygen.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include "Crypto.h"
  5. #include "Identity.h"
  6. #include "common/key.hpp"
  7. int main (int argc, char * argv[])
  8. {
  9. if (argc < 2)
  10. {
  11. std::cout << "Usage: keygen filename <signature type>" << std::endl;
  12. return -1;
  13. }
  14. i2p::crypto::InitCrypto (false, true, true, false);
  15. i2p::data::SigningKeyType type = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519;
  16. if (argc > 2) {
  17. std::string str(argv[2]);
  18. type = NameToSigType(str);
  19. if (SigTypeToName(type).find("unknown") != std::string::npos) { std::cerr << "Incorrect signature type" << std::endl; return -2; }
  20. }
  21. auto keys = i2p::data::PrivateKeys::CreateRandomKeys (type);
  22. std::ofstream f (argv[1], std::ofstream::binary | std::ofstream::out);
  23. if (f)
  24. {
  25. size_t len = keys.GetFullLen ();
  26. uint8_t * buf = new uint8_t[len];
  27. len = keys.ToBuffer (buf, len);
  28. f.write ((char *)buf, len);
  29. delete[] buf;
  30. std::cout << "Destination " << keys.GetPublic ()->GetIdentHash ().ToBase32 () << " created" << std::endl;
  31. std::cout << "Signature type: " << SigTypeToName(type) << " (" << type << ")" << std::endl;
  32. }
  33. else
  34. std::cout << "Can't create file " << argv[1] << std::endl;
  35. i2p::crypto::TerminateCrypto ();
  36. return 0;
  37. }