regaddr_3ld.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <ctime>
  5. #include "Identity.h"
  6. #include "Base.h"
  7. static void help ()
  8. {
  9. std::cout << "Usage:" << std::endl;
  10. std::cout << "\treg3ldaddr step1 privkey address" << std::endl;
  11. std::cout << "\treg3ldaddr step2 step1file oldprivkey oldaddress" << std::endl;
  12. std::cout << "\treg3ldaddr step3 step2file privkey" << std::endl;
  13. }
  14. int main (int argc, char * argv[])
  15. {
  16. if (argc < 3) { help(); return -1;}
  17. std::string arg = argv[1];
  18. i2p::crypto::InitCrypto (false, true, true, false);
  19. i2p::data::PrivateKeys keys;
  20. if (arg == "step1") {
  21. std::ifstream s(argv[2], std::ifstream::binary);
  22. if (s.is_open ()) {
  23. s.seekg (0, std::ios::end);
  24. size_t len = s.tellg();
  25. s.seekg (0, std::ios::beg);
  26. uint8_t * buf = new uint8_t[len];
  27. s.read ((char *)buf, len);
  28. if(keys.FromBuffer (buf, len)) {
  29. std::stringstream out;
  30. out << argv[3] << "="; // address
  31. out << keys.GetPublic ()->ToBase64 ();
  32. out << "#!action=addsubdomain";
  33. std::cout << out.str () << std::endl;
  34. } else
  35. std::cout << "Failed to load keyfile " << argv[1] << std::endl;
  36. delete[] buf;
  37. }
  38. }
  39. else if (arg == "step2") {
  40. std::ifstream t(argv[2]);
  41. std::ifstream s(argv[3], std::ifstream::binary);
  42. std::string regtxt;
  43. std::stringstream out;
  44. if (t.is_open ()) {
  45. while (t.good()) {
  46. getline (t, regtxt);
  47. out << regtxt;
  48. }
  49. t.close();
  50. } else {
  51. std::cout << "Failed to read file with STEP1 output";
  52. exit(1);
  53. }
  54. if (s.is_open ()) {
  55. s.seekg (0, std::ios::end);
  56. size_t len = s.tellg();
  57. s.seekg (0, std::ios::beg);
  58. uint8_t * buf = new uint8_t[len];
  59. s.read ((char *)buf, len);
  60. if(keys.FromBuffer (buf, len)) {
  61. auto signatureLen = keys.GetPublic ()->GetSignatureLen ();
  62. uint8_t * signature = new uint8_t[signatureLen];
  63. char * sig = new char[signatureLen*2];
  64. out << "#date=" << std::time(nullptr);
  65. out << "#olddest=" << keys.GetPublic ()->ToBase64 ();
  66. out << "#oldname=" << argv[4];
  67. keys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), signature);
  68. auto len = i2p::data::ByteStreamToBase64 (signature, signatureLen, sig, signatureLen*2);
  69. sig[len] = 0;
  70. out << "#oldsig=" << sig;
  71. delete[] signature;
  72. delete[] sig;
  73. std::cout << out.str () << std::endl;
  74. } else
  75. std::cout << "Failed to load keyfile " << argv[1] << std::endl;
  76. delete[] buf;
  77. }
  78. }
  79. else if (arg == "step3") {
  80. std::ifstream t(argv[2]);
  81. std::ifstream s(argv[3], std::ifstream::binary);
  82. std::string regtxt;
  83. std::stringstream out;
  84. if (t.is_open ()) {
  85. while (t.good()) {
  86. getline (t, regtxt);
  87. out << regtxt;
  88. }
  89. t.close();
  90. } else {
  91. std::cout << "Failed to read file with STEP2 output";
  92. exit(1);
  93. }
  94. if (s.is_open ()) {
  95. s.seekg (0, std::ios::end);
  96. size_t len = s.tellg();
  97. s.seekg (0, std::ios::beg);
  98. uint8_t * buf = new uint8_t[len];
  99. s.read ((char *)buf, len);
  100. if(keys.FromBuffer (buf, len)) {
  101. auto signatureLen = keys.GetPublic ()->GetSignatureLen ();
  102. uint8_t * signature = new uint8_t[signatureLen];
  103. char * sig = new char[signatureLen*2];
  104. keys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), signature);
  105. auto len = i2p::data::ByteStreamToBase64 (signature, signatureLen, sig, signatureLen*2);
  106. sig[len] = 0;
  107. out << "#sig=" << sig;
  108. delete[] signature;
  109. delete[] sig;
  110. std::cout << out.str () << std::endl;
  111. } else
  112. std::cout << "Failed to load keyfile " << argv[1] << std::endl;
  113. delete[] buf;
  114. }
  115. }
  116. else {
  117. help(); exit(1);
  118. }
  119. i2p::crypto::TerminateCrypto ();
  120. return 0;
  121. }