nss: upgrade to release 3.73
[LibreOffice.git] / comphelper / qa / unit / test_hash.cxx
blob8b6e5a393d23f3b80424d700fe6f0ae974eec410
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>
16 #include <iomanip>
18 #include <cppunit/TestFixture.h>
19 #include <cppunit/extensions/HelperMacros.h>
21 #if USE_TLS_NSS
22 #include <nss.h>
23 #endif
25 class TestHash : public CppUnit::TestFixture
27 public:
28 void testMD5();
29 void testSHA1();
30 void testSHA256();
31 void testSHA512();
32 void testSHA512_NoSaltNoSpin();
33 void testSHA512_saltspin();
35 virtual void tearDown()
37 #if USE_TLS_NSS
38 NSS_Shutdown();
39 #endif
41 CPPUNIT_TEST_SUITE(TestHash);
42 CPPUNIT_TEST(testMD5);
43 CPPUNIT_TEST(testSHA1);
44 CPPUNIT_TEST(testSHA256);
45 CPPUNIT_TEST(testSHA512);
46 CPPUNIT_TEST(testSHA512_NoSaltNoSpin);
47 CPPUNIT_TEST(testSHA512_saltspin);
48 CPPUNIT_TEST_SUITE_END();
51 namespace {
53 std::string tostring(const std::vector<unsigned char>& a)
55 std::stringstream aStrm;
56 for (auto& i:a)
58 aStrm << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(i);
61 return aStrm.str();
66 void TestHash::testMD5()
68 comphelper::Hash aHash(comphelper::HashType::MD5);
69 const char* const pInput = "";
70 aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0);
71 std::vector<unsigned char> calculate_hash = aHash.finalize();
72 CPPUNIT_ASSERT_EQUAL(size_t(16), calculate_hash.size());
73 CPPUNIT_ASSERT_EQUAL(std::string("d41d8cd98f00b204e9800998ecf8427e"), tostring(calculate_hash));
76 void TestHash::testSHA1()
78 comphelper::Hash aHash(comphelper::HashType::SHA1);
79 const char* const pInput = "";
80 aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0);
81 std::vector<unsigned char> calculate_hash = aHash.finalize();
82 CPPUNIT_ASSERT_EQUAL(size_t(20), calculate_hash.size());
83 CPPUNIT_ASSERT_EQUAL(std::string("da39a3ee5e6b4b0d3255bfef95601890afd80709"), tostring(calculate_hash));
86 void TestHash::testSHA256()
88 comphelper::Hash aHash(comphelper::HashType::SHA256);
89 const char* const pInput = "";
90 aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0);
91 std::vector<unsigned char> calculate_hash = aHash.finalize();
92 CPPUNIT_ASSERT_EQUAL(size_t(32), calculate_hash.size());
93 CPPUNIT_ASSERT_EQUAL(std::string("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"), tostring(calculate_hash));
96 void TestHash::testSHA512()
98 comphelper::Hash aHash(comphelper::HashType::SHA512);
99 const char* const pInput = "";
100 aHash.update(reinterpret_cast<const unsigned char*>(pInput), 0);
101 std::vector<unsigned char> calculate_hash = aHash.finalize();
102 CPPUNIT_ASSERT_EQUAL(size_t(64), calculate_hash.size());
103 std::string aStr("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
104 CPPUNIT_ASSERT_EQUAL(aStr, tostring(calculate_hash));
107 // Must be identical to testSHA512()
108 void TestHash::testSHA512_NoSaltNoSpin()
110 const char* const pInput = "";
111 std::vector<unsigned char> calculate_hash =
112 comphelper::Hash::calculateHash( reinterpret_cast<const unsigned char*>(pInput), 0,
113 nullptr, 0, 0, comphelper::Hash::IterCount::NONE, comphelper::HashType::SHA512);
114 CPPUNIT_ASSERT_EQUAL(size_t(64), calculate_hash.size());
115 std::string aStr("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
116 CPPUNIT_ASSERT_EQUAL(aStr, tostring(calculate_hash));
119 // Password, salt, hash and spin count taken from OOXML sheetProtection of
120 // tdf#104250 https://bugs.documentfoundation.org/attachment.cgi?id=129104
121 void TestHash::testSHA512_saltspin()
123 const OUString aHash = comphelper::DocPasswordHelper::GetOoxHashAsBase64( "pwd", "876MLoKTq42+/DLp415iZQ==", 100000,
124 comphelper::Hash::IterCount::APPEND, "SHA-512");
125 CPPUNIT_ASSERT_EQUAL(OUString("5l3mgNHXpWiFaBPv5Yso1Xd/UifWvQWmlDnl/hsCYbFT2sJCzorjRmBCQ/3qeDu6Q/4+GIE8a1DsdaTwYh1q2g=="), aHash);
128 CPPUNIT_TEST_SUITE_REGISTRATION(TestHash);
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */