Skip to content

Instantly share code, notes, and snippets.

@landgenoot
Last active May 14, 2017 13:38
Show Gist options
  • Save landgenoot/6f9a58876ea0f03a48b9485159ad7869 to your computer and use it in GitHub Desktop.
Save landgenoot/6f9a58876ea0f03a48b9485159ad7869 to your computer and use it in GitHub Desktop.
// g++ -Wall ecdsa-test.cpp -o ecdsa-test -lcryptopp && ./ecdsa-test
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include "cryptopp/eccrypto.h"
using CryptoPP::ECP;
using CryptoPP::ECDSA;
#include "cryptopp/sha.h"
using CryptoPP::SHA256;
#include "cryptopp/queue.h"
using CryptoPP::ByteQueue;
#include "cryptopp/oids.h"
using CryptoPP::OID;
// ASN1 is a namespace, not an object
#include "cryptopp/asn.h"
using namespace CryptoPP::ASN1;
#include "cryptopp/files.h"
using CryptoPP::FileSource;
using CryptoPP::FileSink;
#include "cryptopp/integer.h"
using CryptoPP::Integer;
#include "cryptopp/cryptlib.h"
using CryptoPP::PublicKey;
using CryptoPP::BufferedTransformation;
void LoadPublicKey(const string& filename, PublicKey& key);
void Load(const string& filename, BufferedTransformation& bt);
void LoadPublicKey(const string& filename, PublicKey& key)
{
ByteQueue queue;
Load(filename, queue);
key.Load(queue);
}
void Load(const string& filename, BufferedTransformation& bt)
{
FileSource file(filename.c_str(), true /*pumpAll*/);
file.TransferTo(bt);
bt.MessageEnd();
}
void LoadFile(const string& filename, std::vector<char>& buffer)
{
std::ifstream file(filename.c_str(), std::ios::binary | std::ios::ate);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
file.read(buffer.data(), size);
}
int main()
{
ECDSA<ECP, SHA256>::PublicKey pubKey;
LoadPublicKey("public", pubKey);
ECDSA<ECP, SHA256>::Verifier verifier( pubKey );
std::vector<char> signature(80);
std::vector<char> data(8);
LoadFile("signature", signature);
LoadFile("data", data);
bool result = verifier.VerifyMessage( (const byte*)data.data(), data.size(), (const byte*)signature.data(), signature.size() );
if(result)
cout << "Verified signature on message" << endl;
else
cerr << "Failed to verify signature on message" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment