1 | // |
---|---|
2 | // DigestEngineTest.cpp |
3 | // |
4 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "DigestEngineTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Crypto/DigestEngine.h" |
15 | |
16 | |
17 | using Poco::Crypto::DigestEngine; |
18 | |
19 | |
20 | DigestEngineTest::DigestEngineTest(const std::string& name): CppUnit::TestCase(name) |
21 | { |
22 | } |
23 | |
24 | |
25 | DigestEngineTest::~DigestEngineTest() |
26 | { |
27 | } |
28 | |
29 | |
30 | void DigestEngineTest::testMD5() |
31 | { |
32 | DigestEngine engine("MD5"); |
33 | |
34 | // test vectors from RFC 1321 |
35 | |
36 | engine.update(""); |
37 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "d41d8cd98f00b204e9800998ecf8427e"); |
38 | |
39 | engine.update("a"); |
40 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "0cc175b9c0f1b6a831c399e269772661"); |
41 | |
42 | engine.update("abc"); |
43 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "900150983cd24fb0d6963f7d28e17f72"); |
44 | |
45 | engine.update("message digest"); |
46 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "f96b697d7cb7938d525a2f31aaf161d0"); |
47 | |
48 | engine.update("abcdefghijklmnopqrstuvwxyz"); |
49 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "c3fcd3d76192e4007dfb496cca67e13b"); |
50 | |
51 | engine.update("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
52 | engine.update("abcdefghijklmnopqrstuvwxyz0123456789"); |
53 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "d174ab98d277d9f5a5611c2c9f419d9f"); |
54 | |
55 | engine.update("12345678901234567890123456789012345678901234567890123456789012345678901234567890"); |
56 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "57edf4a22be3c955ac49da2e2107b67a"); |
57 | } |
58 | |
59 | void DigestEngineTest::testSHA1() |
60 | { |
61 | DigestEngine engine("SHA1"); |
62 | |
63 | // test vectors from FIPS 180-1 |
64 | |
65 | engine.update("abc"); |
66 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "a9993e364706816aba3e25717850c26c9cd0d89d"); |
67 | |
68 | engine.update("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); |
69 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "84983e441c3bd26ebaae4aa1f95129e5e54670f1"); |
70 | |
71 | for (int i = 0; i < 1000000; ++i) |
72 | engine.update('a'); |
73 | assertTrue (DigestEngine::digestToHex(engine.digest()) == "34aa973cd4c4daa4f61eeb2bdbad27316534016f"); |
74 | } |
75 | |
76 | void DigestEngineTest::setUp() |
77 | { |
78 | } |
79 | |
80 | |
81 | void DigestEngineTest::tearDown() |
82 | { |
83 | } |
84 | |
85 | |
86 | CppUnit::Test* DigestEngineTest::suite() |
87 | { |
88 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DigestEngineTest"); |
89 | |
90 | CppUnit_addTest(pSuite, DigestEngineTest, testMD5); |
91 | CppUnit_addTest(pSuite, DigestEngineTest, testSHA1); |
92 | |
93 | return pSuite; |
94 | } |
95 |