HmacSha1Signer.cc 3.1 KB

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