在Perl中采用十六进制字符串的SHA1 HMAC [英] Taking the SHA1 HMAC of hex strings in Perl

查看:252
本文介绍了在Perl中采用十六进制字符串的SHA1 HMAC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串(键和数据),它们是十六进制字符串格式,我想要采用它们的HMAC。字符串为:

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

我想要生成等效的javascript jsSHA函数,其中字符串被视为十六进制字符串。此演示 http://caligatio.github.io/jsSHA/ 可让您指定密钥和数据但是,当我在Perl中使用 hmac_sha1_hex($ data,$ key)时,字符串是HEX字符串。 被视为文本。我得到这个输出为hmac_sha1_hex:

  775083be8f8c94baea8d12a5038d191cab3759ac 

如何产生与jsSHA演示相同的输出,其中两个输入都被视为十六进制,输出也是十六进制?我想要这样的输出:

  f2ea4899a8582c21610085988c54645fd7193393 


解决方案

我不知道您使用哪个模块来提供 hmac_sha1_hex ,但我建议 Digest 系列模块。如果您使用 Digest :: HMAC 结合 Digest :: SHA1 您可以计算SHA1 HMAC,并且使用 <$ c $完成从十六进制字符串到二进制的翻译c> pack



这段代码将整个东西包装成一个子程序给你。

 使用strict; 
使用警告;

使用Digest :: HMAC;
使用Digest :: SHA1;

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

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

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

输出

  f2ea4899a8582c21610085988c54645fd7193393 






更新



我忽略了还有一个 Digest :: HMAC_SHA1 模块,它为您完成所有这些工作,并使代码更简单。 p>

像这样

  use strict; 
使用警告;

使用Digest :: HMAC_SHA1 qw / hmac_sha1_hex /;

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

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

hmac_sha1_hex_string {
my($ key,$ data)=地图包('H *',$ _),@_;
hmac_sha1_hex($ data,$ key);
}

输出与前面的代码相同。






更新

这是如何使用 Digest :: HMAC 的过程接口而不是面向对象的风格来完成它的。

 使用strict; 
使用警告;

使用Digest :: HMAC qw / hmac_hex /;
使用Digest :: SHA1 qw / sha1 /;

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

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

hmac_sha1_hex_string {
my($ key,$ data)=地图包('H *',$ _),@_;
hmac_hex($ data,$ key,\& sha1);






更新



我刚刚阅读了您对我的评论的回答。我没有意识到HMAC功能写入了 Digest :: SHA 。使用该模块及其 hmac_sha1_hex 调用,剩下的就是对十六进制字符串执行 pack 调用。 / p>

  use strict; 
使用警告;

使用Digest :: SHA qw / hmac_sha1_hex /;

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

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

hmac_sha1_hex_string {
my($ key,$ data)=地图包('H *',$ _),@_;
hmac_sha1_hex($ data,$ key);
}


I have two strings (key and data) which are in hex string format, and I'd like to take the HMAC of them. The strings are:

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

I want to produce the equivalent of the javascript jsSHA function where the strings are treated as hex strings. This demo http://caligatio.github.io/jsSHA/ lets you specify that the key and data are HEX strings.

However, when I use hmac_sha1_hex($data, $key) in Perl, the strings are treated as text. I get this output for the hmac_sha1_hex:

775083be8f8c94baea8d12a5038d191cab3759ac

How do I produce 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

解决方案

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);
}

这篇关于在Perl中采用十六进制字符串的SHA1 HMAC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆