Skip to content

Commit 07a3921

Browse files
committed
Replace use of sprintf by stream op
Fixes build on some OS as sprintf is deprecated
1 parent b7e4b11 commit 07a3921

2 files changed

Lines changed: 23 additions & 20 deletions

File tree

libs/common/src/md5.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#include "md5.hpp"
77
#include <algorithm>
8+
#include <iomanip>
9+
#include <sstream>
810

911
namespace s25util {
1012
namespace {
@@ -52,10 +54,11 @@ void md5::context::reset()
5254

5355
std::string md5::toString() const
5456
{
55-
std::string result(32, 0);
56-
for(int i = 0; i < 16; ++i)
57-
std::sprintf(&result[i * 2], "%02x", digest()[i]);
58-
return result;
57+
std::ostringstream result;
58+
result << std::hex << std::setfill('0');
59+
for(const uint8_t d : msg_digest_)
60+
result << std::setw(2) << unsigned(d);
61+
return result.str();
5962
}
6063

6164
void md5::process(const void* vdata, size_type len, bool add)

tests/testMD5.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,37 @@
88

99
BOOST_AUTO_TEST_CASE(Fixed_Cases)
1010
{
11-
BOOST_TEST_REQUIRE(s25util::md5("").toString() == "d41d8cd98f00b204e9800998ecf8427e");
12-
BOOST_TEST_REQUIRE(s25util::md5("Test").toString() == "0cbc6611f5540bd0809a388dc95a615b");
13-
BOOST_TEST_REQUIRE(s25util::md5("SomeVeryLongInputSomeVeryLongInputSomeVeryLongInputSomeVeryLongInputSomeVeryLongIn"
14-
"putSomeVeryLongInputSomeVeryLongInput")
15-
.toString()
16-
== "4f5586fbeb407ffd0d5e23762167ac67");
17-
BOOST_TEST_REQUIRE(s25util::md5("64CharsOfInput56789012345678901234567890123456789012345678901234").toString()
18-
== "c123841d524187153dd686e0f87bde7d");
19-
BOOST_TEST_REQUIRE(s25util::md5("64CharsOfInput5678901234567890123456789012345678901234567890123464CharsOfInput5678"
20-
"9012345678901234567890123456789012345678901234")
21-
.toString()
22-
== "65a5f6c7d0925bcaad324108046a5644");
11+
BOOST_TEST(s25util::md5("").toString() == "d41d8cd98f00b204e9800998ecf8427e");
12+
BOOST_TEST(s25util::md5("Test").toString() == "0cbc6611f5540bd0809a388dc95a615b");
13+
BOOST_TEST(s25util::md5("SomeVeryLongInputSomeVeryLongInputSomeVeryLongInputSomeVeryLongInputSomeVeryLongIn"
14+
"putSomeVeryLongInputSomeVeryLongInput")
15+
.toString()
16+
== "4f5586fbeb407ffd0d5e23762167ac67");
17+
BOOST_TEST(s25util::md5("64CharsOfInput56789012345678901234567890123456789012345678901234").toString()
18+
== "c123841d524187153dd686e0f87bde7d");
19+
BOOST_TEST(s25util::md5("64CharsOfInput5678901234567890123456789012345678901234567890123464CharsOfInput5678"
20+
"9012345678901234567890123456789012345678901234")
21+
.toString()
22+
== "65a5f6c7d0925bcaad324108046a5644");
2323
}
2424

2525
BOOST_AUTO_TEST_CASE(StreamOp)
2626
{
2727
std::stringstream ss;
2828
ss << s25util::md5("Test");
29-
BOOST_TEST_REQUIRE(ss.str() == "0cbc6611f5540bd0809a388dc95a615b");
29+
BOOST_TEST(ss.str() == "0cbc6611f5540bd0809a388dc95a615b");
3030
}
3131

3232
BOOST_AUTO_TEST_CASE(ProcessAdd)
3333
{
3434
s25util::md5 inst1("");
3535
inst1.process("Test", true);
36-
BOOST_TEST_REQUIRE(inst1.toString() == s25util::md5("Test").toString());
36+
BOOST_TEST(inst1.toString() == s25util::md5("Test").toString());
3737

3838
inst1 = s25util::md5("Str1");
3939
s25util::md5 inst2 = inst1;
4040
inst1.process("Str2", true);
4141
inst2.process("Str2", false);
42-
BOOST_TEST_REQUIRE(inst1.toString() == s25util::md5("Str1Str2").toString());
43-
BOOST_TEST_REQUIRE(inst2.toString() == s25util::md5("Str2").toString());
42+
BOOST_TEST(inst1.toString() == s25util::md5("Str1Str2").toString());
43+
BOOST_TEST(inst2.toString() == s25util::md5("Str2").toString());
4444
}

0 commit comments

Comments
 (0)