177
|
1 /*
|
|
2 * shatest.cpp
|
|
3 *
|
|
4 * Copyright (C) 1998, 2009
|
|
5 * Paul E. Jones <paulej@packetizer.com>
|
|
6 * All Rights Reserved
|
|
7 *
|
|
8 *****************************************************************************
|
|
9 * $Id: shatest.cpp 12 2009-06-22 19:34:25Z paulej $
|
|
10 *****************************************************************************
|
|
11 *
|
|
12 * Description:
|
|
13 * This file will exercise the SHA1 class and perform the three
|
|
14 * tests documented in FIPS PUB 180-1.
|
|
15 *
|
|
16 * Portability Issues:
|
|
17 * None.
|
|
18 *
|
|
19 */
|
|
20
|
|
21 #include <iostream>
|
|
22 #include "sha1.h"
|
|
23
|
|
24 using namespace std;
|
|
25
|
|
26 /*
|
|
27 * Define patterns for testing
|
|
28 */
|
|
29 #define TESTA "abc"
|
|
30 #define TESTB "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
|
|
31
|
|
32 /*
|
|
33 * Function prototype
|
|
34 */
|
|
35 void DisplayMessageDigest(unsigned *message_digest);
|
|
36
|
|
37 /*
|
|
38 * main
|
|
39 *
|
|
40 * Description:
|
|
41 * This is the entry point for the program
|
|
42 *
|
|
43 * Parameters:
|
|
44 * None.
|
|
45 *
|
|
46 * Returns:
|
|
47 * Nothing.
|
|
48 *
|
|
49 * Comments:
|
|
50 *
|
|
51 */
|
|
52 int main()
|
|
53 {
|
|
54 SHA1 sha;
|
|
55 unsigned message_digest[5];
|
|
56
|
|
57 /*
|
|
58 * Perform test A
|
|
59 */
|
|
60 cout << endl << "Test A: 'abc'" << endl;
|
|
61
|
|
62 sha.Reset();
|
|
63 sha << TESTA;
|
|
64
|
|
65 if (!sha.Result(message_digest))
|
|
66 {
|
|
67 cerr << "ERROR-- could not compute message digest" << endl;
|
|
68 }
|
|
69 else
|
|
70 {
|
|
71 DisplayMessageDigest(message_digest);
|
|
72 cout << "Should match:" << endl;
|
|
73 cout << '\t' << "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D" << endl;
|
|
74 }
|
|
75
|
|
76 /*
|
|
77 * Perform test B
|
|
78 */
|
|
79 cout << endl << "Test B: " << TESTB << endl;
|
|
80
|
|
81 sha.Reset();
|
|
82 sha << TESTB;
|
|
83
|
|
84 if (!sha.Result(message_digest))
|
|
85 {
|
|
86 cerr << "ERROR-- could not compute message digest" << endl;
|
|
87 }
|
|
88 else
|
|
89 {
|
|
90 DisplayMessageDigest(message_digest);
|
|
91 cout << "Should match:" << endl;
|
|
92 cout << '\t' << "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1" << endl;
|
|
93 }
|
|
94
|
|
95 /*
|
|
96 * Perform test C
|
|
97 */
|
|
98 cout << endl << "Test C: One million 'a' characters" << endl;
|
|
99
|
|
100 sha.Reset();
|
|
101 for(int i = 1; i <= 1000000; i++) sha.Input('a');
|
|
102
|
|
103 if (!sha.Result(message_digest))
|
|
104 {
|
|
105 cerr << "ERROR-- could not compute message digest" << endl;
|
|
106 }
|
|
107 else
|
|
108 {
|
|
109 DisplayMessageDigest(message_digest);
|
|
110 cout << "Should match:" << endl;
|
|
111 cout << '\t' << "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F" << endl;
|
|
112 }
|
|
113
|
|
114 return 0;
|
|
115 }
|
|
116
|
|
117 /*
|
|
118 * DisplayMessageDigest
|
|
119 *
|
|
120 * Description:
|
|
121 * Display Message Digest array
|
|
122 *
|
|
123 * Parameters:
|
|
124 * None.
|
|
125 *
|
|
126 * Returns:
|
|
127 * Nothing.
|
|
128 *
|
|
129 * Comments:
|
|
130 *
|
|
131 */
|
|
132 void DisplayMessageDigest(unsigned *message_digest)
|
|
133 {
|
|
134 ios::fmtflags flags;
|
|
135
|
|
136 cout << '\t';
|
|
137
|
|
138 flags = cout.setf(ios::hex|ios::uppercase,ios::basefield);
|
|
139 cout.setf(ios::uppercase);
|
|
140
|
|
141 for(int i = 0; i < 5 ; i++)
|
|
142 {
|
|
143 cout << message_digest[i] << ' ';
|
|
144 }
|
|
145
|
|
146 cout << endl;
|
|
147
|
|
148 cout.setf(flags);
|
|
149 }
|