I don't know which module you are using to provide hmac_sha1_hex
, but instead I recommend the Digest
family of modules. If you use Digest::HMAC
in combination with Digest::SHA1
you can calculate a SHA1 HMAC, and the translation from a hex string to binary is done with pack
.
This code parcels the whole thing up into 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
I overlooked that there is also a Digest::HMAC_SHA1
module that does all this for you and makes the code simpler still.
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 that of the previous code.
Update
Just to complete the set, this is how to do it using the procedural interface of Digest::HMAC
instead of the 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've just read your answer to my comment. I didn't realise there was HMAC functionality written into Digest::SHA
. Using that module and its hmac_sha1_hex
call, all there is left is to perform the pack
calls on the hex 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);
}