11MakeError(BadHash, Error);
14enum struct HashAlgorithm :
char { MD5 = 42, SHA1, SHA256, SHA512 };
17const int md5HashSize = 16;
18const int sha1HashSize = 20;
19const int sha256HashSize = 32;
20const int sha512HashSize = 64;
22extern const std::set<std::string> hashAlgorithms;
24extern const std::string nix32Chars;
42extern const std::set<std::string> hashFormats;
46 constexpr static size_t maxHashSize = 64;
48 uint8_t hash[maxHashSize] = {};
55 explicit Hash(HashAlgorithm algo);
64 static Hash parseAny(std::string_view
s, std::optional<HashAlgorithm> optAlgo);
78 static Hash parseSRI(std::string_view original);
85 Hash(std::string_view
s, HashAlgorithm algo,
bool isSRI);
111 return (hashSize * 8 - 1) / 5 + 1;
119 return ((4 * hashSize / 3) + 3) & ~3;
127 [[nodiscard]] std::string to_string(
HashFormat hashFormat,
bool includeAlgo)
const;
129 [[nodiscard]] std::string gitRev()
const
134 [[nodiscard]] std::string gitShortRev()
const
136 return std::string(to_string(HashFormat::Base16,
false), 0, 7);
150Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashAlgorithm> ha);
155std::string printHash16or32(
const Hash & hash);
160Hash hashString(HashAlgorithm ha, std::string_view
s);
167Hash hashFile(HashAlgorithm ha,
const Path & path);
180Hash compressHash(
const Hash & hash,
unsigned int newSize);
185HashFormat parseHashFormat(std::string_view hashFormatName);
190std::optional<HashFormat> parseHashFormatOpt(std::string_view hashFormatName);
195std::string_view printHashFormat(
HashFormat hashFormat);
200HashAlgorithm parseHashAlgo(std::string_view
s);
205std::optional<HashAlgorithm> parseHashAlgoOpt(std::string_view
s);
210std::string_view printHashAlgo(HashAlgorithm ha);
228 HashSink(HashAlgorithm ha);
229 HashSink(
const HashSink & h);
231 void writeUnbuffered(std::string_view
data)
override;
return s
Definition lexer.l:459
std::variant< std::string, std::string_view > data
Definition lexer.l:177
std::pair< Hash, uint64_t > HashResult
Definition hash.hh:174
HashFormat
Enumeration representing the hash formats.
Definition hash.hh:29
@ Base16
Lowercase hexadecimal encoding.
Definition hash.hh:36
@ Base64
Base 64 encoding.
Definition hash.hh:32
@ Nix32
Nix-specific base-32 encoding.
Definition hash.hh:34
@ SRI
"<hash algo>:<Base 64 hash>", format of the SRI integrity attribute.
Definition hash.hh:39
static Hash parseAnyPrefixed(std::string_view s)
Definition hash.cc:172
size_t base64Len() const
Definition hash.hh:117
size_t base16Len() const
Definition hash.hh:101
static Hash random(HashAlgorithm algo)
Definition hash.cc:265
static Hash parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo)
Definition hash.cc:201
std::strong_ordering operator<=>(const Hash &h2) const noexcept
Definition hash.cc:53
size_t base32Len() const
Definition hash.hh:109
bool operator==(const Hash &h2) const noexcept
Definition hash.cc:44
Hash(HashAlgorithm algo)
Definition hash.cc:36
static Hash parseAny(std::string_view s, std::optional< HashAlgorithm > optAlgo)
Definition hash.cc:185
Definition serialise.hh:20
std::string Path
Definition types.hh:22