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