HMAC en perl
[ Nouvelle discussion
| Répondre au groupe
|
fr.misc.cryptologie ]
Bonjour à tous,
J'ai fait une implémentation rapide de l'algo HMAC en perl. Une petite
relecture serait la bienvenue, si certains d'entre vous ont 2 minutes
à y consacrer.....
Merci,
Julien
----------------------
#! /usr/bin/perl -w
use strict;
##################################
## implémentation HMAC RFC 2104 ##
## j. vehent - mai 2008 ##
##################################
use Digest::SHA1 qw(sha1);
use MIME::Base64;
my ($K, $text, $H); # 'K' the key, 'text' the data to compute the hmac
on, 'H' the HMAC
my $B = 64; # block size in bytes
print "\ninput HMAC key in base 64 form -> ";
$K = decode_base64(<STDIN>);
print "\ninput ASCII data to compute HMAC on -> ";
$text = <STDIN>;
# if key length > block size, reset key to sha1(key)
$K = sha1($K) if length($K) > $B;
# ipad = key xor '0x36' b times
my $k_ipad = $K ^ (chr(0x36) x $B);
# opad = key xor '0x5c' b times
my $k_opad = $K ^ (chr(0x5c) x $B);
# HMAC = sha1((key xor '0x5c'*B) || sha1((key xor '0x36'*B) || text))
# with || is concatenation
$H = sha1($k_ipad.$text);
$H = sha1($k_opad.$H);
print "\n\nHMAC = ".unpack("H*", $H)."\n\n";

|
 cette fonctionnalité est reservée aux membres ayant une session active !
|