annotate Resources/sha1/shatest.cpp @ 523:68451838fb2c dicom-rt

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