Skip to content

Instantly share code, notes, and snippets.

@varnit
Created September 21, 2011 21:01
Show Gist options
  • Select an option

  • Save varnit/1233303 to your computer and use it in GitHub Desktop.

Select an option

Save varnit/1233303 to your computer and use it in GitHub Desktop.

Revisions

  1. varnit revised this gist Sep 22, 2011. 2 changed files with 26 additions and 16 deletions.
    40 changes: 25 additions & 15 deletions 2. hmac.c
    Original file line number Diff line number Diff line change
    @@ -5,20 +5,30 @@

    int main() {

    char* key = "secret key";
    char* data = "my data";
    unsigned int result_len = 40;
    unsigned char* result = malloc(sizeof(unsigned char) * result_len);
    char* key = "secret key";
    char* data = "my data";
    char tmp[3];
    unsigned int result_len, i;
    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);
    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("expected: %s\n", "97a62219092f3ddab3707cc9e85e63e926f45c87");

    return 0;
    }
    char* output = malloc(sizeof(char) * result_len);
    for (i = 0; i != result_len; i++)
    {
    //printf("Got %02X at byte %d\n", result[i], i);
    sprintf(tmp, "%02x", result[i]);
    strcat(output, tmp);
    }


    printf("output : %s\n", output);
    printf("expected: %s\n", "97a62219092f3ddab3707cc9e85e63e926f45c87");

    return 0;
    }
    2 changes: 1 addition & 1 deletion gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -2,5 +2,5 @@ $ perl hmac.pl
    97a62219092f3ddab3707cc9e85e63e926f45c87

    $ gcc -Wall -lcrypto hmac.c -o hmac && ./hmac
    output : ��" /=ڳp|��^c�&�\�
    output : 97a62219092f3ddab3707cc9e85e63e926f45c87
    expected: 97a62219092f3ddab3707cc9e85e63e926f45c87
  2. varnit revised this gist Sep 21, 2011. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  3. varnit revised this gist Sep 21, 2011. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
  4. varnit revised this gist Sep 21, 2011. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions hmac.c
    Original file line number Diff line number Diff line change
    @@ -7,9 +7,8 @@ int main() {

    char* key = "secret key";
    char* data = "my data";
    unsigned char* result;
    unsigned int result_len = 40;
    result = (unsigned char*) malloc(sizeof(char) * result_len);
    unsigned char* result = malloc(sizeof(unsigned char) * result_len);

    HMAC_CTX ctx;
    HMAC_CTX_init(&ctx);
    @@ -22,4 +21,4 @@ int main() {
    printf("expected: %s\n", "97a62219092f3ddab3707cc9e85e63e926f45c87");

    return 0;
    }
    }
  5. varnit created this gist Sep 21, 2011.
    6 changes: 6 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    $ perl hmac.pl
    97a62219092f3ddab3707cc9e85e63e926f45c87

    $ gcc -Wall -lcrypto hmac.c -o hmac && ./hmac
    output : ��" /=ڳp|��^c�&�\�
    expected: 97a62219092f3ddab3707cc9e85e63e926f45c87
    25 changes: 25 additions & 0 deletions hmac.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <openssl/hmac.h>

    int main() {

    char* key = "secret key";
    char* data = "my data";
    unsigned char* result;
    unsigned int result_len = 40;
    result = (unsigned char*) malloc(sizeof(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("expected: %s\n", "97a62219092f3ddab3707cc9e85e63e926f45c87");

    return 0;
    }
    8 changes: 8 additions & 0 deletions hmac.pl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    #!/usr/bin/perl

    use strict;
    use warnings;
    use Digest::SHA qw(hmac_sha1_hex);

    print hmac_sha1_hex("my data", "secret key") . "\n";