Passwordfile library 5.0.12
C++ library to read/write passwords from/to encrypted files
Loading...
Searching...
No Matches
opensslutils.cpp
Go to the documentation of this file.
1#include "../util/openssl.h"
2
3#include <c++utilities/conversion/stringconversion.h>
4#include <c++utilities/tests/testutils.h>
5
6#include <cppunit/TestFixture.h>
7#include <cppunit/extensions/HelperMacros.h>
8
9#include <random>
10
11using namespace std;
12using namespace Util::OpenSsl;
13using namespace CppUtilities;
14using namespace CppUtilities::Literals;
15
16using namespace CPPUNIT_NS;
17
21class OpenSslUtilsTests : public TestFixture {
22 CPPUNIT_TEST_SUITE(OpenSslUtilsTests);
23 CPPUNIT_TEST(testComputeSha256Sum);
24 CPPUNIT_TEST(testGenerateRandomNumber);
25 CPPUNIT_TEST_SUITE_END();
26
27public:
28 void setUp() override;
29 void tearDown() override;
30
33};
34
36
40
44
46{
47 const char someString[] = "hello world";
48 Sha256Sum sum = computeSha256Sum(reinterpret_cast<unsigned const char *>(someString), sizeof(someString));
49 string sumAsHex;
50 sumAsHex.reserve(64);
51 for (unsigned char hashNumber : sum.data) {
52 const string digits = numberToString(hashNumber, static_cast<unsigned char>(16));
53 sumAsHex.push_back(digits.size() < 2 ? '0' : digits.front());
54 sumAsHex.push_back(digits.back());
55 }
56 CPPUNIT_ASSERT_EQUAL("430646847E70344C09F58739E99D5BC96EAC8D5FE7295CF196B986279876BF9B"s, sumAsHex);
57 // note that the termination char is hashed as well
58}
59
61{
62 CPPUNIT_ASSERT_EQUAL(static_cast<uint32_t>(0u), generateRandomNumber(0u, 0u));
63 CPPUNIT_ASSERT_EQUAL(static_cast<uint32_t>(1u), generateRandomNumber(1u, 1u));
64}
The OpenSslUtilsTests class tests the functions in the Util::OpenSsl namespace.
void setUp() override
void testGenerateRandomNumber()
void tearDown() override
Contains functions utilizing the usage of OpenSSL.
Definition openssl.h:11
PASSWORD_FILE_EXPORT std::uint32_t generateRandomNumber(std::uint32_t min, std::uint32_t max)
PASSWORD_FILE_EXPORT Sha256Sum computeSha256Sum(const unsigned char *buffer, std::size_t size)
Computes a SHA-256 sum using OpenSSL.
Definition openssl.cpp:50
CPPUNIT_TEST_SUITE_REGISTRATION(OpenSslUtilsTests)
unsigned char data[size]
Definition openssl.h:15