Skip to content

Instantly share code, notes, and snippets.

@nullren
Forked from varnit/1. hmac.pl
Created September 21, 2011 22:32
Show Gist options
  • Save nullren/1233509 to your computer and use it in GitHub Desktop.
Save nullren/1233509 to your computer and use it in GitHub Desktop.
c hmac
#!/usr/bin/perl
use strict;
use warnings;
use Digest::SHA qw(hmac_sha1_hex);
print hmac_sha1_hex("my data", "secret key") . "\n";
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <openssl/hmac.h>
int main() {
char* key = "secret key";
char* data = "my data";
unsigned int result_len = 40;
unsigned char* result = malloc(sizeof(unsigned char) * result_len);
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, (unsigned char*) key, strlen(key), EVP_sha1(), NULL);
HMAC_Update(&ctx, (unsigned char*) data, strlen(data));
HMAC_Final(&ctx, result, &result_len);
HMAC_CTX_cleanup(&ctx);
//printf("output : %s\n", (char*)result);
printf("output : ");
int i;
for(i=0; i<result_len; i++)
printf("%.2x", result[i]);
printf("\n");
printf("expected: %s\n", "97a62219092f3ddab3707cc9e85e63e926f45c87");
return 0;
}
$ perl hmac.pl
97a62219092f3ddab3707cc9e85e63e926f45c87
$ gcc -Wall -lcrypto hmac.c -o hmac && ./hmac
output : ��" /=ڳp|��^c�&�\�
expected: 97a62219092f3ddab3707cc9e85e63e926f45c87
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment