Passwordfile library 5.1.0
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
4
5#include <c++utilities/chrono/datetime.h>
6#include <c++utilities/conversion/stringconversion.h>
7#include <c++utilities/tests/testutils.h>
8
9#include <cppunit/TestFixture.h>
10#include <cppunit/extensions/HelperMacros.h>
11
12#include <random>
13
14using namespace std;
15using namespace Util::OpenSsl;
16using namespace CppUtilities;
17using namespace CppUtilities::Literals;
18
19using namespace CPPUNIT_NS;
20
24class OpenSslUtilsTests : public TestFixture {
25 CPPUNIT_TEST_SUITE(OpenSslUtilsTests);
26 CPPUNIT_TEST(testComputeSha256Sum);
27 CPPUNIT_TEST(testGenerateRandomNumber);
28 CPPUNIT_TEST(testComputeTOTP);
29 CPPUNIT_TEST_SUITE_END();
30
31public:
32 void setUp() override;
33 void tearDown() override;
34
37 void testComputeTOTP();
38};
39
41
45
49
51{
52 const char someString[] = "hello world";
53 Sha256Sum sum = computeSha256Sum(reinterpret_cast<unsigned const char *>(someString), sizeof(someString));
54 string sumAsHex;
55 sumAsHex.reserve(64);
56 for (unsigned char hashNumber : sum.data) {
57 const string digits = numberToString(hashNumber, static_cast<unsigned char>(16));
58 sumAsHex.push_back(digits.size() < 2 ? '0' : digits.front());
59 sumAsHex.push_back(digits.back());
60 }
61 CPPUNIT_ASSERT_EQUAL("430646847E70344C09F58739E99D5BC96EAC8D5FE7295CF196B986279876BF9B"s, sumAsHex);
62 // note that the termination char is hashed as well
63}
64
66{
67 CPPUNIT_ASSERT_EQUAL(static_cast<uint32_t>(0u), generateRandomNumber(0u, 0u));
68 CPPUNIT_ASSERT_EQUAL(static_cast<uint32_t>(1u), generateRandomNumber(1u, 1u));
69}
70
72{
73 const auto urlDigits6Period30 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=6&issuer=foo%20bar";
74 const auto urlDigits8Period15 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=15&digits=8&issuer=foo%20bar";
75 const auto urlSha256Digits8 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=8&algorithm=SHA256";
76 const auto urlSha512Digits10 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=10&algorithm=SHA512";
77 const auto urlInvalidSecret = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABC1&period=30&digits=10&algorithm=SHA512";
78 const auto urlInvalidAlgo = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=10&algorithm=SHA513";
79
80 const auto time = DateTime::fromDateAndTime(2026, 5, 2, 10, 52, 30);
81 CPPUNIT_ASSERT_EQUAL("757702"s, computeTOTP(urlDigits6Period30, time).digits);
82 CPPUNIT_ASSERT_EQUAL("41448963"s, computeTOTP(urlDigits8Period15, time).digits);
83 CPPUNIT_ASSERT_EQUAL("10222808"s, computeTOTP(urlSha256Digits8, time).digits);
84 CPPUNIT_ASSERT_EQUAL("0340892126"s, computeTOTP(urlSha512Digits10, time).digits);
85 CPPUNIT_ASSERT_THROW(computeTOTP(urlInvalidSecret, time), ConversionException);
86 CPPUNIT_ASSERT_THROW(computeTOTP(urlInvalidAlgo, time), Io::CryptoException);
87}
The exception that is thrown when an encryption/decryption error occurs.
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:19
PASSWORD_FILE_EXPORT std::uint32_t generateRandomNumber(std::uint32_t min, std::uint32_t max)
Generates a random number using OpenSSL.
Definition openssl.cpp:140
PASSWORD_FILE_EXPORT TOTP computeTOTP(std::string_view url, CppUtilities::DateTime time)
Compute a token following the TOTP standard (RFC 6238).
Definition openssl.cpp:157
PASSWORD_FILE_EXPORT Sha256Sum computeSha256Sum(const unsigned char *buffer, std::size_t size)
Computes a SHA-256 sum using OpenSSL.
Definition openssl.cpp:130
CPPUNIT_TEST_SUITE_REGISTRATION(OpenSslUtilsTests)
unsigned char data[size]
Definition openssl.h:23