main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <map>
  5. #include <mutex>
  6. #include <MqttDef.h>
  7. #include <MqttClient/MqttApi.h>
  8. #include <proto/MqttLog.h>
  9. #include <proto/ProtoParser.h>
  10. #include <proto/Crypt.h>
  11. #include <assert.h>
  12. void TestDecode()
  13. {
  14. auto cid = "YVQuIJXwW1k77yO0yLWWxvtwppyNOr4sQtDMv4f+7vIuBudxMOtZSttSJkc+gXsX";
  15. MqttLog(LOG_ERROR, LOG_TYPE_FUNC, "aes deocde: %s", cid);
  16. auto len = base64DecodeLength(cid, strlen(cid));
  17. auto cbytes = new uint8_t[len];
  18. auto retLen = base64Decode(cbytes, len, (uint8_t *)cid, strlen(cid));
  19. auto need = aesDecryptLength(retLen);
  20. uint8_t *outBytes = new uint8_t[need];
  21. auto outLen = aesDecrypt(cbytes, retLen, nullptr, outBytes);
  22. outBytes[outLen] = 0;
  23. MqttLog(LOG_ERROR, LOG_TYPE_FUNC, "aes deocde result: %s", outBytes + 4);
  24. delete[] outBytes;
  25. delete[] cbytes;
  26. auto pas = "hN0AF2XX6+rD4vZEfafQo9Ant7O26OAqS9U9b8qUqg5qSDFfLwhaiQ==";
  27. MqttLog(LOG_ERROR, LOG_TYPE_FUNC, "des deocde: %s", pas);
  28. len = base64DecodeLength(pas, strlen(pas));
  29. cbytes = new uint8_t[len];
  30. retLen = base64Decode(cbytes, len, (uint8_t *)pas, strlen(pas));
  31. need = desDecryptLength(retLen);
  32. outBytes = new uint8_t[need];
  33. outLen = desDecrypt(cbytes, retLen, outBytes);
  34. outBytes[outLen] = 0;
  35. MqttLog(LOG_ERROR, LOG_TYPE_FUNC, "des deocde result: %s", outBytes);
  36. delete[] outBytes;
  37. delete[] cbytes;
  38. }
  39. void TestAES()
  40. {
  41. int len = 0;
  42. uint8_t *tmp = new uint8_t[200];
  43. uint8_t *tmp2 = new uint8_t[200];
  44. uint8_t *out = new uint8_t[200];
  45. for (size_t i = 0; i < 100; i++)
  46. {
  47. tmp[i] = i;
  48. }
  49. aesEncrypt((uint8_t *)"aabb", 4, nullptr, out);
  50. char *str = "LFwMv5a3x2bL0MaOz6ZGu3J6uQkYpD8mu///sYnHITtric8ejkHiI4kq86C+pIjyk3omkyxusUWngLiCzDTodGRl1OfRsXS8FMPivQ52LybxrnvuAi4UmuOCYoctqJ811AGmsHT1BhQYHfSRA5mpyQ==";
  51. int n = base64Decode(tmp, 160, (uint8_t *)str, strlen(str));
  52. base64Encode((char *)tmp2, 160, tmp, n);
  53. assert(strcmp(str, (char *)tmp2) == 0);
  54. n = aesDecrypt(tmp, n, nullptr, tmp2);
  55. tmp2[n] = 0;
  56. for (size_t i = 0; i < 50; i++)
  57. {
  58. auto need = aesEncryptLength(i);
  59. if (need <= 0)
  60. continue;
  61. auto dlen = aesEncrypt(tmp, i, nullptr, out);
  62. memset(&out[dlen], 0, 10);
  63. need = aesDecryptLength(dlen);
  64. dlen = aesDecrypt(out, dlen, nullptr, tmp2);
  65. assert(dlen == i);
  66. auto same = memcmp(tmp, tmp2, i);
  67. assert(same == 0);
  68. }
  69. }
  70. void TestDES()
  71. {
  72. int len = 0;
  73. uint8_t *tmp = new uint8_t[100];
  74. uint8_t *tmp2 = new uint8_t[100];
  75. uint8_t *out = new uint8_t[200];
  76. for (size_t i = 0; i < 100; i++)
  77. {
  78. tmp[i] = i;
  79. }
  80. for (size_t i = 0; i < 50; i++)
  81. {
  82. auto need = desEncryptLength(i);
  83. if (need <= 0)
  84. continue;
  85. auto dlen = desEncrypt(tmp, i, out);
  86. memset(&out[dlen], 0, 10);
  87. need = desDecryptLength(dlen);
  88. dlen = desDecrypt(out, dlen, tmp2);
  89. assert(dlen == i);
  90. auto same = memcmp(tmp, tmp2, i);
  91. assert(same == 0);
  92. }
  93. }
  94. int main()
  95. {
  96. auto str = "AJBd+jzQToy8y2zl7199VsSD8hvVgJToyzDu07SCS06q";
  97. ProtoParser parser;
  98. parser.setToken("", "aabb", "90d86418-9fd9-413c-a5e4-ea81d2897a13");
  99. uint8_t bytes[] = {64, 53, 0, 1, 0, 0, 0, 235, 81, 247, 181, 34, 89, 204, 6, 118, 140, 79, 102, 119, 239, 109, 79, 129, 198, 31, 215, 153, 218, 57, 196, 215, 66, 4, 159, 68, 146, 1, 127, 53, 42, 39, 227, 147, 164, 123, 244, 1, 1, 13, 114, 144, 9, 121, 81};
  100. std::vector<uint8_t> data;
  101. data.insert(data.end(), bytes, bytes + sizeof(bytes));
  102. parser.parse(data);
  103. auto s = parser.connectPack("test01#7548");
  104. auto a = parser.getUserInfo("{\"request\":[{\"uid\":\"test01#7549\"}]}", "");
  105. TestAES();
  106. TestDES();
  107. //TestDecode();
  108. auto mqtt = mqttCreate("tcp://im.zljyhz.com:8993/");
  109. if (mqtt == nullptr)
  110. {
  111. printf("create mqtt failed.");
  112. return -1;
  113. }
  114. mqttDestory(mqtt);
  115. return 0;
  116. }