diff --git a/sha1.cpp b/sha1.cpp index f7fd3c4..432aeef 100644 --- a/sha1.cpp +++ b/sha1.cpp @@ -18,7 +18,8 @@ /* Based on: http://www.zedwood.com/article/cpp-sha1-function * Modified by NewYaroslav, 2025-04-15 */ - + +#include #include #include "sha1.hpp" @@ -247,7 +248,7 @@ namespace hmac_hash { char buf[2 * hmac_hash::SHA1::DIGEST_SIZE + 1]; std::fill(buf, buf + (2 * hmac_hash::SHA1::DIGEST_SIZE + 1), '\0'); for(size_t i = 0; i < hmac_hash::SHA1::DIGEST_SIZE; ++i) { - sprintf(buf + i * 2, "%02x", digest[i]); + std::snprintf(buf + i * 2, sizeof(buf) - i * 2, "%02x", digest[i]); } return std::string(buf, (2 * hmac_hash::SHA1::DIGEST_SIZE)); } diff --git a/sha256.cpp b/sha256.cpp index 41bf52b..85e543b 100644 --- a/sha256.cpp +++ b/sha256.cpp @@ -41,6 +41,7 @@ #include #include +#include //#include #include "sha256.hpp" @@ -206,7 +207,7 @@ namespace hmac_hash { char buf[2 * hmac_hash::SHA256::DIGEST_SIZE + 1]; std::fill(buf, buf + (2 * hmac_hash::SHA256::DIGEST_SIZE + 1), '\0'); for(size_t i = 0; i < hmac_hash::SHA256::DIGEST_SIZE; ++i) { - sprintf(buf + i * 2, "%02x", digest[i]); + std::snprintf(buf + i * 2, sizeof(buf) - i * 2, "%02x", digest[i]); } return std::string(buf, (2 * hmac_hash::SHA256::DIGEST_SIZE)); } diff --git a/sha512.cpp b/sha512.cpp index 5aaa687..ba00b78 100644 --- a/sha512.cpp +++ b/sha512.cpp @@ -41,6 +41,7 @@ #include #include +#include #include "sha512.hpp" #define SHA2_SHFR(x, n) (x >> n) @@ -241,7 +242,7 @@ namespace hmac_hash { char buf[2 * hmac_hash::SHA512::DIGEST_SIZE + 1]; std::fill(buf, buf + (2 * hmac_hash::SHA512::DIGEST_SIZE + 1), '\0'); for(size_t i = 0; i < hmac_hash::SHA512::DIGEST_SIZE; ++i){ - sprintf(buf + i * 2, "%02x", digest[i]); + std::snprintf(buf + i * 2, sizeof(buf) - i * 2, "%02x", digest[i]); } return std::string(buf, (2 * hmac_hash::SHA512::DIGEST_SIZE)); }