-
Notifications
You must be signed in to change notification settings - Fork 80
/
test_sha1.cpp
129 lines (103 loc) · 3.01 KB
/
test_sha1.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
test_sha1.cpp - test program of
============
SHA-1 in C++
============
100% Public Domain.
Original C Code
-- Steve Reid <[email protected]>
Small changes to fit into bglibs
-- Bruce Guenter <[email protected]>
Translation to simpler C++ Code
-- Volker Grabsch <[email protected]>
*/
#include "sha1.hpp"
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
void compare(const string &result, const string &expected)
{
const string &state = (result == expected) ? "OK" : "Failure";
cout << "Result: " << result << endl;
cout << "Expected: " << expected << " (" << state << ")" << endl;
}
/*
* The 3 test vectors from FIPS PUB 180-1
*/
void test_standard()
{
SHA1 checksum;
cout << endl;
cout << "Test: abc" << endl;
checksum.update("abc");
compare(checksum.final(), "a9993e364706816aba3e25717850c26c9cd0d89d");
cout << endl;
cout << "Test: abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" << endl;
checksum.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq");
compare(checksum.final(), "84983e441c3bd26ebaae4aa1f95129e5e54670f1");
cout << endl;
cout << "Test: A million repetitions of 'a'" << endl;
for (int i = 0; i < 1000000/200; ++i)
{
checksum.update("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
);
}
compare(checksum.final(), "34aa973cd4c4daa4f61eeb2bdbad27316534016f");
}
/*
* Other tests
*/
void test_other()
{
SHA1 checksum;
cout << endl;
cout << "Test: No string" << endl;
compare(checksum.final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709");
cout << endl;
checksum.update("");
cout << "Test: Empty string" << endl;
compare(checksum.final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709");
cout << endl;
cout << "Test: abcde" << endl;
checksum.update("abcde");
compare(checksum.final(), "03de6c570bfe24bfc328ccd7ca46b76eadaf4334");
cout << endl;
cout << "Test: Two concurrent checksum calculations" << endl;
SHA1 checksum1, checksum2;
checksum1.update("abc");
compare(checksum2.final(), "da39a3ee5e6b4b0d3255bfef95601890afd80709"); /* "" */
compare(checksum1.final(), "a9993e364706816aba3e25717850c26c9cd0d89d"); /* "abc" */
}
/*
* immitate "sha1sum -b"
*/
void test_file(const string &filename)
{
cout << SHA1::from_file(filename) << " *" << filename << endl;
}
/*
* main
*/
int main(int argc, const char *argv[])
{
if (argc > 1)
{
for (int i = 1; i < argc; i++)
{
test_file(argv[i]);
}
}
else
{
test_standard();
test_other();
cout << endl;
cout << endl;
}
return 0;
}