Created
August 24, 2014 19:51
-
-
Save FROGGS/e7fd26b9e41ed4c5bc3b to your computer and use it in GitHub Desktop.
almost Digest::PSHA1 for Perl 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use feature qw(say); | |
use MIME::Base64; | |
use Digest::SHA1 qw(sha1); | |
use Digest::HMAC qw(hmac); | |
# Calculates Pseudorandom SHA1 as defined in http://tools.ietf.org/html/rfc5246 (5. HMAC and the Pseudorandom Function) | |
sub psha1 { | |
my $secret = shift; | |
my $seed = shift; | |
my $keySize = shift || 256; | |
my $clientBytes = decode_base64($secret); | |
my $serverBytes = decode_base64($seed); | |
my $sizeBytes = $keySize / 8; | |
my $sha1DigestSizeBytes = 160 / 8; # 160 is the length of sha1 digest | |
my $buffer1 = $serverBytes; | |
my $buffer2; | |
my $pshaBuffer; | |
my $i = 0; | |
my $temp; | |
while ($i < $sizeBytes) { | |
$buffer1 = hmac($buffer1, $clientBytes, \&sha1, 64); | |
$buffer2 = $buffer1; | |
substr($buffer2, $sha1DigestSizeBytes) = $serverBytes; | |
$temp = hmac($buffer2, $clientBytes, \&sha1, 64); | |
for (my $x = 0; $x < length($temp); $x++) { | |
if ($i < $sizeBytes) { | |
substr($pshaBuffer, $i, 1) = substr($temp, $x, 1); | |
$i++; | |
} else { | |
last; | |
} | |
} | |
} | |
return encode_base64($pshaBuffer, ''); | |
} | |
say psha1("grrlUUfhuNwlvQzQ4bV6TT3wA8ieZPltIf4+H7nIvCE=", "YLABh3ZmZyiO5gvVLZe9J4JPd9w59KGeTFwE85XlzxE="); | |
say psha1("YLABh3ZmZyiO5gvVLZe9J4JPd9w59KGeTFwE85XlzxE=", "grrlUUfhuNwlvQzQ4bV6TT3wA8ieZPltIf4+H7nIvCE="); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment