Passwordfile library 5.2.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(testComputeHmacSha256);
28 CPPUNIT_TEST(testGenerateRandomNumber);
29 CPPUNIT_TEST(testComputeTOTP);
30 CPPUNIT_TEST_SUITE_END();
31
32public:
33 void setUp() override;
34 void tearDown() override;
35
39 void testComputeTOTP();
40};
41
43
47
51
53{
54 const char someString[] = "hello world";
55 Sha256Sum sum = computeSha256Sum(reinterpret_cast<unsigned const char *>(someString), sizeof(someString));
56 string sumAsHex;
57 sumAsHex.reserve(64);
58 for (unsigned char hashNumber : sum.data) {
59 const string digits = numberToString(hashNumber, static_cast<unsigned char>(16));
60 sumAsHex.push_back(digits.size() < 2 ? '0' : digits.front());
61 sumAsHex.push_back(digits.back());
62 }
63 CPPUNIT_ASSERT_EQUAL("430646847E70344C09F58739E99D5BC96EAC8D5FE7295CF196B986279876BF9B"s, sumAsHex);
64 // note that the termination char is hashed as well
65}
66
68{
69 // RFC 4231 Test Case 2
70 const auto *key = reinterpret_cast<const unsigned char *>("Jefe");
71 const auto keySize = std::size_t(4);
72 const auto *data = reinterpret_cast<const unsigned char *>("what do ya want for nothing?");
73 const auto dataSize = std::size_t(28);
74 const auto hmac = computeHmacSha256(key, keySize, data, dataSize);
75 auto hmacAsHex = std::string();
76 hmacAsHex.reserve(64);
77 for (auto hashNumber : hmac.data) {
78 const auto digits = numberToString(hashNumber, static_cast<unsigned char>(16));
79 hmacAsHex.push_back(digits.size() < 2 ? '0' : digits.front());
80 hmacAsHex.push_back(digits.back());
81 }
82 CPPUNIT_ASSERT_EQUAL("5BDCC146BF60754E6A042426089575C75A003F089D2739839DEC58B964EC3843"s, hmacAsHex);
83}
84
86{
87 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint32_t>(0u), generateRandomNumber(0u, 0u));
88 CPPUNIT_ASSERT_EQUAL(static_cast<std::uint32_t>(1u), generateRandomNumber(1u, 1u));
89 const auto number = generateRandomNumber(5u, 7u);
90 CPPUNIT_ASSERT(number == 5 || number == 6 || number == 7);
91}
92
94{
95 const auto urlDigits6Period30 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=6&issuer=foo%20bar";
96 const auto urlDigits8Period15 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=15&digits=8&issuer=foo%20bar";
97 const auto urlSha256Digits8 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=8&algorithm=SHA256";
98 const auto urlSha512Digits10 = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=10&algorithm=SHA512";
99 const auto urlInvalidSecret = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABC1&period=30&digits=10&algorithm=SHA512";
100 const auto urlInvalidAlgo = "otpauth://totp/foo%20bar?secret=ABCDABCDABCDABCD&period=30&digits=10&algorithm=SHA513";
101
102 const auto time = DateTime::fromDateAndTime(2026, 5, 2, 10, 52, 30);
103 CPPUNIT_ASSERT_EQUAL("757702"s, computeTOTP(urlDigits6Period30, time).digits);
104 CPPUNIT_ASSERT_EQUAL("41448963"s, computeTOTP(urlDigits8Period15, time).digits);
105 CPPUNIT_ASSERT_EQUAL("10222808"s, computeTOTP(urlSha256Digits8, time).digits);
106 CPPUNIT_ASSERT_EQUAL("0340892126"s, computeTOTP(urlSha512Digits10, time).digits);
107 CPPUNIT_ASSERT_THROW(computeTOTP(urlInvalidSecret, time), ConversionException);
108 CPPUNIT_ASSERT_THROW(computeTOTP(urlInvalidAlgo, time), Io::CryptoException);
109}
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:153
PASSWORD_FILE_EXPORT Sha256Sum computeHmacSha256(const unsigned char *key, std::size_t keySize, const unsigned char *data, std::size_t dataSize)
Computes an HMAC-SHA256 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:177
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