aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/include
diff options
context:
space:
mode:
authorNicole Mazzuca <mazzucan@outlook.com>2019-08-08 16:14:59 -0700
committernicole mazzuca <mazzucan@outlook.com>2019-08-26 12:35:22 -0700
commit782723959399a1a0725ac4921b1b7a7c9d10baf7 (patch)
tree4e611c1b585180f438c692b3bfa367e89bf4a9b6 /toolsrc/include
parente417ff69b746f7842b3b9d0fceacb080498e1c5d (diff)
downloadvcpkg-782723959399a1a0725ac4921b1b7a7c9d10baf7.tar.gz
vcpkg-782723959399a1a0725ac4921b1b7a7c9d10baf7.zip
(#7757) [vcpkg] Switch to internal hash algorithms 📜
On non-Windows platforms, there is no standard way to get the hash of an item -- before this PR, what we did was check for the existence of a few common utility names (shasum, sha1, sha256, sha512), and then call that utility on a file we created containing the contents we wish to hash. This PR adds internal hashers for sha1, sha256, and sha512, and standardizes the interface to allow anyone to implement hashers in the future. These hashers are not extremely optimized, so it's likely that in the future we could get more optimized, but for now we just call out to BCryptHasher on Windows, since it's standard and easy to use (and about 2x faster for sha1 and sha256, and 1.5x faster for sha512). However, they are reasonably fast for being unoptimized. I attempted a few minor optimizations, which actually made the code slower! So as of right now, it's implemented as just a basic conversion of the code on Wikipedia to C++. I have tested these on the standard NIST test vectors (and those test vectors are located in vcpkg-test/hash.cpp).
Diffstat (limited to 'toolsrc/include')
-rw-r--r--toolsrc/include/vcpkg/base/hash.h44
-rw-r--r--toolsrc/include/vcpkg/base/strings.h12
-rw-r--r--toolsrc/include/vcpkg/base/stringview.h5
3 files changed, 56 insertions, 5 deletions
diff --git a/toolsrc/include/vcpkg/base/hash.h b/toolsrc/include/vcpkg/base/hash.h
index 9e6f118c0..d62fd3921 100644
--- a/toolsrc/include/vcpkg/base/hash.h
+++ b/toolsrc/include/vcpkg/base/hash.h
@@ -6,6 +6,46 @@
namespace vcpkg::Hash
{
- std::string get_string_hash(const std::string& s, const std::string& hash_type);
- std::string get_file_hash(const Files::Filesystem& fs, const fs::path& path, const std::string& hash_type);
+ enum class Algorithm
+ {
+ Sha1,
+ Sha256,
+ Sha512,
+ };
+
+ const char* to_string(Algorithm algo) noexcept;
+ Optional<Algorithm> algorithm_from_string(StringView sv) noexcept;
+
+ struct Hasher
+ {
+ virtual void add_bytes(const void* start, const void* end) noexcept = 0;
+
+ // one may only call this once before calling `clear()` or the dtor
+ virtual std::string get_hash() noexcept = 0;
+ virtual void clear() noexcept = 0;
+ virtual ~Hasher() = default;
+ };
+
+ std::unique_ptr<Hasher> get_hasher_for(Algorithm algo) noexcept;
+
+ std::string get_bytes_hash(const void* first, const void* last, Algorithm algo) noexcept;
+ std::string get_string_hash(StringView s, Algorithm algo) noexcept;
+ std::string get_file_hash(const Files::Filesystem& fs,
+ const fs::path& path,
+ Algorithm algo,
+ std::error_code& ec) noexcept;
+ inline std::string get_file_hash(LineInfo li,
+ const Files::Filesystem& fs,
+ const fs::path& path,
+ Algorithm algo) noexcept
+ {
+ std::error_code ec;
+ const auto result = get_file_hash(fs, path, algo, ec);
+ if (ec)
+ {
+ Checks::exit_with_message(li, "Failure to read file for hashing: %s", ec.message());
+ }
+
+ return result;
+ }
}
diff --git a/toolsrc/include/vcpkg/base/strings.h b/toolsrc/include/vcpkg/base/strings.h
index 0f25607df..481e686d1 100644
--- a/toolsrc/include/vcpkg/base/strings.h
+++ b/toolsrc/include/vcpkg/base/strings.h
@@ -12,15 +12,25 @@
namespace vcpkg::Strings::details
{
template<class T>
- auto to_printf_arg(const T& t) -> decltype(t.to_string())
+ auto to_string(const T& t) -> decltype(t.to_string())
{
return t.to_string();
}
+ // first looks up to_string on `T` using ADL; then, if that isn't found,
+ // uses the above definition which returns t.to_string()
+ template<class T, class = std::enable_if_t<!std::is_arithmetic<T>::value>>
+ auto to_printf_arg(const T& t) -> decltype(to_string(t))
+ {
+ return to_string(t);
+ }
+
inline const char* to_printf_arg(const std::string& s) { return s.c_str(); }
inline const char* to_printf_arg(const char* s) { return s; }
+ inline const wchar_t* to_printf_arg(const wchar_t* s) { return s; }
+
template<class T, class = std::enable_if_t<std::is_arithmetic<T>::value>>
T to_printf_arg(T s)
{
diff --git a/toolsrc/include/vcpkg/base/stringview.h b/toolsrc/include/vcpkg/base/stringview.h
index fef5bef4e..8503f5f10 100644
--- a/toolsrc/include/vcpkg/base/stringview.h
+++ b/toolsrc/include/vcpkg/base/stringview.h
@@ -40,10 +40,11 @@ namespace vcpkg
std::string to_string() const;
void to_string(std::string& out) const;
- bool operator==(StringView other) const;
-
private:
const char* m_ptr = 0;
size_t m_size = 0;
};
+
+ bool operator==(StringView lhs, StringView rhs) noexcept;
+ bool operator!=(StringView lhs, StringView rhs) noexcept;
}