4#include <openssl/evp.h>
17using EvpMdCtxPtr = std::unique_ptr<EVP_MD_CTX,
decltype(&EVP_MD_CTX_free)>;
22MakeError(BadHash, Error);
25enum class HashType :
char { MD5 = 42, SHA1, SHA256, SHA512 };
28const int md5HashSize = 16;
29const int sha1HashSize = 20;
30const int sha256HashSize = 32;
31const int sha512HashSize = 64;
33extern std::set<std::string> hashTypes;
35extern const std::string base32Chars;
37enum class Base :
int { Base64, Base32, Base16, SRI };
42 constexpr static size_t maxHashSize = 64;
44 uint8_t hash[maxHashSize] = {};
60 static Hash parseAny(std::string_view s, std::optional<HashType> type);
74 static Hash parseSRI(std::string_view original);
81 Hash(std::string_view s, HashType type,
bool isSRI);
112 return (hashSize * 8 - 1) / 5 + 1;
120 return ((4 * hashSize / 3) + 3) & ~3;
128 std::string
to_string(Base base,
bool includeType)
const;
130 std::string gitRev()
const
135 std::string gitShortRev()
const
137 return std::string(
to_string(Base::Base16,
false), 0, 7);
146Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashType> ht);
151std::string printHash16or32(
const Hash & hash);
156Hash hashString(HashType ht, std::string_view s);
161Hash hashFile(HashType ht,
const Path & path);
171 return hashPath(ht, *prepareDump(std::move(path)));
178Hash compressHash(
const Hash & hash,
unsigned int newSize);
183HashType parseHashType(std::string_view s);
188std::optional<HashType> parseHashTypeOpt(std::string_view s);
193std::string_view printHashType(HashType ht);
205 detail::EvpMdCtxPtr ctx;
209 HashSink(HashType ht);
210 HashSink(
const HashSink & h);
212 void writeUnbuffered(std::string_view data)
override;
std::pair< Hash, uint64_t > HashResult
Definition hash.hh:167
static Hash parseAnyPrefixed(std::string_view s)
Definition hash.cc:174
size_t base64Len() const
Definition hash.hh:118
size_t base16Len() const
Definition hash.hh:102
bool operator==(const Hash &h2) const
Definition hash.cc:41
static Hash parseNonSRIUnprefixed(std::string_view s, HashType type)
Definition hash.cc:203
size_t base32Len() const
Definition hash.hh:110
bool operator!=(const Hash &h2) const
Definition hash.cc:50
std::string to_string(Base base, bool includeType) const
Definition hash.cc:116
bool operator<(const Hash &h) const
Definition hash.cc:56
static Hash parseAny(std::string_view s, std::optional< HashType > type)
Definition hash.cc:187
Hash(HashType type)
Definition hash.cc:33
Definition serialise.hh:18
Definition serialise.hh:66
std::string Path
Definition types.hh:28