|
16 | 16 | #ifndef SRC_UTILS_SHA1_H_ |
17 | 17 | #define SRC_UTILS_SHA1_H_ |
18 | 18 |
|
| 19 | +#include <array> |
19 | 20 | #include <string> |
| 21 | +#include <string_view> |
20 | 22 |
|
21 | 23 | #include "src/utils/string.h" |
22 | 24 | #include "mbedtls/md.h" |
@@ -49,23 +51,23 @@ class DigestImpl { |
49 | 51 | private: |
50 | 52 |
|
51 | 53 | template<typename ConvertOp> |
52 | | - static auto digestHelper(const std::string &input, |
| 54 | + static auto digestHelper(std::string_view input, |
53 | 55 | ConvertOp convertOp) -> auto { |
54 | | - unsigned char digest[DigestSize] = {}; |
| 56 | + std::array<unsigned char, DigestSize> digest = {}; |
55 | 57 | const auto *mdInfo = mbedtls_md_info_from_type(DigestType); |
56 | 58 | if (mdInfo == nullptr) { |
57 | 59 | return convertOp(std::string_view()); |
58 | 60 | } |
59 | 61 |
|
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) { |
64 | 65 | return convertOp(std::string_view()); |
65 | 66 | } |
66 | 67 |
|
| 68 | + // mbedtls uses unsigned char buffers, while string_view expects char. |
67 | 69 | return convertOp(std::string_view( |
68 | | - reinterpret_cast<const char *>(digest), DigestSize)); |
| 70 | + reinterpret_cast<const char *>(digest.data()), DigestSize)); |
69 | 71 | } |
70 | 72 | }; |
71 | 73 |
|
|
0 commit comments