HmacSha1Signer.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 1999-2019 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 <alibabacloud/core/HmacSha1Signer.h>
  17. #ifdef _WIN32
  18. #include <windows.h>
  19. #include <wincrypt.h>
  20. #else
  21. #include <openssl/hmac.h>
  22. #endif
  23. namespace AlibabaCloud {
  24. HmacSha1Signer::HmacSha1Signer() :
  25. Signer(HmacSha1, "HMAC-SHA1", "1.0") {
  26. }
  27. HmacSha1Signer::~HmacSha1Signer() {
  28. }
  29. std::string HmacSha1Signer::generate(const std::string & src,
  30. const std::string & secret) const {
  31. if (src.empty())
  32. return std::string();
  33. #ifdef _WIN32
  34. typedef struct _my_blob {
  35. BLOBHEADER hdr;
  36. DWORD dwKeySize;
  37. BYTE rgbKeyData[];
  38. }my_blob;
  39. DWORD kbLen = sizeof(my_blob) + secret.size();
  40. my_blob * kb = (my_blob *)LocalAlloc(LPTR, kbLen);
  41. kb->hdr.bType = PLAINTEXTKEYBLOB;
  42. kb->hdr.bVersion = CUR_BLOB_VERSION;
  43. kb->hdr.reserved = 0;
  44. kb->hdr.aiKeyAlg = CALG_RC2;
  45. kb->dwKeySize = secret.size();
  46. memcpy(&kb->rgbKeyData, secret.c_str(), secret.size());
  47. HCRYPTPROV hProv = 0;
  48. HCRYPTKEY hKey = 0;
  49. HCRYPTHASH hHmacHash = 0;
  50. BYTE pbHash[32];
  51. DWORD dwDataLen = 32;
  52. HMAC_INFO HmacInfo;
  53. ZeroMemory(&HmacInfo, sizeof(HmacInfo));
  54. HmacInfo.HashAlgid = CALG_SHA1;
  55. CryptAcquireContext(&hProv, NULL,
  56. MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET);
  57. CryptImportKey(hProv, (BYTE*)kb, kbLen, 0, CRYPT_IPSEC_HMAC_KEY, &hKey);
  58. CryptCreateHash(hProv, CALG_HMAC, hKey, 0, &hHmacHash);
  59. CryptSetHashParam(hHmacHash, HP_HMAC_INFO, (BYTE*)&HmacInfo, 0);
  60. CryptHashData(hHmacHash, (BYTE*)(src.c_str()), src.size(), 0);
  61. CryptGetHashParam(hHmacHash, HP_HASHVAL, pbHash, &dwDataLen, 0);
  62. LocalFree(kb);
  63. CryptDestroyHash(hHmacHash);
  64. CryptDestroyKey(hKey);
  65. CryptReleaseContext(hProv, 0);
  66. DWORD dlen = 0;
  67. CryptBinaryToString(pbHash,
  68. dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, NULL, &dlen);
  69. char* dest = new char[dlen];
  70. CryptBinaryToString(pbHash,
  71. dwDataLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, dest, &dlen);
  72. std::string ret = std::string(dest, dlen);
  73. delete dest;
  74. return ret;
  75. #else
  76. unsigned char md[EVP_MAX_BLOCK_LENGTH];
  77. unsigned int mdLen = EVP_MAX_BLOCK_LENGTH;
  78. if (HMAC(EVP_sha1(), secret.c_str(), secret.size(),
  79. reinterpret_cast<const unsigned char*>(src.c_str()), src.size(),
  80. md, &mdLen) == nullptr)
  81. return std::string();
  82. char encodedData[100];
  83. EVP_EncodeBlock(reinterpret_cast<unsigned char*>(encodedData), md, mdLen);
  84. return encodedData;
  85. #endif
  86. }
  87. } // namespace AlibabaCloud