summaryrefslogtreecommitdiff
path: root/t/2-nist-sha-oo.t
blob: c1a6b3e52a316449c743794a47cece6a90a52048 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use Test;
use strict;
use integer;
use File::Basename qw(dirname);
use File::Spec;
use Digest::SHA;

BEGIN {
	if ($ENV{PERL_CORE}) {
		chdir 't' if -d 't';
		@INC = '../lib';
	}
}

my(@vec);

BEGIN {
	@vec = (
"ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0",
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"
	);

	plan tests => 5 + scalar(@vec);
}

	# attempt to use an invalid algorithm, and check for failure

my $NSA = "SHA-42";	# No Such Algorithm
ok(Digest::SHA->new($NSA), undef);

	# test OO methods using first two SHA-256 vectors from NIST

my $temp = File::Spec->catfile(dirname($0), "oo.tmp");
my $file = File::Spec->canonpath($temp);
open(FILE, ">$file");
binmode(FILE);
print FILE "bcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
close(FILE);

my $ctx = Digest::SHA->new()->reset("SHA-256")->new();
$ctx->add_bits("a", 5)->add_bits("001");

my $rsp = shift(@vec);
ok($ctx->clone->add("b", "c")->b64digest, $rsp);

$rsp = shift(@vec);
open(FILE, "<$file");
binmode(FILE);
ok($ctx->clone->addfile(*FILE)->hexdigest, $rsp);
close(FILE);

	# test addfile using file name instead of handle

ok($ctx->addfile($file, "b")->hexdigest, $rsp);

	# test addfile portable mode

open(FILE, ">$file");
binmode(FILE);
print FILE "abc\012" x 2048;		# using UNIX newline
close(FILE);

ok($ctx->new(1)->addfile($file, "p")->hexdigest,
	"d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51");

open(FILE, ">$file");
binmode(FILE);
print FILE "abc\015\012" x 2048;	# using DOS/Windows newline
close(FILE);

ok($ctx->new(1)->addfile($file, "p")->hexdigest,
	"d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51");

open(FILE, ">$file");
binmode(FILE);
print FILE "abc\015" x 2048;		# using Apple/Mac newline
close(FILE);

ok($ctx->new(1)->addfile($file, "p")->hexdigest,
	"d449e19c1b0b0c191294c8dc9fa2e4a6ff77fc51");

unlink($file);