Utils.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2009-2017 Alibaba Cloud All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "Utils.h"
  17. #include <curl/curl.h>
  18. #ifdef _WIN32
  19. #include <Windows.h>
  20. #else
  21. #include <openssl/hmac.h>
  22. #include <openssl/md5.h>
  23. #include <uuid/uuid.h>
  24. #endif
  25. std::string AlibabaCloud::GenerateUuid()
  26. {
  27. #ifdef _WIN32
  28. char *data;
  29. UUID uuidhandle;
  30. UuidCreate(&uuidhandle);
  31. UuidToString(&uuidhandle, (RPC_CSTR*)&data);
  32. std::string uuid(data);
  33. RpcStringFree((RPC_CSTR*)&data);
  34. return uuid;
  35. #else
  36. uuid_t uu;
  37. uuid_generate(uu);
  38. char buf[36];
  39. uuid_unparse(uu, buf);
  40. return buf;
  41. #endif
  42. }
  43. std::string AlibabaCloud::UrlEncode(const std::string & src)
  44. {
  45. CURL *curl = curl_easy_init();
  46. char *output = curl_easy_escape(curl, src.c_str(), src.size());
  47. std::string result(output);
  48. curl_free(output);
  49. return result;
  50. }
  51. std::string AlibabaCloud::UrlDecode(const std::string & src)
  52. {
  53. CURL *curl = curl_easy_init();
  54. int outlength = 0;
  55. char *output = curl_easy_unescape(curl, src.c_str(), src.size(), &outlength);
  56. std::string result(output, outlength);
  57. curl_free(output);
  58. return result;
  59. }
  60. std::string AlibabaCloud::ComputeContentMD5(const char * data, size_t size)
  61. {
  62. #ifdef _WIN32
  63. HCRYPTPROV hProv = 0;
  64. HCRYPTHASH hHash = 0;
  65. BYTE pbHash[16];
  66. DWORD dwDataLen = 16;
  67. CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
  68. CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
  69. CryptHashData(hHash, (BYTE*)(data), size, 0);
  70. CryptGetHashParam(hHash, HP_HASHVAL, pbHash, &dwDataLen, 0);
  71. CryptDestroyHash(hHash);
  72. CryptReleaseContext(hProv, 0);
  73. DWORD dlen = 0;
  74. CryptBinaryToString(pbHash, dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &dlen);
  75. char* dest = new char[dlen];
  76. CryptBinaryToString(pbHash, dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, dest, &dlen);
  77. std::string ret = std::string(dest, dlen);
  78. delete dest;
  79. return ret;
  80. #else
  81. unsigned char md[MD5_DIGEST_LENGTH];
  82. MD5(reinterpret_cast<const unsigned char*>(data), size, (unsigned char*)&md);
  83. char encodedData[100];
  84. EVP_EncodeBlock(reinterpret_cast<unsigned char*>(encodedData), md, MD5_DIGEST_LENGTH);
  85. return encodedData;
  86. #endif
  87. }
  88. void AlibabaCloud::StringReplace(std::string & src, const std::string & s1, const std::string & s2)
  89. {
  90. std::string::size_type pos =0;
  91. while ((pos = src.find(s1, pos)) != std::string::npos)
  92. {
  93. src.replace(pos, s1.length(), s2);
  94. pos += s2.length();
  95. }
  96. }
  97. std::string AlibabaCloud::HttpMethodToString(HttpRequest::Method method)
  98. {
  99. switch (method)
  100. {
  101. case HttpRequest::Method::Head:
  102. return "HEAD";
  103. break;
  104. case HttpRequest::Method::Post:
  105. return "POST";
  106. break;
  107. case HttpRequest::Method::Put:
  108. return "PUT";
  109. break;
  110. case HttpRequest::Method::Delete:
  111. return "DELETE";
  112. break;
  113. case HttpRequest::Method::Connect:
  114. return "CONNECT";
  115. break;
  116. case HttpRequest::Method::Options:
  117. return "OPTIONS";
  118. break;
  119. case HttpRequest::Method::Patch:
  120. return "PATCH";
  121. break;
  122. case HttpRequest::Method::Trace:
  123. return "TRACE";
  124. break;
  125. case HttpRequest::Method::Get:
  126. default:
  127. return "GET";
  128. break;
  129. }
  130. }