regaddralias.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include "Identity.h"
  5. #include "Base.h"
  6. int main (int argc, char * argv[])
  7. {
  8. if (argc < 3)
  9. {
  10. std::cout << "Usage: regaddralias oldfilename newfilename address" << std::endl;
  11. return -1;
  12. }
  13. i2p::crypto::InitCrypto (false, true, true, false);
  14. i2p::data::PrivateKeys oldkeys, newkeys;
  15. {
  16. std::ifstream s(argv[1], std::ifstream::binary);
  17. if (s.is_open ())
  18. {
  19. s.seekg (0, std::ios::end);
  20. size_t len = s.tellg();
  21. s.seekg (0, std::ios::beg);
  22. uint8_t * buf = new uint8_t[len];
  23. s.read ((char *)buf, len);
  24. if(!oldkeys.FromBuffer (buf, len))
  25. {
  26. std::cout << "Failed to load keyfile " << argv[1] << std::endl;
  27. return -1;
  28. }
  29. delete[] buf;
  30. }
  31. else
  32. {
  33. std::cout << "Can't open keyfile " << argv[1] << std::endl;
  34. return -1;
  35. }
  36. }
  37. {
  38. std::ifstream s(argv[2], std::ifstream::binary);
  39. if (s.is_open ())
  40. {
  41. s.seekg (0, std::ios::end);
  42. size_t len = s.tellg();
  43. s.seekg (0, std::ios::beg);
  44. uint8_t * buf = new uint8_t[len];
  45. s.read ((char *)buf, len);
  46. if(!newkeys.FromBuffer (buf, len))
  47. {
  48. std::cout << "Failed to load keyfile " << argv[2] << std::endl;
  49. return -1;
  50. }
  51. delete[] buf;
  52. }
  53. else
  54. {
  55. std::cout << "Can't open keyfile " << argv[2] << std::endl;
  56. return -1;
  57. }
  58. }
  59. std::stringstream out;
  60. out << argv[3] << "="; // address
  61. out << newkeys.GetPublic ()->ToBase64 ();
  62. out << "#!action=adddest#olddest=";
  63. out << oldkeys.GetPublic ()->ToBase64 ();
  64. auto oldSignatureLen = oldkeys.GetPublic ()->GetSignatureLen ();
  65. uint8_t * oldSignature = new uint8_t[oldSignatureLen];
  66. char * oldSig = new char[oldSignatureLen*2];
  67. oldkeys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), oldSignature);
  68. auto len = i2p::data::ByteStreamToBase64 (oldSignature, oldSignatureLen, oldSig, oldSignatureLen*2);
  69. oldSig[len] = 0;
  70. out << "#oldsig=" << oldSig;
  71. delete[] oldSignature;
  72. delete[] oldSig;
  73. auto signatureLen = newkeys.GetPublic ()->GetSignatureLen ();
  74. uint8_t * signature = new uint8_t[signatureLen];
  75. char * sig = new char[signatureLen*2];
  76. newkeys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), signature);
  77. len = i2p::data::ByteStreamToBase64 (signature, signatureLen, sig, signatureLen*2);
  78. sig[len] = 0;
  79. out << "#sig=" << sig;
  80. delete[] signature;
  81. delete[] sig;
  82. std::cout << out.str () << std::endl;
  83. i2p::crypto::TerminateCrypto ();
  84. return 0;
  85. }