Created
February 20, 2025 13:47
-
-
Save salrashid123/5ea2439017199a024adc395fcfe183c2 to your computer and use it in GitHub Desktop.
hmacsha256 with openssl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <sys/socket.h> | |
#include <arpa/inet.h> | |
#include <openssl/ssl.h> | |
#include <openssl/err.h> | |
#include <openssl/hmac.h> | |
#include <stdlib.h> | |
// gcc hmacsha256.c -lcrypto -lssl -o h | |
unsigned char* hmac_sha256(const char *key, int keylen, unsigned char *data, int datalen, unsigned char *result, unsigned int *resultlen) { | |
return HMAC(EVP_sha256(), key, keylen, (unsigned char*)data, datalen, result, resultlen); | |
} | |
int main(int argc, char **argv) | |
{ | |
char key[] = "mysecretkey"; | |
char data[] = "This is the message to be authenticated"; | |
unsigned char result[EVP_MAX_MD_SIZE]; | |
unsigned int resultlen; | |
hmac_sha256(key, strlen(key), data, strlen(data), result, &resultlen); | |
printf("HMAC-SHA256: "); | |
for (int i = 0; i < resultlen; i++) { | |
printf("%02x", result[i]); | |
} | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment