Permalink
Browse files

Merge pull request #1 from glassflag/master

  • Loading branch information...
2 parents c455e05 + c7a750f commit da07032d5020a2efe4fe1e63e559eeb9025cf707 @soh-cah-toa committed Jan 8, 2012
Showing with 58 additions and 16 deletions.
  1. +41 −15 lib/Digest/SHA256.pm
  2. +17 −1 t/02-args.t
View
@@ -2,7 +2,7 @@
module Digest::SHA256:<soh-cah-toa 0.1>;
-=begin
+=begin Pod
=head1 NAME
@@ -39,8 +39,11 @@ Calculates the SHA256 digest and returns it in hexadecimal form.
=item B<sha256_sum(Str $msg)>, B<sha256_sum(@msg)>
-This function is still in the process of being developed. Do not bother with
-it.
+Returns an Int Array with the resulting SHA256 sum.
+
+=item B<sha256_print(Str $msg)>, B<sha256_sum(@msg)>
+
+Prints the hexadecimal representation of the SHA256 digest og the given string.
=back
@@ -60,23 +63,46 @@ This program is distributed under the terms of the Artistic License 2.0.
For further information, please see LICENSE or visit
<http://www.perlfoundation.org/attachment/legal/artistic-2_0.txt>.
-=end
+=end Pod
=cut
pir::load_bytecode('Digest/sha256.pir');
-my $sum = Q:PIR { %r = get_root_global ['parrot'; 'Digest'], '_sha256sum' };
-my $hex = Q:PIR { %r = get_root_global ['parrot'; 'Digest'], '_sha256_hex' };
-my $print = Q:PIR { %r = get_root_global ['parrot'; 'Digest'], '_sha256_print' };
-
-multi sub sha256_sum(Str $msg) is export { return $sum.($msg) }
-multi sub sha256_sum(@msg) is export { return sha256_sum(@msg.join) }
-
-multi sub sha256_hex(Str $msg) is export { return $hex.($sum.($msg)) }
-multi sub sha256_hex(@msg) is export { return sha256_hex(@msg.join) }
-
- sub sha256_print(@sum) is export { $print.(@sum) }
+my $PD := Q:PIR { %r = new ['Digest';'SHA256'] };
+
+multi sub sha256_sum(Str $msg) is export {
+ my $FIA := $PD.sha_sum($msg);
+ my int $elems = pir::set__IP($FIA);
+ my Int @list;
+ loop (my Int $i = 0; $i < $elems; $i++) {
+ my Int $item := nqp::p6box_i(nqp::atpos($FIA, $i));
+ @list.push($item);
+ }
+ return @list;
+}
+
+multi sub sha256_sum(@msg) is export {
+ return sha256_sum(@msg.join);
+}
+
+multi sub sha256_hex(Str $msg) is export {
+ sha256_sum($msg);
+ return nqp::p6box_s($PD.sha_hex());
+}
+
+multi sub sha256_hex(@msg) is export {
+ return sha256_hex(@msg.join);
+}
+
+multi sub sha256_print($sum) is export {
+ sha256_sum($sum);
+ return nqp::p6box_s($PD.sha_print());
+}
+
+multi sub sha256_print(@sum) is export {
+ return sha256_print(@sum.join);
+}
# vim: ft=perl6
View
@@ -3,7 +3,7 @@ use v6;
use Test;
use Digest::SHA256;
-plan 8;
+plan 19;
my @str = (
'foobar', 'c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2',
@@ -27,6 +27,22 @@ for @list -> $msg, $digest {
is(sha256_hex($msg), $digest, 'List type arguments');
}
+is(sha256_sum("foo"), list(740734059, 1761592975, 4187702588, 489701684, 323104112, 1686355872, 4186594952, 1650911150), 'correct int array created from str');
+is(sha256_sum("bar"), list(4242418478, 3685051380, 140517303, 570334044, 864882926, 1117691983, 2924810678, 2411696057), 'correct in array created from str');
+is(sha256_sum("baz"), list(3131416726, 1295196411, 3234244898, 335827912, 1363059274, 3103589751, 55051433, 1730445462), 'correct in array created from str');
+
+is(sha256_sum(['fish', 'chips']), list(4010022330, 2951128356, 818387141, 1671446482, 1987488175, 3945630504, 4085708939, 3943861802), 'array from list');
+is(sha256_sum(['salt', 'pepper']), list(2467830826, 1409115585, 3621403270, 1431163980, 1688474892, 65164852, 4048750178, 1828287433), 'array from list');
+is(sha256_sum(['cats', 'and', 'dogs', 'and', 'elephants']), list(3251334942, 1552458806, 1598266799, 1645003775, 2335798701, 1518402238, 3373162856, 4203707799), 'array from list');
+
+is(sha256_print("Lorem"), "1b7f8466f087c27f24e1c90017b829cd8208969018a0bbe7d9c452fa224bc6cc", 'sha256_print string value');
+is(sha256_print("Ipsum"), "5816f7ccf1564896a273b031fc0d1b04759ed70d2d02c50b3978b5a0125b0ec5", 'sha256_print string value');
+
+is(sha256_print(['cats', 'dog', 'living', 'together']), "bb8d483e467e477d056707fa890a904e2ebae6ad77ffc8faa961cbac76490e8a", 'sha256_print string value');
+is(sha256_print(['mercury', 'venus', 'earth', 'mars', 'jupiter']), "9556c3203745d3662fbd98191a06da9081afc5ec43e0094d0033d78a43231fe6", 'sha256_print string value');
+
+is(sha256_print(['the', 'answer', '2', 'LIFE', 'the', 'UNIVERSE', '&', 'everything']), sha256_hex('theanswer2LIFEtheUNIVERSE&everything'), 'list result from print matched result from string hex');
+
done;
# vim: ft=perl6

0 comments on commit da07032

Please sign in to comment.