Skip to content

Commit

Permalink
better HMAC-SHA1 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 28, 2013
1 parent 5d3f2fd commit 790e80e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 36 deletions.
47 changes: 15 additions & 32 deletions lib/Mojo/Util.pm
Expand Up @@ -134,36 +134,32 @@ sub punycode_decode {
my $input = shift;
use integer;
# Delimiter
my @output;
push @output, split //, $1 if $input =~ s/(.*)\x2d//s;
my $n = PC_INITIAL_N;
my $i = 0;
my $bias = PC_INITIAL_BIAS;
my @output;
# Consume all code points before the last delimiter
push @output, split //, $1 if $input =~ s/(.*)\x2d//s;
while (length $input) {
my $oldi = $i;
my $w = 1;
# Base to infinity in steps of base
for (my $k = PC_BASE; 1; $k += PC_BASE) {
# Digit
my $digit = ord substr $input, 0, 1, '';
$digit = $digit < 0x40 ? $digit + (26 - 0x30) : ($digit & 0x1f) - 1;
$i += $digit * $w;
my $t = $k - $bias;
$t = $t < PC_TMIN ? PC_TMIN : $t > PC_TMAX ? PC_TMAX : $t;
last if $digit < $t;
$w *= (PC_BASE - $t);
$w *= PC_BASE - $t;
}
# Bias
$bias = _adapt($i - $oldi, @output + 1, $oldi == 0);
$n += $i / (@output + 1);
$i = $i % (@output + 1);
splice @output, $i++, 0, chr $n;
}
Expand All @@ -175,54 +171,41 @@ sub punycode_encode {
my $output = shift;
use integer;
# Split input
my $n = PC_INITIAL_N;
my $delta = 0;
my $bias = PC_INITIAL_BIAS;
# Extract basic code points
my $len = length $output;
my @input = map {ord} split //, $output;
my @chars = sort grep { $_ >= PC_INITIAL_N } @input;
# Handle non-basic characters
$output =~ s/[^\x00-\x7f]+//gs;
my $h = my $b = length $output;
$output .= "\x2d" if $b > 0;
my $n = PC_INITIAL_N;
my $delta = 0;
my $bias = PC_INITIAL_BIAS;
for my $m (@chars) {
# Basic character
next if $m < $n;
# Walk all code points in order
$delta += ($m - $n) * ($h + 1);
$n = $m;
for (my $i = 0; $i < $len; $i++) {
my $c = $input[$i];
# Basic character
$delta++ if $c < $n;
# Non basic character
if ($c == $n) {
if ($c < $n) { $delta++ }
elsif ($c == $n) {
my $q = $delta;
# Base to infinity in steps of base
for (my $k = PC_BASE; 1; $k += PC_BASE) {
my $t = $k - $bias;
$t = $t < PC_TMIN ? PC_TMIN : $t > PC_TMAX ? PC_TMAX : $t;
last if $q < $t;
# Code point for digit "t"
my $o = $t + (($q - $t) % (PC_BASE - $t));
$output .= chr $o + ($o < 26 ? 0x61 : 0x30 - 26);
$q = ($q - $t) / (PC_BASE - $t);
}
# Code point for digit "q"
$output .= chr $q + ($q < 26 ? 0x61 : 0x30 - 26);
# Bias
$bias = _adapt($delta, $h + 1, $h == $b);
$delta = 0;
$h++;
Expand Down Expand Up @@ -334,8 +317,8 @@ sub xor_encode {

sub _adapt {
my ($delta, $numpoints, $firsttime) = @_;

use integer;

$delta = $firsttime ? $delta / PC_DAMP : $delta / 2;
$delta += $delta / $numpoints;
my $k = 0;
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/bytestream.t
Expand Up @@ -75,8 +75,8 @@ is b('foo bar baz')->sha1_sum, 'c7567e8b39e2428e38bf9c9226ac68de4c67dc39',
'right sha1 checksum';

# hmac_sha1_sum
is b('Hi there')->hmac_sha1_sum(1234567890),
'4fd7160f392dc54308608cae6587e137c62c2e39', 'right hmac sha1 checksum';
is b('Hi there')->hmac_sha1_sum('abc1234567890'),
'5344f37e1948dd3ffb07243a4d9201a227abd6e1', 'right hmac sha1 checksum';

# secure_compare
ok b('hello')->secure_compare('hello'), 'values are equal';
Expand Down
4 changes: 2 additions & 2 deletions t/mojo/util.t
Expand Up @@ -294,8 +294,8 @@ is sha1_sum('foo bar baz'), 'c7567e8b39e2428e38bf9c9226ac68de4c67dc39',
'right sha1 checksum';

# hmac_sha1_sum
is hmac_sha1_sum('Hi there', 1234567890),
'4fd7160f392dc54308608cae6587e137c62c2e39', 'right hmac sha1 checksum';
is hmac_sha1_sum('Hi there', 'abc1234567890'),
'5344f37e1948dd3ffb07243a4d9201a227abd6e1', 'right hmac sha1 checksum';

# secure_compare
ok secure_compare('hello', 'hello'), 'values are equal';
Expand Down

0 comments on commit 790e80e

Please sign in to comment.