OpenSSL HMAC Hasing Example in C++

This tutorial will guide you on how to hash a string by using OpenSSL’s HMAC hash function. HMAC hashing allow user to hash with a secret key. HMAC can take most of the hash engine in order to hash your data with the secret key. This tutorial will create two C++ example files which will compile and run in Ubuntu environment.

Here are the openssl HMAC sample source code.

Example #1: hmac_sample1.cpp

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

#include <stdio.h>

#include <string.h>

#include <openssl/hmac.h>

intmain()

{

// The key to hash

charkey[]="012345678";

// The data that we're going to hash using HMAC

chardata[]="hello world";

unsignedchar*digest;

// Using sha1 hash engine here.

// You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc