Nicole Mazzuca 7827239593 (#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).
2019-08-26 12:35:22 -07:00

51 lines
1.8 KiB
C++

#pragma once
#include <vcpkg/base/optional.h>
#include <string>
#include <vector>
namespace vcpkg
{
struct StringView
{
static std::vector<StringView> find_all_enclosed(const StringView& input,
const std::string& left_delim,
const std::string& right_delim);
static StringView find_exactly_one_enclosed(const StringView& input,
const std::string& left_tag,
const std::string& right_tag);
static Optional<StringView> find_at_most_one_enclosed(const StringView& input,
const std::string& left_tag,
const std::string& right_tag);
constexpr StringView() = default;
StringView(const std::string& s); // Implicit by design
template<size_t Sz>
StringView(const char (&arr)[Sz]) : m_ptr(arr), m_size(Sz - 1)
{
}
constexpr StringView(const char* ptr, size_t size) : m_ptr(ptr), m_size(size) {}
constexpr StringView(const char* b, const char* e) : m_ptr(b), m_size(static_cast<size_t>(e - b)) {}
constexpr const char* begin() const { return m_ptr; }
constexpr const char* end() const { return m_ptr + m_size; }
constexpr const char* data() const { return m_ptr; }
constexpr size_t size() const { return m_size; }
std::string to_string() const;
void to_string(std::string& out) 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;
}