-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsign.hpp
53 lines (44 loc) · 1.85 KB
/
sign.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// using Crypto++ library version 5.6.5 from https://www.cryptopp.com/
#include <string>
#include <cstdlib>
#include "cryptopp/cryptlib.h"
#include "cryptopp/hmac.h"
#include "cryptopp/sha.h"
#include "cryptopp/base64.h"
#include "cryptopp/filters.h"
static std::string calculateHMAC(const std::string &key, const std::string &canonicalRequest, bool debug = false)
{
std::string decoded_key, calculatedHmac, encodedHmac;
if (debug)
{
std::cerr << "key: " << key << std::endl;
std::cerr << "canonicalRequest: " << canonicalRequest << std::endl;
}
try
{
// Decode the key since it is base64url encoded
CryptoPP::StringSource(key, true,
new CryptoPP::Base64URLDecoder(
new CryptoPP::StringSink(decoded_key)) // Base64URLDecoder
); // StringSource
// Calculate HMAC
CryptoPP::HMAC<CryptoPP::SHA256> hmac((byte *)decoded_key.c_str(), decoded_key.size());
CryptoPP::StringSource(canonicalRequest, true,
new CryptoPP::HashFilter(hmac,
new CryptoPP::StringSink(calculatedHmac)) // HashFilter
); // StringSource
}
catch (const CryptoPP::Exception &e)
{
std::cerr << e.what() << std::endl;
abort();
}
if (debug)
{
std::cerr << "encodedHmac: " << encodedHmac << std::endl;
std::cerr << "encodedHmac.size(): " << encodedHmac.size() << std::endl;
std::cerr << "calculatedHmac: " << calculatedHmac << std::endl;
std::cerr << "calculatedHmac.size(): " << calculatedHmac.size() << std::endl;
}
return calculatedHmac;
}