]> git.saurik.com Git - apt.git/blob - test/hash.cc
add sha512 support in the client now as well
[apt.git] / test / hash.cc
1 #include <apt-pkg/md5.h>
2 #include <apt-pkg/sha1.h>
3 #include <apt-pkg/sha256.h>
4 #include <apt-pkg/sha512.h>
5 #include <apt-pkg/strutl.h>
6 #include <iostream>
7
8 using namespace std;
9
10 template <class T> void Test(const char *In,const char *Out)
11 {
12 T Sum;
13 Sum.Add(In);
14
15 cout << "expected: '" << Out << "'" << endl;
16 cout << "got : '" << Sum.Result().Value() << "'" << endl;
17 cout << "got : '" << Sum.Result().Value() << "'" << endl;
18 cout << "got : '" << Sum.Result().Value() << "'" << endl;
19 if (stringcasecmp(Sum.Result().Value(), Out) != 0) {
20 cout << "FAIL" << endl << endl;
21 abort();
22 } else {
23 cout << "PASS" << endl << endl;
24 }
25 }
26
27 template <class T> void TestMill(const char *Out)
28 {
29 T Sum;
30
31 const unsigned char As[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
32 unsigned Count = 1000000;
33 for (; Count != 0;)
34 {
35 if (Count >= 64)
36 {
37 Sum.Add(As,64);
38 Count -= 64;
39 }
40 else
41 {
42 Sum.Add(As,Count);
43 Count = 0;
44 }
45 }
46
47 if (stringcasecmp(Sum.Result().Value(), Out) != 0)
48 abort();
49 }
50
51 int main()
52 {
53 // From FIPS PUB 180-1
54 Test<SHA1Summation>("abc","A9993E364706816ABA3E25717850C26C9CD0D89D");
55 Test<SHA1Summation>("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
56 "84983E441C3BD26EBAAE4AA1F95129E5E54670F1");
57 TestMill<SHA1Summation>("34AA973CD4C4DAA4F61EEB2BDBAD27316534016F");
58
59 // MD5 tests from RFC 1321
60 Test<MD5Summation>("","d41d8cd98f00b204e9800998ecf8427e");
61 Test<MD5Summation>("a","0cc175b9c0f1b6a831c399e269772661");
62 Test<MD5Summation>("abc","900150983cd24fb0d6963f7d28e17f72");
63 Test<MD5Summation>("message digest","f96b697d7cb7938d525a2f31aaf161d0");
64 Test<MD5Summation>("abcdefghijklmnopqrstuvwxyz","c3fcd3d76192e4007dfb496cca67e13b");
65 Test<MD5Summation>("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
66 "d174ab98d277d9f5a5611c2c9f419d9f");
67 Test<MD5Summation>("12345678901234567890123456789012345678901234567890123456789012345678901234567890",
68 "57edf4a22be3c955ac49da2e2107b67a");
69
70 // SHA-256, From FIPS 180-2
71 Test<SHA256Summation>("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
72 "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
73
74 // SHA-512, From
75 Test<SHA512Summation>(
76 "abc",
77 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
78 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
79
80 return 0;
81 }
82
83