Skip to content

Instantly share code, notes, and snippets.

View devendranaga's full-sized avatar
🍈

Dev devendranaga

🍈
  • <>
View GitHub Profile
#include <iostream>
void (*mem_fun_cb)(void);
class mem_fun {
private:
public:
mem_fun() { }
~mem_fun() { }
#include <iostream>
#include <functional>
void f()
{
std::cout << "called f" << std::endl;
}
int main()
{
// callback declaration
void (*callback)(int);
void print_func(int a)
{
printf("%d\n", a);
}
int register_print_callback()
{
static void (*event_callback)(void *user_priv, uint8_t *datap, size_t datap_len);
static void *user_priv;
int register_event_callback(void *user_ptr)
{
event_callback = my_event_callback;
user_priv = user_ptr;
return 0;
}
@devendranaga
devendranaga / ecc_demo.cc
Last active February 14, 2019 05:11
ECC keygen / sign and verify in C++
#include <iostream>
#include <string>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/ecdsa.h>
#include <openssl/ec.h>
#include <openssl/conf.h>
#include <openssl/rand.h>
#include <openssl/pem.h>
#include <openssl/sha.h>
int generate_keys(std::string pubkeyfile, std::string privkeyfile, std::string curve_name)
{
EC_KEY *keygen;
int nid = to_nid(curve_name);
if (nid == -1) {
return -1;
}
// get curve name
// loads in the pubkey
int load_pubkey(std::string pubkey)
{
FILE *fp;
// load in the keys
fp = fopen(pubkey.c_str(), "r");
if (!fp) {
return -1;
}
int load_privkey(std::string privkey)
{
FILE *fp;
fp = fopen(privkey.c_str(), "r");
if (!fp) {
return -1;
}
privatekey = PEM_read_ECPrivateKey(fp, NULL, NULL, NULL);
int sign(uint8_t *msg, size_t msglen, std::string sha_alg)
{
if (!evp_sign_key || !privatekey) {
std::cerr << "invalid sign key or private key is not loaded" << std::endl;
return -1;
}
const EVP_MD *md;
// mark the sha alg to use
int verify(uint8_t *msg, size_t msglen, uint8_t *signature, size_t signature_len, std::string sha_alg)
{
if (!msg || !signature) {
std::cerr << "invalid msg or signature" << std::endl;
return -1;
}
const EVP_MD *md;
if (sha_alg == "sha256") {