Skip to content

Instantly share code, notes, and snippets.

@gamunu
Forked from woodja/hmac.cpp
Created May 29, 2014 17:18
Show Gist options
  • Save gamunu/dd74f701b0abbab2371d to your computer and use it in GitHub Desktop.
Save gamunu/dd74f701b0abbab2371d to your computer and use it in GitHub Desktop.
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include "cryptlib.h"
using CryptoPP::Exception;
#include "hmac.h"
using CryptoPP::HMAC;
#include "sha.h"
using CryptoPP::SHA256;
#include "base64.h"
using CryptoPP::Base64Encoder;
#include "filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::HashFilter;
string sign(string key, string plain)
{
string mac, encoded;
try
{
HMAC< SHA256 > hmac((byte*)key.c_str(), key.length());
StringSource(plain, true,
new HashFilter(hmac,
new StringSink(mac)
) // HashFilter
); // StringSource
}
catch(const CryptoPP::Exception& e)
{
cerr << e.what() << endl;
}
encoded.clear();
StringSource(mac, true,
new Base64Encoder(
new StringSink(encoded)
) // Base64Encoder
); // StringSource
return encoded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment