Taking SHA1 HMAC hex strings in Perl

I have two strings (key and data) which are in hex string format and I would like to take HMAC from them. The lines:

$data = "0000000002ccbe80";
$key  = "48656c6c6f21deadbeef";

      

I want to create an equivalent to a javascript jsSHA function where strings are treated as hex strings. This demo http://caligatio.github.io/jsSHA/ allows you to specify that key and data are HEX strings.

However, when I use hmac_sha1_hex($data, $key)

in Perl, the lines are treated as text. I am getting this output for hmac_sha1_hex:

775083be8f8c94baea8d12a5038d191cab3759ac

      

How do I create the same output as the jsSHA demo, where both inputs are treated as hex and the output is also in hex? I want this output:

f2ea4899a8582c21610085988c54645fd7193393

      

+2


source to share


1 answer


I don't know which module you are using to provide hmac_sha1_hex

, but I recommend the module family instead Digest

. If you use Digest::HMAC

in conjunction with Digest::SHA1

you can compute SHA1 HMAC, and translation from hex string to binary is done with pack

.

This code transfers all of this to a subroutine for you.

use strict;
use warnings;

use Digest::HMAC;
use Digest::SHA1;

my $data = '0000000002ccbe80';
my $key  = '48656c6c6f21deadbeef';

print hmac_sha1_hex_string($key, $data), "\n";

sub  hmac_sha1_hex_string {
   my ($key, $data) = map pack('H*', $_), @_;
   my $hmac = Digest::HMAC->new($key, 'Digest::SHA1');
   $hmac->add($data);
   $hmac->hexdigest;
}

      

Output

f2ea4899a8582c21610085988c54645fd7193393

      


Update

What I lost sight of is that there is a module Digest::HMAC_SHA1

that does all of this for you and makes the code even simpler.

Like this

use strict;
use warnings;

use Digest::HMAC_SHA1 qw/ hmac_sha1_hex /;

my $data = '0000000002ccbe80';
my $key  = '48656c6c6f21deadbeef';

print hmac_sha1_hex_string($key, $data), "\n";

sub  hmac_sha1_hex_string {
   my ($key, $data) = map pack('H*', $_), @_;
   hmac_sha1_hex($data, $key);
}

      



The output is identical to the output of the previous code.


Update

Just to complete the set, how to do it using a procedural interface Digest::HMAC

instead of an object oriented style.

use strict;
use warnings;

use Digest::HMAC qw/ hmac_hex /;
use Digest::SHA1 qw/ sha1 /;

my $data = '0000000002ccbe80';
my $key  = '48656c6c6f21deadbeef';

print hmac_sha1_hex_string($key, $data), "\n";

sub  hmac_sha1_hex_string {
   my ($key, $data) = map pack('H*', $_), @_;
   hmac_hex($data, $key, \&sha1);
}

      


Update

I just read your answer to my comment. I didn't realize that the HMAC functionality is written in Digest::SHA

. Using this module and calling it hmac_sha1_hex

, all that's left is to make calls pack

to the hexadecimal strings.

use strict;
use warnings;

use Digest::SHA qw/ hmac_sha1_hex /;

my $data = '0000000002ccbe80';
my $key  = '48656c6c6f21deadbeef';

print hmac_sha1_hex_string($key, $data), "\n";

sub  hmac_sha1_hex_string {
   my ($key, $data) = map pack('H*', $_), @_;
   hmac_sha1_hex($data, $key);
}

      

+5


source







All Articles