Skip to content

Commit ed0202d

Browse files
Merge pull request #37 from Easton97-Jens/codex/update-sha1.h-for-sonarcloud-findings
Use std::string_view and std::array in DigestImpl::digestHelper
2 parents 5238e2b + a4a864c commit ed0202d

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

src/utils/sha1.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#ifndef SRC_UTILS_SHA1_H_
1717
#define SRC_UTILS_SHA1_H_
1818

19+
#include <array>
1920
#include <string>
21+
#include <string_view>
2022

2123
#include "src/utils/string.h"
2224
#include "mbedtls/md.h"
@@ -49,23 +51,23 @@ class DigestImpl {
4951
private:
5052

5153
template<typename ConvertOp>
52-
static auto digestHelper(const std::string &input,
54+
static auto digestHelper(std::string_view input,
5355
ConvertOp convertOp) -> auto {
54-
unsigned char digest[DigestSize] = {};
56+
std::array<unsigned char, DigestSize> digest = {};
5557
const auto *mdInfo = mbedtls_md_info_from_type(DigestType);
5658
if (mdInfo == nullptr) {
5759
return convertOp(std::string_view());
5860
}
5961

60-
const auto ret = mbedtls_md(mdInfo,
61-
reinterpret_cast<const unsigned char *>(input.data()),
62-
input.size(), digest);
63-
if (ret != 0) {
62+
if (const auto ret = mbedtls_md(mdInfo,
63+
reinterpret_cast<const unsigned char *>(input.data()),
64+
input.size(), digest.data()); ret != 0) {
6465
return convertOp(std::string_view());
6566
}
6667

68+
// mbedtls uses unsigned char buffers, while string_view expects char.
6769
return convertOp(std::string_view(
68-
reinterpret_cast<const char *>(digest), DigestSize));
70+
reinterpret_cast<const char *>(digest.data()), DigestSize));
6971
}
7072
};
7173

0 commit comments

Comments
 (0)