-
Notifications
You must be signed in to change notification settings - Fork 128
Add md5 sum function #4801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Add md5 sum function #4801
Changes from 18 commits
46a4c48
d370466
885a7fb
4a6297a
28e8fb6
a0d1150
e48d6c3
07d3810
fd92133
6f8fff7
48561da
a1cb4c0
e60d38b
fa10a9f
f8884ff
e2095a0
5b2487f
8476d37
4a51820
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * The MIT License (MIT) | ||
| * | ||
| * Copyright (c) 2015-2026 Advanced Micro Devices, Inc. All rights reserved. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| #ifndef MIGRAPHX_GUARD_RTGLIB_MD5_HPP | ||
| #define MIGRAPHX_GUARD_RTGLIB_MD5_HPP | ||
|
|
||
| #include <string> | ||
| #include <string_view> | ||
| #include <migraphx/config.hpp> | ||
|
|
||
| namespace migraphx { | ||
| inline namespace MIGRAPHX_INLINE_NS { | ||
|
|
||
| /// Compute the MD5 digest of a string and return it as a lowercase hex string. | ||
| std::string MIGRAPHX_EXPORT md5(const std::string_view& str); | ||
|
|
||
| } // namespace MIGRAPHX_INLINE_NS | ||
| } // namespace migraphx | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,12 +25,17 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #define MIGRAPHX_GUARD_MIGRAPHLIB_STRINGUTILS_HPP | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <algorithm> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <array> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <cstdint> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <numeric> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <string> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <sstream> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <type_traits> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <unordered_map> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <vector> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <migraphx/algorithm.hpp> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <migraphx/as_number.hpp> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #include <migraphx/ranges.hpp> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace migraphx { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inline namespace MIGRAPHX_INLINE_NS { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -236,6 +241,38 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ss.str(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Concatenate the lowercase hex representation of each integer in a range. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Each element emits 2*sizeof(element) characters. When lsb is false the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// most-significant byte is emitted first (standard hex notation); when true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// the least-significant byte is first (matches byte-stream hash digests). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template <class Range> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| inline std::string to_hex_string(const Range& r, bool lsb = false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| constexpr std::array<char, 16> hex_digits = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return std::accumulate(r.begin(), r.end(), std::string{}, [&](std::string acc, const auto& x) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using type = std::make_unsigned_t<std::decay_t<decltype(x)>>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const auto u = bit_cast<type>(x); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const auto to_byte = [&](std::ptrdiff_t b) -> std::uint8_t { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+244
to
+256
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return (u >> (b * 8u)) & 0xffu; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Check warning on line 257 in src/include/migraphx/stringutils.hpp
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const auto append_hex = [&](std::string s, std::uint8_t byte) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s.push_back(hex_digits[byte >> 4u]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s.push_back(hex_digits[byte & 0x0fu]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return s; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const auto bytes = range(sizeof(type)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(lsb) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return transform_accumulate( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| bytes.begin(), bytes.end(), std::move(acc), append_hex, to_byte); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const auto rbytes = reverse(bytes); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return transform_accumulate( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rbytes.begin(), rbytes.end(), std::move(acc), append_hex, to_byte); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+253
to
+273
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return std::accumulate(r.begin(), r.end(), std::string{}, [&](std::string acc, const auto& x) { | |
| using T = std::make_unsigned_t<std::decay_t<decltype(x)>>; | |
| const auto u = static_cast<T>(x); | |
| const auto to_byte = [&](std::ptrdiff_t b) -> std::uint8_t { | |
| return (u >> (b * 8u)) & 0xffu; | |
| }; | |
| const auto append_hex = [&](std::string s, std::uint8_t byte) { | |
| s.push_back(hex_digits[byte >> 4u]); | |
| s.push_back(hex_digits[byte & 0x0fu]); | |
| return s; | |
| }; | |
| const auto bytes = range(sizeof(T)); | |
| if(lsb) | |
| { | |
| return transform_accumulate( | |
| bytes.begin(), bytes.end(), std::move(acc), append_hex, to_byte); | |
| } | |
| const auto rbytes = reverse(bytes); | |
| return transform_accumulate( | |
| rbytes.begin(), rbytes.end(), std::move(acc), append_hex, to_byte); | |
| }); | |
| using T = std::make_unsigned_t<std::decay_t<decltype(*r.begin())>>; | |
| std::string result; | |
| result.reserve(r.size() * sizeof(T) * 2); | |
| const auto append_hex = [&](std::uint8_t byte) { | |
| result.push_back(hex_digits[byte >> 4u]); | |
| result.push_back(hex_digits[byte & 0x0fu]); | |
| }; | |
| for(const auto& x : r) | |
| { | |
| const auto u = static_cast<T>(x); | |
| const auto to_byte = [&](std::ptrdiff_t b) -> std::uint8_t { | |
| return (u >> (b * 8u)) & 0xffu; | |
| }; | |
| const auto bytes = range(sizeof(T)); | |
| if(lsb) | |
| { | |
| for(auto b : bytes) | |
| append_hex(to_byte(b)); | |
| } | |
| else | |
| { | |
| for(auto b : reverse(bytes)) | |
| append_hex(to_byte(b)); | |
| } | |
| } | |
| return result; |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,159 @@ | ||||||||||
| /* | ||||||||||
| * The MIT License (MIT) | ||||||||||
| * | ||||||||||
| * Copyright (c) 2015-2026 Advanced Micro Devices, Inc. All rights reserved. | ||||||||||
| * | ||||||||||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||||||
| * of this software and associated documentation files (the "Software"), to deal | ||||||||||
| * in the Software without restriction, including without limitation the rights | ||||||||||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||||||
| * copies of the Software, and to permit persons to whom the Software is | ||||||||||
| * furnished to do so, subject to the following conditions: | ||||||||||
| * | ||||||||||
| * The above copyright notice and this permission notice shall be included in | ||||||||||
| * all copies or substantial portions of the Software. | ||||||||||
| * | ||||||||||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||||||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||||||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||||||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||||||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||||||||||
| * THE SOFTWARE. | ||||||||||
| */ | ||||||||||
| #include <migraphx/md5.hpp> | ||||||||||
| #include <migraphx/bit_cast.hpp> | ||||||||||
| #include <migraphx/stringutils.hpp> | ||||||||||
|
||||||||||
| #include <migraphx/stringutils.hpp> | |
| #include <migraphx/stringutils.hpp> | |
| #include <migraphx/ranges.hpp> | |
| #include <migraphx/algorithm.hpp> |
Copilot
AI
Apr 20, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
process_block takes the 64-byte block by value, which copies the entire block for every compression call. Since this runs once per 64 bytes of input, it can add avoidable overhead for large strings; take block by const& instead.
| std::array<std::uint32_t, 4> process_block(std::array<std::uint32_t, 4> state, | |
| std::array<std::uint8_t, block_size> block) | |
| std::array<std::uint32_t, 4> process_block( | |
| std::array<std::uint32_t, 4> state, const std::array<std::uint8_t, block_size>& block) |
Copilot
AI
Apr 20, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These transforms feed char data into to_uint8(int8_t). On platforms where char is unsigned, bytes >= 0x80 can be altered by the intermediate int8_t conversion, yielding incorrect MD5s for non-ASCII data. Consider using a lambda that casts char -> unsigned char -> std::uint8_t directly.
Copilot
AI
Apr 24, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
transform_partial_sum writes through the output iterator, but last is bound as const auto& so last.end() - 8 is a const_iterator and the assignment in transform_partial_sum won’t compile. Make last a non-const reference (e.g., auto& last = ...) so the length field can be written into tail[0/1].
Copilot
AI
Apr 24, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last is bound as a const reference (const auto& last = ...), but transform_partial_sum writes through the output iterator last.end() - 8. For std::array this yields a const_iterator, so this will not compile (and even if it did, it would be writing into a const object). Make last a non-const reference (or select the destination buffer via pointer/index) so the length bytes can be written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
md5is a public API and MD5 is not suitable for cryptographic security, consider documenting that this function is intended for non-cryptographic checksums (e.g., cache keys) so callers don't misinterpret it as secure hashing.