HmacSha1Signer.cc 2.9 KB

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