From e424f4e041d0d4e4fbc497022bd348fe265424c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blar?= Date: Sun, 23 Jun 2024 19:41:24 +0100 Subject: [PATCH] refactor: change crypto class to namespace (#712) --- LICENSES.md | 2 +- include/faker-cxx/Crypto.h | 50 +++++++++++++---------------- src/modules/crypto/Crypto.cpp | 38 +++++++++++----------- tests/modules/crypto/CryptoTest.cpp | 10 +++--- 4 files changed, 48 insertions(+), 52 deletions(-) diff --git a/LICENSES.md b/LICENSES.md index 4d3925046..692c63f18 100644 --- a/LICENSES.md +++ b/LICENSES.md @@ -1,6 +1,6 @@ # Third Party Licenses -## flexxxxer/md5_hash: +## flexxxxer/Md5Hash: MIT License diff --git a/include/faker-cxx/Crypto.h b/include/faker-cxx/Crypto.h index 6319b4355..45f44f2d8 100644 --- a/include/faker-cxx/Crypto.h +++ b/include/faker-cxx/Crypto.h @@ -3,33 +3,29 @@ #include #include -namespace faker +namespace faker::crypto { -class Crypto -{ -public: - /** - * @brief Returns a random SHA256 hash or hash of provided data. - * - * @returns SHA256 hash string. - * - * @code - * Crypto::sha256("hello world") // "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" - * Crypto::sha256() // Random hash of random - * @endcode - */ - static std::string sha256(std::optional = std::nullopt); +/** + * @brief Returns a random SHA256 hash or hash of provided data. + * + * @returns SHA256 hash string. + * + * @code + * crypto::sha256("hello world") // "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" + * crypto::sha256() // Random hash of random + * @endcode + */ +std::string sha256(std::optional = std::nullopt); - /** - * @brief Returns a random MD5 hash or hash of provided data. - * - * @returns MD5 hash string. - * - * @code - * Crypto::md5("hello world") // "5eb63bbbe01eeed093cb22bb8f5acdc3" - * Crypto::md5() // Random hash of random word - * @endcode - */ - static std::string md5(std::optional = std::nullopt); -}; +/** + * @brief Returns a random MD5 hash or hash of provided data. + * + * @returns MD5 hash string. + * + * @code + * crypto::md5("hello world") // "5eb63bbbe01eeed093cb22bb8f5acdc3" + * crypto::md5() // Random hash of random word + * @endcode + */ +std::string md5(std::optional = std::nullopt); } diff --git a/src/modules/crypto/Crypto.cpp b/src/modules/crypto/Crypto.cpp index 104047ede..28301a5b3 100644 --- a/src/modules/crypto/Crypto.cpp +++ b/src/modules/crypto/Crypto.cpp @@ -8,7 +8,7 @@ #include "faker-cxx/Word.h" -namespace faker +namespace faker::crypto { namespace { @@ -68,7 +68,7 @@ class SHA256 void revert(std::array& hash); }; -class md5_hash +class Md5Hash { public: /** @@ -78,7 +78,7 @@ class md5_hash [[nodiscard]] static std::array compute(const std::string& message); private: - md5_hash() = default; + Md5Hash() = default; static uint32_t rotate_left(uint32_t x, int32_t n); static std::array uint32_to_4_bytes(uint32_t value); @@ -101,7 +101,7 @@ class md5_hash }; } -std::string Crypto::sha256(std::optional data) +std::string sha256(std::optional data) { std::string orgData; if (!data.has_value() || data->empty()) @@ -121,7 +121,7 @@ std::string Crypto::sha256(std::optional data) return result; } -std::string Crypto::md5(std::optional data) +std::string md5(std::optional data) { std::string orgData; if (!data.has_value() || data->empty()) @@ -133,12 +133,12 @@ std::string Crypto::md5(std::optional data) orgData = data.value(); } - return toHex(md5_hash::compute(orgData)); + return toHex(Md5Hash::compute(orgData)); } namespace { -uint32_t md5_hash::uint32_from_4_bytes(std::array bytes) +uint32_t Md5Hash::uint32_from_4_bytes(std::array bytes) { uint32_t value = 0; @@ -150,7 +150,7 @@ uint32_t md5_hash::uint32_from_4_bytes(std::array bytes) return value; } -std::array md5_hash::uint32_to_4_bytes(uint32_t value) +std::array Md5Hash::uint32_to_4_bytes(uint32_t value) { std::array bytes{}; @@ -162,12 +162,12 @@ std::array md5_hash::uint32_to_4_bytes(uint32_t value) return bytes; } -uint32_t md5_hash::rotate_left(const uint32_t x, const int32_t n) +uint32_t Md5Hash::rotate_left(const uint32_t x, const int32_t n) { return (x << n) | (x >> (32 - n)); } -std::array md5_hash::compute(const std::string& message) +std::array Md5Hash::compute(const std::string& message) { // These vars will contain the hash uint32_t a0 = 0x67452301, b0 = 0xefcdab89, c0 = 0x98badcfe, d0 = 0x10325476; @@ -187,13 +187,13 @@ std::array md5_hash::compute(const std::string& message) msg_copy[message.size()] = static_cast(0x80); // std::array bytes = md5_hash::uint32_to_4_bytes(message.size() * 8); - std::array bytes = md5_hash::uint32_to_4_bytes(static_cast(message.size() * 8)); + std::array bytes = Md5Hash::uint32_to_4_bytes(static_cast(message.size() * 8)); for (size_t i = new_len; i < new_len + 4; i++) // msg_copy[i] = bytes[i - new_len]; msg_copy[i] = static_cast(bytes[i - new_len]); // bytes = md5_hash::uint32_to_4_bytes(message.size() >> 29); - bytes = md5_hash::uint32_to_4_bytes(static_cast(message.size() >> 29)); + bytes = Md5Hash::uint32_to_4_bytes(static_cast(message.size() >> 29)); for (size_t i = new_len + 4; i < new_len + 8; i++) msg_copy[i] = static_cast(bytes[i - new_len - 4]); // msg_copy[i] = bytes[i - new_len - 4]; @@ -212,7 +212,7 @@ std::array md5_hash::compute(const std::string& message) static_cast(msg_copy[i + j * 4 + 3]), }; - w[j] = md5_hash::uint32_from_4_bytes(array); + w[j] = Md5Hash::uint32_from_4_bytes(array); } uint32_t a = a0, b = b0, c = c0, d = d0; @@ -250,7 +250,7 @@ std::array md5_hash::compute(const std::string& message) d = c; c = b; // b = b + md5_hash::rotate_left((a + f + md5_hash::k[j] + w[g]), md5_hash::r[j]); - b = b + md5_hash::rotate_left((a + f + md5_hash::k[j] + w[g]), static_cast(md5_hash::r[j])); + b = b + Md5Hash::rotate_left((a + f + Md5Hash::k[j] + w[g]), static_cast(Md5Hash::r[j])); a = temp; } @@ -261,10 +261,10 @@ std::array md5_hash::compute(const std::string& message) d0 += d; } - const std::array a0_arr = md5_hash::uint32_to_4_bytes(a0); - const std::array b0_arr = md5_hash::uint32_to_4_bytes(b0); - const std::array c0_arr = md5_hash::uint32_to_4_bytes(c0); - const std::array d0_arr = md5_hash::uint32_to_4_bytes(d0); + const std::array a0_arr = Md5Hash::uint32_to_4_bytes(a0); + const std::array b0_arr = Md5Hash::uint32_to_4_bytes(b0); + const std::array c0_arr = Md5Hash::uint32_to_4_bytes(c0); + const std::array d0_arr = Md5Hash::uint32_to_4_bytes(d0); // append results bytes const std::array result = { @@ -356,7 +356,7 @@ void SHA256::transform() { // Split data in 32 bit blocks for the 16 first words // m[i] = (m_data[j] << 24) | (m_data[j + 1] << 16) | (m_data[j + 2] << 8) | (m_data[j + 3]); m[i] = (static_cast(m_data[j]) << 24) | (static_cast(m_data[j + 1]) << 16) | - (static_cast(m_data[j + 2]) << 8) | static_cast(m_data[j + 3]); + (static_cast(m_data[j + 2]) << 8) | static_cast(m_data[j + 3]); } for (uint8_t k = 16; k < 64; k++) diff --git a/tests/modules/crypto/CryptoTest.cpp b/tests/modules/crypto/CryptoTest.cpp index 74318bd73..14425ad0c 100644 --- a/tests/modules/crypto/CryptoTest.cpp +++ b/tests/modules/crypto/CryptoTest.cpp @@ -6,7 +6,7 @@ #include "gtest/gtest.h" using namespace ::testing; -using namespace faker; +using namespace faker::crypto; class CryptoTest : public Test { @@ -28,28 +28,28 @@ class CryptoTest : public Test TEST_F(CryptoTest, ShouldGenerateSHA256Hash) { - const auto generatedRandomHash = Crypto::sha256(); + const auto generatedRandomHash = sha256(); ASSERT_EQ(generatedRandomHash.length(), 64); } TEST_F(CryptoTest, ChecksSHA256Hash) { - const auto generatedRandomHash = Crypto::sha256(); + const auto generatedRandomHash = sha256(); ASSERT_TRUE(isSHA256Hash(generatedRandomHash)); } TEST_F(CryptoTest, ShouldGenerateMD5Hash) { - const auto generatedRandomHash = Crypto::md5(); + const auto generatedRandomHash = md5(); ASSERT_EQ(generatedRandomHash.length(), 32); } TEST_F(CryptoTest, ChecksMD5Regex) { - const auto generatedRandomHash = Crypto::md5(); + const auto generatedRandomHash = md5(); ASSERT_TRUE(isMD5Hash(generatedRandomHash)); }