5#include <c++utilities/chrono/datetime.h>
6#include <c++utilities/conversion/stringconversion.h>
7#include <c++utilities/tests/testutils.h>
9#include <cppunit/TestFixture.h>
10#include <cppunit/extensions/HelperMacros.h>
17using namespace CppUtilities::Literals;
19using namespace CPPUNIT_NS;
29 CPPUNIT_TEST_SUITE_END();
32 void setUp()
override;
52 const char someString[] =
"hello world";
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());
61 CPPUNIT_ASSERT_EQUAL(
"430646847E70344C09F58739E99D5BC96EAC8D5FE7295CF196B986279876BF9B"s, sumAsHex);
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";
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);
The exception that is thrown when an encryption/decryption error occurs.
The OpenSslUtilsTests class tests the functions in the Util::OpenSsl namespace.
void testComputeSha256Sum()
void testGenerateRandomNumber()
Contains functions utilizing the usage of OpenSSL.
PASSWORD_FILE_EXPORT std::uint32_t generateRandomNumber(std::uint32_t min, std::uint32_t max)
Generates a random number using OpenSSL.
PASSWORD_FILE_EXPORT TOTP computeTOTP(std::string_view url, CppUtilities::DateTime time)
Compute a token following the TOTP standard (RFC 6238).
PASSWORD_FILE_EXPORT Sha256Sum computeSha256Sum(const unsigned char *buffer, std::size_t size)
Computes a SHA-256 sum using OpenSSL.
CPPUNIT_TEST_SUITE_REGISTRATION(OpenSslUtilsTests)