Skip to content

Commit

Permalink
fixed small bug that prevented non-string secrets in Mojolicious (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 5, 2012
1 parent fb14073 commit cfb79b9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Mojolicious.

2.77 2012-04-06
- Improved documentation.
- Fixed small bug that prevented non-string secrets in Mojolicious.

2.76 2012-04-05
- Improved documentation.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Util.pm
Expand Up @@ -628,7 +628,7 @@ sub _hmac {
my $hash = $sha ? sub { sha1(@_) } : sub { md5(@_) };

# Secret
$secret ||= 'Very unsecure!';
$secret = $secret ? "$secret" : 'Very unsecure!';
$secret = $hash->($secret) if length $secret > 64;

# HMAC
Expand Down
34 changes: 19 additions & 15 deletions t/mojo/bytestream.t
Expand Up @@ -5,7 +5,7 @@ use utf8;
# "Homer, we're going to ask you a few simple yes or no questions.
# Do you understand?
# Yes. *lie dectector blows up*"
use Test::More tests => 139;
use Test::More tests => 141;

# Need to be loaded first to trigger edge case
use MIME::Base64;
Expand Down Expand Up @@ -116,9 +116,9 @@ is $stream->unquote, '"foo 23 "bar"', 'right unquoted result';
$original = b('foo bar baz ♥')->encode->to_string;
my $copy = $original;
$stream = b($copy);
is unpack('H*', $stream->md5_bytes), "a740aeb6e066f158cbf19fd92e890d2d",
is unpack('H*', $stream->md5_bytes), 'a740aeb6e066f158cbf19fd92e890d2d',
'right binary md5 checksum';
is unpack('H*', md5_bytes($copy)), "a740aeb6e066f158cbf19fd92e890d2d",
is unpack('H*', md5_bytes($copy)), 'a740aeb6e066f158cbf19fd92e890d2d',
'right binary md5 checksum';
is $copy, $original, 'still equal';

Expand All @@ -129,7 +129,7 @@ is $stream->md5_sum, 'ab07acbb1e496801937adfa772424bf7', 'right md5 checksum';
# sha1_bytes
$stream = b('foo bar baz');
is unpack('H*', $stream->sha1_bytes),
"c7567e8b39e2428e38bf9c9226ac68de4c67dc39", 'right binary sha1 checksum';
'c7567e8b39e2428e38bf9c9226ac68de4c67dc39', 'right binary sha1 checksum';

# sha1_sum
$stream = b('foo bar baz');
Expand All @@ -146,50 +146,54 @@ is $stream->size, 1, 'size is 1';
is $stream->to_string, '0', 'right content';

# hmac_md5_sum (RFC 2202)
is b("Hi There")->hmac_md5_sum(chr(0x0b) x 16),
is b('Hi There')->hmac_md5_sum(chr(0x0b) x 16),
'9294727a3638bb1c13f48ef8158bfc9d', 'right hmac md5 checksum';
is b("what do ya want for nothing?")->hmac_md5_sum("Jefe"),
is b('what do ya want for nothing?')->hmac_md5_sum('Jefe'),
'750c783e6ab0b503eaa86e310a5db738', 'right hmac md5 checksum';
is b(chr(0xdd) x 50)->hmac_md5_sum(chr(0xaa) x 16),
'56be34521d144c88dbb8c733f0e8b3f6', 'right hmac md5 checksum';
is b(chr(0xcd) x 50)
->hmac_md5_sum(
pack 'H*' => '0102030405060708090a0b0c0d0e0f10111213141516171819'),
'697eaf0aca3a3aea3a75164746ffaa79', 'right hmac md5 checksum';
is b("Test With Truncation")->hmac_md5_sum(chr(0x0c) x 16),
is b('Test With Truncation')->hmac_md5_sum(chr(0x0c) x 16),
'56461ef2342edc00f9bab995690efd4c', 'right hmac md5 checksum';
is b("Test Using Larger Than Block-Size Key - Hash Key First")
is b('Test Using Larger Than Block-Size Key - Hash Key First')
->hmac_md5_sum(chr(0xaa) x 80), '6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd',
'right hmac md5 checksum';
is b(
"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data")
'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data')
->hmac_md5_sum(chr(0xaa) x 80), '6f630fad67cda0ee1fb1f562db3aa53e',
'right hmac md5 checksum';
is b('Hi there')->hmac_md5_sum(1234567890),
'e3b5fab1b3f5b9d1fe391d09fce7b2ae', 'right hmac md5 checksum';

# hmac_sha1_sum (RFC 2202)
is b("Hi There")->hmac_sha1_sum(chr(0x0b) x 20),
is b('Hi There')->hmac_sha1_sum(chr(0x0b) x 20),
'b617318655057264e28bc0b6fb378c8ef146be00', 'right hmac sha1 checksum';
is b("what do ya want for nothing?")->hmac_sha1_sum("Jefe"),
is b('what do ya want for nothing?')->hmac_sha1_sum('Jefe'),
'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79', 'right hmac sha1 checksum';
is b(chr(0xdd) x 50)->hmac_sha1_sum(chr(0xaa) x 20),
'125d7342b9ac11cd91a39af48aa17b4f63f175d3', 'right hmac sha1 checksum';
is b(chr(0xcd) x 50)
->hmac_sha1_sum(
pack 'H*' => '0102030405060708090a0b0c0d0e0f10111213141516171819'),
'4c9007f4026250c6bc8414f9bf50c86c2d7235da', 'right hmac sha1 checksum';
is b("Test With Truncation")->hmac_sha1_sum(chr(0x0c) x 20),
is b('Test With Truncation')->hmac_sha1_sum(chr(0x0c) x 20),
'4c1a03424b55e07fe7f27be1d58bb9324a9a5a04', 'right hmac sha1 checksum';
is b("Test Using Larger Than Block-Size Key - Hash Key First")
is b('Test Using Larger Than Block-Size Key - Hash Key First')
->hmac_sha1_sum(chr(0xaa) x 80), 'aa4ae5e15272d00e95705637ce8a3b55ed402112',
'right hmac sha1 checksum';
is b(
"Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data")
'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data')
->hmac_sha1_sum(chr(0xaa) x 80),
'e8e99d0f45237d786d6bbaa7965c7808bbff1a91', 'right hmac sha1 checksum';
is b('Hi there')->hmac_sha1_sum(1234567890),
'4fd7160f392dc54308608cae6587e137c62c2e39', 'right hmac sha1 checksum';

# html_escape
$stream = b("foobar'<baz>");
is $stream->html_escape, "foobar&#39;&lt;baz&gt;",
is $stream->html_escape, 'foobar&#39;&lt;baz&gt;',
'right html escaped result';

# html_escape (nothing to escape)
Expand Down

0 comments on commit cfb79b9

Please sign in to comment.