I want to implement OAuth 1.0
protocol in my C++ project. In order to create OAuth signature I need to implement HMAC-SHA1
algorithm where key
and text
will be some string created according to OAuth specification.
I want to use Crypto++ library for implementing HMAC-SHA1. I found this HMAC-SHA1 example on wiki of project:
AutoSeededRandomPool prng;
SecByteBlock key(16);
prng.GenerateBlock(key, key.size());
string plain = "HMAC Test";
string mac, encoded;
/*********************************\
\*********************************/
// Pretty print key
encoded.clear();
StringSource(key, key.size(), true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "key: " << encoded << endl;
cout << "plain text: " << plain << endl;
/*********************************\
\*********************************/
try
{
HMAC< SHA256 > hmac(key, key.size());
StringSource(plain, true,
new HashFilter(hmac,
new StringSink(mac)
) // HashFilter
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
exit(1);
}
/*********************************\
\*********************************/
// Pretty print
encoded.clear();
StringSource(mac, true,
new HexEncoder(
new StringSink(encoded)
) // HexEncoder
); // StringSource
cout << "hmac: " << encoded << endl;
But I can't understand how instead of random generated string use my created key
. I tried just create:
string key=...; //string generated by OAuth specification;
But then appear compiling errors. However when I write:
string plain=...; //string generated by OAuth specification;
Then there is no errors.
And what key length I need to specify? Because I will have keys of different lengths (with 48 and maybe 96 symbols).