Skip to content

Commit

Permalink
Added methods and tests to return 16 byte Buf md5_buf()
Browse files Browse the repository at this point in the history
  • Loading branch information
MARTIMM committed Sep 1, 2015
1 parent 14d5d7b commit 5a0cdaa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/Digest/MD5.pm
Expand Up @@ -62,4 +62,12 @@ class Digest::MD5:auth<cosimo>:ver<0.05> {
multi method md5_hex(@str) {
md5( @str.join.encode('ascii') ).list».fmt('%02x').join
}

multi method md5_buf(Str $str --> Buf) {
md5( $str.encode('ascii') );
}

multi method md5_buf(@str --> Buf) {
md5( @str.join.encode('ascii') );
}
}
8 changes: 8 additions & 0 deletions lib/Digest/MD5.pm.perl
Expand Up @@ -61,4 +61,12 @@
multi method md5_hex(@str) {
md5( @str.join.encode('ascii') ).list».fmt('%02x').join
}
multi method md5_buf(Str $str --> Buf) {
md5( $str.encode('ascii') );
}
multi method md5_buf(@str --> Buf) {
md5( @str.join.encode('ascii') );
}
}
25 changes: 24 additions & 1 deletion t/perl5-compat.t
Expand Up @@ -19,12 +19,35 @@ for @cases -> $values, $md5 {
Digest::MD5.md5_hex($values), $md5,
"MD5 hex of '$values' must be '$md5' (static method)"
);

is(
$digest.md5_hex($values), $md5,
"MD5 hex of '$values' must be '$md5' (instance method)"
);


my Buf $md5_buf = Digest::MD5.md5_buf($values);
is $md5_buf.elems, 16, 'Length of buf is 16 bytes';

my Str $s = $md5;
$s ~~ m/(<[0..9A..Za..z]>**2)**16/;
my Buf $b .= new( map( {:16($_.Str)}, @($/[0][*])));
is-deeply(
$md5_buf.list, $b.list,
"MD5 binary of '$values' (static method)"
);


$md5_buf = $digest.md5_buf($values);
is $md5_buf.elems, 16, 'Length of buf is 16 bytes';

$s = $md5;
$s ~~ m/(<[0..9A..Za..z]>**2)**16/;
$b .= new( map( {:16($_.Str)}, @($/[0][*])));
is-deeply(
$md5_buf.list, $b.list,
"MD5 binary of '$values' (instance method)"
);
}

done;
Expand Down

0 comments on commit 5a0cdaa

Please sign in to comment.