Skip to content

Commit

Permalink
Fix string param order on SHA1, add unit test (#4665)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed May 1, 2024
1 parent 1fe18ff commit b766aac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/tests/test_sha1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#define BOOST_TEST_MODULE rsa

#include "../otpch.h"

#include "../tools.h"

#include <boost/test/unit_test.hpp>

using namespace std::string_view_literals;

auto testVectors = std::array{
std::pair{""sv, "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18\x90\xaf\xd8\x07\x09"sv},
std::pair{"The quick brown fox jumps over the lazy dog"sv,
"\x2f\xd4\xe1\xc6\x7a\x2d\x28\xfc\xed\x84\x9e\xe1\xbb\x76\xe7\x39\x1b\x93\xeb\x12"sv},
};

BOOST_AUTO_TEST_CASE(test_sha1)
{
for (auto&& [input, expected] : testVectors) {
std::string result = transformToSHA1(input);
BOOST_TEST(result == expected, "expected '" << expected << "', got '" << result << "'");
}
}
2 changes: 1 addition & 1 deletion src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ std::string transformToSHA1(std::string_view input)
}

unsigned int len = EVP_MD_size(md.get());
std::string digest('\0', static_cast<size_t>(len));
std::string digest(static_cast<size_t>(len), '\0');
if (!EVP_DigestFinal_ex(ctx.get(), reinterpret_cast<unsigned char*>(digest.data()), &len)) {
throw std::runtime_error("Message digest finalization failed");
}
Expand Down

0 comments on commit b766aac

Please sign in to comment.