This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Digest-SHA-5.41
[perl5.git] / ext / Digest / SHA / t / 2-nist-sha-oo.t
1 use Test;
2 use strict;
3 use integer;
4 use File::Basename qw(dirname);
5 use File::Spec;
6 use Digest::SHA;
7
8 BEGIN {
9         if ($ENV{PERL_CORE}) {
10                 chdir 't' if -d 't';
11                 @INC = '../lib';
12         }
13 }
14
15 my(@vec);
16
17 BEGIN {
18         @vec = (
19 "ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0",
20 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
21         );
22
23         plan tests => 6 + scalar(@vec);
24 }
25
26         # attempt to use an invalid algorithm, and check for failure
27
28 my $NSA = "SHA-42";     # No Such Algorithm
29 ok(Digest::SHA->new($NSA), undef);
30
31         # test OO methods using first two SHA-256 vectors from NIST
32
33 my $temp = File::Spec->catfile(dirname($0), "oo.tmp");
34 my $file = File::Spec->canonpath($temp);
35 open(FILE, ">$file");
36 binmode(FILE);
37 print FILE "bcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
38 close(FILE);
39
40 my $ctx = Digest::SHA->new()->reset("SHA-256")->new();
41 $ctx->add_bits("a", 5)->add_bits("001");
42
43 my $rsp = shift(@vec);
44 ok($ctx->clone->add("b", "c")->b64digest, $rsp);
45
46 $rsp = shift(@vec);
47 open(FILE, "<$file");
48 binmode(FILE);
49 ok($ctx->clone->addfile(*FILE)->hexdigest, $rsp);
50 close(FILE);
51
52         # use indirect filehandle
53
54 open(my $fh, "<$file");
55 binmode($fh);
56 ok($ctx->clone->addfile($fh)->hexdigest, $rsp);
57 close($fh);
58
59         # test addfile using file name instead of handle
60
61 ok($ctx->addfile($file, "b")->hexdigest, $rsp);
62
63         # test addfile portable mode
64
65 open(FILE, ">$file");
66 binmode(FILE);
67 print FILE "abc\012" x 2048;            # using UNIX newline
68 close(FILE);
69
70 ok($ctx->new(1)->addfile($file, "p")->hexdigest,
71         "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51");
72
73 open(FILE, ">$file");
74 binmode(FILE);
75 print FILE "abc\015\012" x 2048;        # using DOS/Windows newline
76 close(FILE);
77
78 ok($ctx->new(1)->addfile($file, "p")->hexdigest,
79         "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51");
80
81 open(FILE, ">$file");
82 binmode(FILE);
83 print FILE "abc\015" x 2048;            # using Apple/Mac newline
84 close(FILE);
85
86 ok($ctx->new(1)->addfile($file, "p")->hexdigest,
87         "d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51");
88
89 unlink($file);