Skip to content

Commit

Permalink
removed hmac_md5_sum function and method
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 11, 2013
1 parent ec14f4a commit b287b49
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 64 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -15,6 +15,8 @@
- Removed data attribute from Mojo::URL.
- Removed is_chunked, is_dynamic, is_multipart, has_leftovers, leftovers,
write and write_chunk methods from Mojo::Message.
- Removed hmac_md5_sum method from Mojo::ByteStream.
- Removed hmac_md5_sum function from Mojo::Util.
- Removed deprecated end method from Mojo::IOLoop::Delay.
- Removed deprecated build_form_tx, build_json_tx, post_form and post_json
methods from Mojo::UserAgent.
Expand Down
14 changes: 4 additions & 10 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -10,10 +10,10 @@ our @EXPORT_OK = ('b');

# Turn most functions from Mojo::Util into methods
my @UTILS = (
qw(b64_decode b64_encode camelize decamelize hmac_md5_sum hmac_sha1_sum),
qw(html_unescape md5_bytes md5_sum punycode_decode punycode_encode quote),
qw(sha1_bytes sha1_sum slurp spurt squish trim unquote url_escape),
qw(url_unescape xml_escape xor_encode)
qw(b64_decode b64_encode camelize decamelize hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum punycode_decode punycode_encode quote sha1_bytes),
qw(sha1_sum slurp spurt squish trim unquote url_escape url_unescape),
qw(xml_escape xor_encode)
);
for my $name (@UTILS) {
my $sub = Mojo::Util->can($name);
Expand Down Expand Up @@ -161,12 +161,6 @@ Encode bytestream with L<Mojo::Util/"encode">, defaults to C<UTF-8>.
$stream->trim->quote->encode->say;
=head2 hmac_md5_sum
$stream = $stream->hmac_md5_sum('passw0rd');
Generate HMAC-MD5 checksum for bytestream with L<Mojo::Util/"hmac_md5_sum">.
=head2 hmac_sha1_sum
$stream = $stream->hmac_sha1_sum('passw0rd');
Expand Down
36 changes: 13 additions & 23 deletions lib/Mojo/Util.pm
Expand Up @@ -38,11 +38,10 @@ my %CACHE;

our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode deprecated encode get_line hmac_md5_sum hmac_sha1_sum),
qw(html_unescape md5_bytes md5_sum monkey_patch punycode_decode),
qw(punycode_encode quote secure_compare sha1_bytes sha1_sum slurp spurt),
qw(squish steady_time trim unquote url_escape url_unescape xml_escape),
qw(xor_encode)
qw(decode deprecated encode get_line hmac_sha1_sum html_unescape md5_bytes),
qw(md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp spurt squish steady_time trim),
qw(unquote url_escape url_unescape xml_escape xor_encode)
);

sub b64_decode { decode_base64($_[0]) }
Expand Down Expand Up @@ -111,8 +110,15 @@ sub get_line {
return $line;
}
sub hmac_md5_sum { _hmac(\&md5, @_) }
sub hmac_sha1_sum { _hmac(\&sha1, @_) }
sub hmac_sha1_sum {
my ($string, $secret) = @_;
$secret = $secret ? "$secret" : 'Very insecure!';
$secret = sha1 $secret if length $secret > 64;
my $ipad = $secret ^ (chr(0x36) x 64);
my $opad = $secret ^ (chr(0x5c) x 64);
return unpack 'H*', sha1($opad . sha1($ipad . $string));
}
sub html_unescape {
my $string = shift;
Expand Down Expand Up @@ -369,16 +375,6 @@ sub _encoding {
$CACHE{$_[0]} //= find_encoding($_[0]) // croak "Unknown encoding '$_[0]'";
}

sub _hmac {
my ($hash, $string, $secret) = @_;
$secret = $secret ? "$secret" : 'Very insecure!';
$secret = $hash->($secret) if length $secret > 64;

my $ipad = $secret ^ (chr(0x36) x 64);
my $opad = $secret ^ (chr(0x5c) x 64);
return unpack 'H*', $hash->($opad . $hash->($ipad . $string));
}

1;

=head1 NAME
Expand Down Expand Up @@ -491,12 +487,6 @@ Encode characters to bytes.
Extract whole line from string or return C<undef>. Lines are expected to end
with C<0x0d 0x0a> or C<0x0a>.
=head2 hmac_md5_sum
my $checksum = hmac_md5_sum $string, 'passw0rd';
Generate HMAC-MD5 checksum for string.
=head2 hmac_sha1_sum
my $checksum = hmac_sha1_sum $string, 'passw0rd';
Expand Down
4 changes: 0 additions & 4 deletions t/mojo/bytestream.t
Expand Up @@ -74,10 +74,6 @@ is unpack('H*', b('foo bar baz')->sha1_bytes),
is b('foo bar baz')->sha1_sum, 'c7567e8b39e2428e38bf9c9226ac68de4c67dc39',
'right sha1 checksum';

# hmac_md5_sum
is b('Hi there')->hmac_md5_sum(1234567890),
'e3b5fab1b3f5b9d1fe391d09fce7b2ae', 'right hmac md5 checksum';

# hmac_sha1_sum
is b('Hi there')->hmac_sha1_sum(1234567890),
'4fd7160f392dc54308608cae6587e137c62c2e39', 'right hmac sha1 checksum';
Expand Down
31 changes: 4 additions & 27 deletions t/mojo/util.t
Expand Up @@ -10,10 +10,10 @@ use Mojo::DeprecationTest;

use Mojo::Util
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp spurt squish steady_time trim),
qw(unquote url_escape url_unescape xml_escape xor_encode);
qw(decode encode get_line hmac_sha1_sum html_unescape md5_bytes md5_sum),
qw(monkey_patch punycode_decode punycode_encode quote secure_compare),
qw(sha1_bytes sha1_sum slurp spurt squish steady_time trim unquote),
qw(url_escape url_unescape xml_escape xor_encode);

# camelize
is camelize('foo_bar_baz'), 'FooBarBaz', 'right camelized result';
Expand Down Expand Up @@ -293,29 +293,6 @@ is unpack('H*', sha1_bytes 'foo bar baz'),
is sha1_sum('foo bar baz'), 'c7567e8b39e2428e38bf9c9226ac68de4c67dc39',
'right sha1 checksum';

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

# hmac_sha1_sum (RFC 2202)
is hmac_sha1_sum('Hi There', chr(0x0b) x 20),
'b617318655057264e28bc0b6fb378c8ef146be00', 'right hmac sha1 checksum';
Expand Down

0 comments on commit b287b49

Please sign in to comment.