Fix typo
[LibreOffice.git] / comphelper / qa / unit / test_hash.cxx
blob93933f84b9a38e06fc5566d931d655b38d9f32a1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <sal/config.h>
11 #include <config_oox.h>
12 #include <comphelper/hash.hxx>
13 #include <comphelper/docpasswordhelper.hxx>
15 #include <rtl/ustring.hxx>
17 #include <cppunit/TestFixture.h>
18 #include <cppunit/extensions/HelperMacros.h>
20 #if USE_TLS_NSS
21 #include <nss.h>
22 #endif
24 class TestHash : public CppUnit::TestFixture
26 public:
27 void testMD5();
28 void testSHA1();
29 void testSHA256();
30 void testSHA512();
31 void testSHA512_NoSaltNoSpin();
32 void testSHA512_saltspin();
34 virtual void tearDown()
36 #if USE_TLS_NSS
37 NSS_Shutdown();
38 #endif
40 CPPUNIT_TEST_SUITE(TestHash);
41 CPPUNIT_TEST(testMD5);
42 CPPUNIT_TEST(testSHA1);
43 CPPUNIT_TEST(testSHA256);
44 CPPUNIT_TEST(testSHA512);
45 CPPUNIT_TEST(testSHA512_NoSaltNoSpin);
46 CPPUNIT_TEST(testSHA512_saltspin);
47 CPPUNIT_TEST_SUITE_END();
50 void TestHash::testMD5()
52 comphelper::Hash aHash(comphelper::HashType::MD5);
53 const char* const pInput = "";
54 aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0);
55 std::vector<unsigned char> calculate_hash = aHash.finalize();
56 CPPUNIT_ASSERT_EQUAL(size_t(16), calculate_hash.size());
57 CPPUNIT_ASSERT_EQUAL(std::string("d41d8cd98f00b204e9800998ecf8427e"), comphelper::hashToString(calculate_hash));
60 void TestHash::testSHA1()
62 comphelper::Hash aHash(comphelper::HashType::SHA1);
63 const char* const pInput = "";
64 aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0);
65 std::vector<unsigned char> calculate_hash = aHash.finalize();
66 CPPUNIT_ASSERT_EQUAL(size_t(20), calculate_hash.size());
67 CPPUNIT_ASSERT_EQUAL(std::string("da39a3ee5e6b4b0d3255bfef95601890afd80709"), comphelper::hashToString(calculate_hash));
70 void TestHash::testSHA256()
72 comphelper::Hash aHash(comphelper::HashType::SHA256);
73 const char* const pInput = "";
74 aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0);
75 std::vector<unsigned char> calculate_hash = aHash.finalize();
76 CPPUNIT_ASSERT_EQUAL(size_t(32), calculate_hash.size());
77 CPPUNIT_ASSERT_EQUAL(std::string("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"), comphelper::hashToString(calculate_hash));
80 void TestHash::testSHA512()
82 comphelper::Hash aHash(comphelper::HashType::SHA512);
83 const char* const pInput = "";
84 aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0);
85 std::vector<unsigned char> calculate_hash = aHash.finalize();
86 CPPUNIT_ASSERT_EQUAL(size_t(64), calculate_hash.size());
87 std::string aStr("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
88 CPPUNIT_ASSERT_EQUAL(aStr, comphelper::hashToString(calculate_hash));
91 // Must be identical to testSHA512()
92 void TestHash::testSHA512_NoSaltNoSpin()
94 const char* const pInput = "";
95 std::vector<unsigned char> calculate_hash =
96 comphelper::Hash::calculateHash( reinterpret_cast<const unsigned char*>(pInput), 0,
97 nullptr, 0, 0, comphelper::Hash::IterCount::NONE, comphelper::HashType::SHA512);
98 CPPUNIT_ASSERT_EQUAL(size_t(64), calculate_hash.size());
99 std::string aStr("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
100 CPPUNIT_ASSERT_EQUAL(aStr, comphelper::hashToString(calculate_hash));
103 // Password, salt, hash and spin count taken from OOXML sheetProtection of
104 // tdf#104250 https://bugs.documentfoundation.org/attachment.cgi?id=129104
105 void TestHash::testSHA512_saltspin()
107 const OUString aHash = comphelper::DocPasswordHelper::GetOoxHashAsBase64( u"pwd"_ustr, u"876MLoKTq42+/DLp415iZQ==", 100000,
108 comphelper::Hash::IterCount::APPEND, u"SHA-512");
109 CPPUNIT_ASSERT_EQUAL(u"5l3mgNHXpWiFaBPv5Yso1Xd/UifWvQWmlDnl/hsCYbFT2sJCzorjRmBCQ/3qeDu6Q/4+GIE8a1DsdaTwYh1q2g=="_ustr, aHash);
112 CPPUNIT_TEST_SUITE_REGISTRATION(TestHash);
114 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */