mirror of
https://github.com/NixOS/nix
synced 2025-07-07 01:51:47 +02:00
HashType: Rename to HashAlgorithm
To be consistent with CLI, nix API and many other references. As part of this, we also converted it to a scoped enum. https://github.com/NixOS/nix/issues/8876
This commit is contained in:
parent
0c2d5f7673
commit
5334c9c792
64 changed files with 450 additions and 450 deletions
|
@ -546,32 +546,32 @@ nlohmann::json Args::toJSON()
|
|||
|
||||
static void hashTypeCompleter(AddCompletions & completions, size_t index, std::string_view prefix)
|
||||
{
|
||||
for (auto & type : hashTypes)
|
||||
for (auto & type : hashAlgorithms)
|
||||
if (hasPrefix(type, prefix))
|
||||
completions.add(type);
|
||||
}
|
||||
|
||||
Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashType * ht)
|
||||
Args::Flag Args::Flag::mkHashTypeFlag(std::string && longName, HashAlgorithm * ha)
|
||||
{
|
||||
return Flag {
|
||||
.longName = std::move(longName),
|
||||
.description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512')",
|
||||
.labels = {"hash-algo"},
|
||||
.handler = {[ht](std::string s) {
|
||||
*ht = parseHashType(s);
|
||||
.handler = {[ha](std::string s) {
|
||||
*ha = parseHashAlgo(s);
|
||||
}},
|
||||
.completer = hashTypeCompleter,
|
||||
};
|
||||
}
|
||||
|
||||
Args::Flag Args::Flag::mkHashTypeOptFlag(std::string && longName, std::optional<HashType> * oht)
|
||||
Args::Flag Args::Flag::mkHashTypeOptFlag(std::string && longName, std::optional<HashAlgorithm> * oha)
|
||||
{
|
||||
return Flag {
|
||||
.longName = std::move(longName),
|
||||
.description = "hash algorithm ('md5', 'sha1', 'sha256', or 'sha512'). Optional as can also be gotten from SRI hash itself.",
|
||||
.labels = {"hash-algo"},
|
||||
.handler = {[oht](std::string s) {
|
||||
*oht = std::optional<HashType> { parseHashType(s) };
|
||||
.handler = {[oha](std::string s) {
|
||||
*oha = std::optional<HashAlgorithm> {parseHashAlgo(s) };
|
||||
}},
|
||||
.completer = hashTypeCompleter,
|
||||
};
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
enum HashType : char;
|
||||
enum struct HashAlgorithm : char;
|
||||
|
||||
class MultiCommand;
|
||||
|
||||
|
@ -175,8 +175,8 @@ protected:
|
|||
|
||||
std::optional<ExperimentalFeature> experimentalFeature;
|
||||
|
||||
static Flag mkHashTypeFlag(std::string && longName, HashType * ht);
|
||||
static Flag mkHashTypeOptFlag(std::string && longName, std::optional<HashType> * oht);
|
||||
static Flag mkHashTypeFlag(std::string && longName, HashAlgorithm * ha);
|
||||
static Flag mkHashTypeOptFlag(std::string && longName, std::optional<HashAlgorithm> * oha);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -106,7 +106,7 @@ void parse(
|
|||
std::string hashs = getString(source, 20);
|
||||
left -= 20;
|
||||
|
||||
Hash hash(htSHA1);
|
||||
Hash hash(HashAlgorithm::SHA1);
|
||||
std::copy(hashs.begin(), hashs.end(), hash.hash);
|
||||
|
||||
hook(name, TreeEntry {
|
||||
|
@ -241,12 +241,12 @@ Mode dump(
|
|||
|
||||
|
||||
TreeEntry dumpHash(
|
||||
HashType ht,
|
||||
SourceAccessor & accessor, const CanonPath & path, PathFilter & filter)
|
||||
HashAlgorithm ha,
|
||||
SourceAccessor & accessor, const CanonPath & path, PathFilter & filter)
|
||||
{
|
||||
std::function<DumpHook> hook;
|
||||
hook = [&](const CanonPath & path) -> TreeEntry {
|
||||
auto hashSink = HashSink(ht);
|
||||
auto hashSink = HashSink(ha);
|
||||
auto mode = dump(accessor, path, hashSink, hook, filter);
|
||||
auto hash = hashSink.finish().first;
|
||||
return {
|
||||
|
|
|
@ -123,9 +123,9 @@ Mode dump(
|
|||
* A smaller wrapper around `dump`.
|
||||
*/
|
||||
TreeEntry dumpHash(
|
||||
HashType ht,
|
||||
SourceAccessor & accessor, const CanonPath & path,
|
||||
PathFilter & filter = defaultPathFilter);
|
||||
HashAlgorithm ha,
|
||||
SourceAccessor & accessor, const CanonPath & path,
|
||||
PathFilter & filter = defaultPathFilter);
|
||||
|
||||
/**
|
||||
* A line from the output of `git ls-remote --symref`.
|
||||
|
|
|
@ -16,23 +16,23 @@
|
|||
|
||||
namespace nix {
|
||||
|
||||
static size_t regularHashSize(HashType type) {
|
||||
static size_t regularHashSize(HashAlgorithm type) {
|
||||
switch (type) {
|
||||
case htMD5: return md5HashSize;
|
||||
case htSHA1: return sha1HashSize;
|
||||
case htSHA256: return sha256HashSize;
|
||||
case htSHA512: return sha512HashSize;
|
||||
case HashAlgorithm::MD5: return md5HashSize;
|
||||
case HashAlgorithm::SHA1: return sha1HashSize;
|
||||
case HashAlgorithm::SHA256: return sha256HashSize;
|
||||
case HashAlgorithm::SHA512: return sha512HashSize;
|
||||
}
|
||||
abort();
|
||||
}
|
||||
|
||||
|
||||
std::set<std::string> hashTypes = { "md5", "sha1", "sha256", "sha512" };
|
||||
std::set<std::string> hashAlgorithms = {"md5", "sha1", "sha256", "sha512" };
|
||||
|
||||
|
||||
Hash::Hash(HashType type) : type(type)
|
||||
Hash::Hash(HashAlgorithm algo) : algo(algo)
|
||||
{
|
||||
hashSize = regularHashSize(type);
|
||||
hashSize = regularHashSize(algo);
|
||||
assert(hashSize <= maxHashSize);
|
||||
memset(hash, 0, maxHashSize);
|
||||
}
|
||||
|
@ -109,16 +109,16 @@ static std::string printHash32(const Hash & hash)
|
|||
|
||||
std::string printHash16or32(const Hash & hash)
|
||||
{
|
||||
assert(hash.type);
|
||||
return hash.to_string(hash.type == htMD5 ? HashFormat::Base16 : HashFormat::Base32, false);
|
||||
assert(static_cast<char>(hash.algo));
|
||||
return hash.to_string(hash.algo == HashAlgorithm::MD5 ? HashFormat::Base16 : HashFormat::Base32, false);
|
||||
}
|
||||
|
||||
|
||||
std::string Hash::to_string(HashFormat hashFormat, bool includeType) const
|
||||
std::string Hash::to_string(HashFormat hashFormat, bool includeAlgo) const
|
||||
{
|
||||
std::string s;
|
||||
if (hashFormat == HashFormat::SRI || includeType) {
|
||||
s += printHashType(type);
|
||||
if (hashFormat == HashFormat::SRI || includeAlgo) {
|
||||
s += printHashAlgo(algo);
|
||||
s += hashFormat == HashFormat::SRI ? '-' : ':';
|
||||
}
|
||||
switch (hashFormat) {
|
||||
|
@ -136,7 +136,7 @@ std::string Hash::to_string(HashFormat hashFormat, bool includeType) const
|
|||
return s;
|
||||
}
|
||||
|
||||
Hash Hash::dummy(htSHA256);
|
||||
Hash Hash::dummy(HashAlgorithm::SHA256);
|
||||
|
||||
Hash Hash::parseSRI(std::string_view original) {
|
||||
auto rest = original;
|
||||
|
@ -145,18 +145,18 @@ Hash Hash::parseSRI(std::string_view original) {
|
|||
auto hashRaw = splitPrefixTo(rest, '-');
|
||||
if (!hashRaw)
|
||||
throw BadHash("hash '%s' is not SRI", original);
|
||||
HashType parsedType = parseHashType(*hashRaw);
|
||||
HashAlgorithm parsedType = parseHashAlgo(*hashRaw);
|
||||
|
||||
return Hash(rest, parsedType, true);
|
||||
}
|
||||
|
||||
// Mutates the string to eliminate the prefixes when found
|
||||
static std::pair<std::optional<HashType>, bool> getParsedTypeAndSRI(std::string_view & rest)
|
||||
static std::pair<std::optional<HashAlgorithm>, bool> getParsedTypeAndSRI(std::string_view & rest)
|
||||
{
|
||||
bool isSRI = false;
|
||||
|
||||
// Parse the hash type before the separator, if there was one.
|
||||
std::optional<HashType> optParsedType;
|
||||
std::optional<HashAlgorithm> optParsedType;
|
||||
{
|
||||
auto hashRaw = splitPrefixTo(rest, ':');
|
||||
|
||||
|
@ -166,7 +166,7 @@ static std::pair<std::optional<HashType>, bool> getParsedTypeAndSRI(std::string_
|
|||
isSRI = true;
|
||||
}
|
||||
if (hashRaw)
|
||||
optParsedType = parseHashType(*hashRaw);
|
||||
optParsedType = parseHashAlgo(*hashRaw);
|
||||
}
|
||||
|
||||
return {optParsedType, isSRI};
|
||||
|
@ -185,29 +185,29 @@ Hash Hash::parseAnyPrefixed(std::string_view original)
|
|||
return Hash(rest, *optParsedType, isSRI);
|
||||
}
|
||||
|
||||
Hash Hash::parseAny(std::string_view original, std::optional<HashType> optType)
|
||||
Hash Hash::parseAny(std::string_view original, std::optional<HashAlgorithm> optAlgo)
|
||||
{
|
||||
auto rest = original;
|
||||
auto [optParsedType, isSRI] = getParsedTypeAndSRI(rest);
|
||||
|
||||
// Either the string or user must provide the type, if they both do they
|
||||
// must agree.
|
||||
if (!optParsedType && !optType)
|
||||
if (!optParsedType && !optAlgo)
|
||||
throw BadHash("hash '%s' does not include a type, nor is the type otherwise known from context", rest);
|
||||
else if (optParsedType && optType && *optParsedType != *optType)
|
||||
throw BadHash("hash '%s' should have type '%s'", original, printHashType(*optType));
|
||||
else if (optParsedType && optAlgo && *optParsedType != *optAlgo)
|
||||
throw BadHash("hash '%s' should have type '%s'", original, printHashAlgo(*optAlgo));
|
||||
|
||||
HashType hashType = optParsedType ? *optParsedType : *optType;
|
||||
return Hash(rest, hashType, isSRI);
|
||||
HashAlgorithm hashAlgo = optParsedType ? *optParsedType : *optAlgo;
|
||||
return Hash(rest, hashAlgo, isSRI);
|
||||
}
|
||||
|
||||
Hash Hash::parseNonSRIUnprefixed(std::string_view s, HashType type)
|
||||
Hash Hash::parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo)
|
||||
{
|
||||
return Hash(s, type, false);
|
||||
return Hash(s, algo, false);
|
||||
}
|
||||
|
||||
Hash::Hash(std::string_view rest, HashType type, bool isSRI)
|
||||
: Hash(type)
|
||||
Hash::Hash(std::string_view rest, HashAlgorithm algo, bool isSRI)
|
||||
: Hash(algo)
|
||||
{
|
||||
if (!isSRI && rest.size() == base16Len()) {
|
||||
|
||||
|
@ -257,19 +257,19 @@ Hash::Hash(std::string_view rest, HashType type, bool isSRI)
|
|||
}
|
||||
|
||||
else
|
||||
throw BadHash("hash '%s' has wrong length for hash type '%s'", rest, printHashType(this->type));
|
||||
throw BadHash("hash '%s' has wrong length for hash algorithm '%s'", rest, printHashAlgo(this->algo));
|
||||
}
|
||||
|
||||
Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashType> ht)
|
||||
Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashAlgorithm> ha)
|
||||
{
|
||||
if (hashStr.empty()) {
|
||||
if (!ht)
|
||||
if (!ha)
|
||||
throw BadHash("empty hash requires explicit hash type");
|
||||
Hash h(*ht);
|
||||
Hash h(*ha);
|
||||
warn("found empty hash, assuming '%s'", h.to_string(HashFormat::SRI, true));
|
||||
return h;
|
||||
} else
|
||||
return Hash::parseAny(hashStr, ht);
|
||||
return Hash::parseAny(hashStr, ha);
|
||||
}
|
||||
|
||||
|
||||
|
@ -282,58 +282,58 @@ union Ctx
|
|||
};
|
||||
|
||||
|
||||
static void start(HashType ht, Ctx & ctx)
|
||||
static void start(HashAlgorithm ha, Ctx & ctx)
|
||||
{
|
||||
if (ht == htMD5) MD5_Init(&ctx.md5);
|
||||
else if (ht == htSHA1) SHA1_Init(&ctx.sha1);
|
||||
else if (ht == htSHA256) SHA256_Init(&ctx.sha256);
|
||||
else if (ht == htSHA512) SHA512_Init(&ctx.sha512);
|
||||
if (ha == HashAlgorithm::MD5) MD5_Init(&ctx.md5);
|
||||
else if (ha == HashAlgorithm::SHA1) SHA1_Init(&ctx.sha1);
|
||||
else if (ha == HashAlgorithm::SHA256) SHA256_Init(&ctx.sha256);
|
||||
else if (ha == HashAlgorithm::SHA512) SHA512_Init(&ctx.sha512);
|
||||
}
|
||||
|
||||
|
||||
static void update(HashType ht, Ctx & ctx,
|
||||
std::string_view data)
|
||||
static void update(HashAlgorithm ha, Ctx & ctx,
|
||||
std::string_view data)
|
||||
{
|
||||
if (ht == htMD5) MD5_Update(&ctx.md5, data.data(), data.size());
|
||||
else if (ht == htSHA1) SHA1_Update(&ctx.sha1, data.data(), data.size());
|
||||
else if (ht == htSHA256) SHA256_Update(&ctx.sha256, data.data(), data.size());
|
||||
else if (ht == htSHA512) SHA512_Update(&ctx.sha512, data.data(), data.size());
|
||||
if (ha == HashAlgorithm::MD5) MD5_Update(&ctx.md5, data.data(), data.size());
|
||||
else if (ha == HashAlgorithm::SHA1) SHA1_Update(&ctx.sha1, data.data(), data.size());
|
||||
else if (ha == HashAlgorithm::SHA256) SHA256_Update(&ctx.sha256, data.data(), data.size());
|
||||
else if (ha == HashAlgorithm::SHA512) SHA512_Update(&ctx.sha512, data.data(), data.size());
|
||||
}
|
||||
|
||||
|
||||
static void finish(HashType ht, Ctx & ctx, unsigned char * hash)
|
||||
static void finish(HashAlgorithm ha, Ctx & ctx, unsigned char * hash)
|
||||
{
|
||||
if (ht == htMD5) MD5_Final(hash, &ctx.md5);
|
||||
else if (ht == htSHA1) SHA1_Final(hash, &ctx.sha1);
|
||||
else if (ht == htSHA256) SHA256_Final(hash, &ctx.sha256);
|
||||
else if (ht == htSHA512) SHA512_Final(hash, &ctx.sha512);
|
||||
if (ha == HashAlgorithm::MD5) MD5_Final(hash, &ctx.md5);
|
||||
else if (ha == HashAlgorithm::SHA1) SHA1_Final(hash, &ctx.sha1);
|
||||
else if (ha == HashAlgorithm::SHA256) SHA256_Final(hash, &ctx.sha256);
|
||||
else if (ha == HashAlgorithm::SHA512) SHA512_Final(hash, &ctx.sha512);
|
||||
}
|
||||
|
||||
|
||||
Hash hashString(HashType ht, std::string_view s)
|
||||
Hash hashString(HashAlgorithm ha, std::string_view s)
|
||||
{
|
||||
Ctx ctx;
|
||||
Hash hash(ht);
|
||||
start(ht, ctx);
|
||||
update(ht, ctx, s);
|
||||
finish(ht, ctx, hash.hash);
|
||||
Hash hash(ha);
|
||||
start(ha, ctx);
|
||||
update(ha, ctx, s);
|
||||
finish(ha, ctx, hash.hash);
|
||||
return hash;
|
||||
}
|
||||
|
||||
|
||||
Hash hashFile(HashType ht, const Path & path)
|
||||
Hash hashFile(HashAlgorithm ha, const Path & path)
|
||||
{
|
||||
HashSink sink(ht);
|
||||
HashSink sink(ha);
|
||||
readFile(path, sink);
|
||||
return sink.finish().first;
|
||||
}
|
||||
|
||||
|
||||
HashSink::HashSink(HashType ht) : ht(ht)
|
||||
HashSink::HashSink(HashAlgorithm ha) : ha(ha)
|
||||
{
|
||||
ctx = new Ctx;
|
||||
bytes = 0;
|
||||
start(ht, *ctx);
|
||||
start(ha, *ctx);
|
||||
}
|
||||
|
||||
HashSink::~HashSink()
|
||||
|
@ -345,14 +345,14 @@ HashSink::~HashSink()
|
|||
void HashSink::writeUnbuffered(std::string_view data)
|
||||
{
|
||||
bytes += data.size();
|
||||
update(ht, *ctx, data);
|
||||
update(ha, *ctx, data);
|
||||
}
|
||||
|
||||
HashResult HashSink::finish()
|
||||
{
|
||||
flush();
|
||||
Hash hash(ht);
|
||||
nix::finish(ht, *ctx, hash.hash);
|
||||
Hash hash(ha);
|
||||
nix::finish(ha, *ctx, hash.hash);
|
||||
return HashResult(hash, bytes);
|
||||
}
|
||||
|
||||
|
@ -360,16 +360,16 @@ HashResult HashSink::currentHash()
|
|||
{
|
||||
flush();
|
||||
Ctx ctx2 = *ctx;
|
||||
Hash hash(ht);
|
||||
nix::finish(ht, ctx2, hash.hash);
|
||||
Hash hash(ha);
|
||||
nix::finish(ha, ctx2, hash.hash);
|
||||
return HashResult(hash, bytes);
|
||||
}
|
||||
|
||||
|
||||
HashResult hashPath(
|
||||
HashType ht, const Path & path, PathFilter & filter)
|
||||
HashAlgorithm ha, const Path & path, PathFilter & filter)
|
||||
{
|
||||
HashSink sink(ht);
|
||||
HashSink sink(ha);
|
||||
dumpPath(path, sink, filter);
|
||||
return sink.finish();
|
||||
}
|
||||
|
@ -377,7 +377,7 @@ HashResult hashPath(
|
|||
|
||||
Hash compressHash(const Hash & hash, unsigned int newSize)
|
||||
{
|
||||
Hash h(hash.type);
|
||||
Hash h(hash.algo);
|
||||
h.hashSize = newSize;
|
||||
for (unsigned int i = 0; i < hash.hashSize; ++i)
|
||||
h.hash[i % newSize] ^= hash.hash[i];
|
||||
|
@ -420,31 +420,31 @@ std::string_view printHashFormat(HashFormat HashFormat)
|
|||
}
|
||||
}
|
||||
|
||||
std::optional<HashType> parseHashTypeOpt(std::string_view s)
|
||||
std::optional<HashAlgorithm> parseHashAlgoOpt(std::string_view s)
|
||||
{
|
||||
if (s == "md5") return htMD5;
|
||||
if (s == "sha1") return htSHA1;
|
||||
if (s == "sha256") return htSHA256;
|
||||
if (s == "sha512") return htSHA512;
|
||||
if (s == "md5") return HashAlgorithm::MD5;
|
||||
if (s == "sha1") return HashAlgorithm::SHA1;
|
||||
if (s == "sha256") return HashAlgorithm::SHA256;
|
||||
if (s == "sha512") return HashAlgorithm::SHA512;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
HashType parseHashType(std::string_view s)
|
||||
HashAlgorithm parseHashAlgo(std::string_view s)
|
||||
{
|
||||
auto opt_h = parseHashTypeOpt(s);
|
||||
auto opt_h = parseHashAlgoOpt(s);
|
||||
if (opt_h)
|
||||
return *opt_h;
|
||||
else
|
||||
throw UsageError("unknown hash algorithm '%1%', expect 'md5', 'sha1', 'sha256', or 'sha512'", s);
|
||||
}
|
||||
|
||||
std::string_view printHashType(HashType ht)
|
||||
std::string_view printHashAlgo(HashAlgorithm ha)
|
||||
{
|
||||
switch (ht) {
|
||||
case htMD5: return "md5";
|
||||
case htSHA1: return "sha1";
|
||||
case htSHA256: return "sha256";
|
||||
case htSHA512: return "sha512";
|
||||
switch (ha) {
|
||||
case HashAlgorithm::MD5: return "md5";
|
||||
case HashAlgorithm::SHA1: return "sha1";
|
||||
case HashAlgorithm::SHA256: return "sha256";
|
||||
case HashAlgorithm::SHA512: return "sha512";
|
||||
default:
|
||||
// illegal hash type enum value internally, as opposed to external input
|
||||
// which should be validated with nice error message.
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace nix {
|
|||
MakeError(BadHash, Error);
|
||||
|
||||
|
||||
enum HashType : char { htMD5 = 42, htSHA1, htSHA256, htSHA512 };
|
||||
enum struct HashAlgorithm : char { MD5 = 42, SHA1, SHA256, SHA512 };
|
||||
|
||||
|
||||
const int md5HashSize = 16;
|
||||
|
@ -20,7 +20,7 @@ const int sha1HashSize = 20;
|
|||
const int sha256HashSize = 32;
|
||||
const int sha512HashSize = 64;
|
||||
|
||||
extern std::set<std::string> hashTypes;
|
||||
extern std::set<std::string> hashAlgorithms;
|
||||
|
||||
extern const std::string base32Chars;
|
||||
|
||||
|
@ -46,12 +46,12 @@ struct Hash
|
|||
size_t hashSize = 0;
|
||||
uint8_t hash[maxHashSize] = {};
|
||||
|
||||
HashType type;
|
||||
HashAlgorithm algo;
|
||||
|
||||
/**
|
||||
* Create a zero-filled hash object.
|
||||
*/
|
||||
explicit Hash(HashType type);
|
||||
explicit Hash(HashAlgorithm algo);
|
||||
|
||||
/**
|
||||
* Parse the hash from a string representation in the format
|
||||
|
@ -60,7 +60,7 @@ struct Hash
|
|||
* is not present, then the hash type must be specified in the
|
||||
* string.
|
||||
*/
|
||||
static Hash parseAny(std::string_view s, std::optional<HashType> type);
|
||||
static Hash parseAny(std::string_view s, std::optional<HashAlgorithm> optAlgo);
|
||||
|
||||
/**
|
||||
* Parse a hash from a string representation like the above, except the
|
||||
|
@ -72,7 +72,7 @@ struct Hash
|
|||
* Parse a plain hash that musst not have any prefix indicating the type.
|
||||
* The type is passed in to disambiguate.
|
||||
*/
|
||||
static Hash parseNonSRIUnprefixed(std::string_view s, HashType type);
|
||||
static Hash parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo);
|
||||
|
||||
static Hash parseSRI(std::string_view original);
|
||||
|
||||
|
@ -81,7 +81,7 @@ private:
|
|||
* The type must be provided, the string view must not include <type>
|
||||
* prefix. `isSRI` helps disambigate the various base-* encodings.
|
||||
*/
|
||||
Hash(std::string_view s, HashType type, bool isSRI);
|
||||
Hash(std::string_view s, HashAlgorithm algo, bool isSRI);
|
||||
|
||||
public:
|
||||
/**
|
||||
|
@ -125,10 +125,10 @@ public:
|
|||
|
||||
/**
|
||||
* Return a string representation of the hash, in base-16, base-32
|
||||
* or base-64. By default, this is prefixed by the hash type
|
||||
* or base-64. By default, this is prefixed by the hash algo
|
||||
* (e.g. "sha256:").
|
||||
*/
|
||||
[[nodiscard]] std::string to_string(HashFormat hashFormat, bool includeType) const;
|
||||
[[nodiscard]] std::string to_string(HashFormat hashFormat, bool includeAlgo) const;
|
||||
|
||||
[[nodiscard]] std::string gitRev() const
|
||||
{
|
||||
|
@ -146,7 +146,7 @@ public:
|
|||
/**
|
||||
* Helper that defaults empty hashes to the 0 hash.
|
||||
*/
|
||||
Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashType> ht);
|
||||
Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashAlgorithm> ha);
|
||||
|
||||
/**
|
||||
* Print a hash in base-16 if it's MD5, or base-32 otherwise.
|
||||
|
@ -156,14 +156,14 @@ std::string printHash16or32(const Hash & hash);
|
|||
/**
|
||||
* Compute the hash of the given string.
|
||||
*/
|
||||
Hash hashString(HashType ht, std::string_view s);
|
||||
Hash hashString(HashAlgorithm ha, std::string_view s);
|
||||
|
||||
/**
|
||||
* Compute the hash of the given file, hashing its contents directly.
|
||||
*
|
||||
* (Metadata, such as the executable permission bit, is ignored.)
|
||||
*/
|
||||
Hash hashFile(HashType ht, const Path & path);
|
||||
Hash hashFile(HashAlgorithm ha, const Path & path);
|
||||
|
||||
/**
|
||||
* Compute the hash of the given path, serializing as a Nix Archive and
|
||||
|
@ -172,8 +172,8 @@ Hash hashFile(HashType ht, const Path & path);
|
|||
* The hash is defined as (essentially) hashString(ht, dumpPath(path)).
|
||||
*/
|
||||
typedef std::pair<Hash, uint64_t> HashResult;
|
||||
HashResult hashPath(HashType ht, const Path & path,
|
||||
PathFilter & filter = defaultPathFilter);
|
||||
HashResult hashPath(HashAlgorithm ha, const Path & path,
|
||||
PathFilter & filter = defaultPathFilter);
|
||||
|
||||
/**
|
||||
* Compress a hash to the specified number of bytes by cyclically
|
||||
|
@ -199,17 +199,17 @@ std::string_view printHashFormat(HashFormat hashFormat);
|
|||
/**
|
||||
* Parse a string representing a hash type.
|
||||
*/
|
||||
HashType parseHashType(std::string_view s);
|
||||
HashAlgorithm parseHashAlgo(std::string_view s);
|
||||
|
||||
/**
|
||||
* Will return nothing on parse error
|
||||
*/
|
||||
std::optional<HashType> parseHashTypeOpt(std::string_view s);
|
||||
std::optional<HashAlgorithm> parseHashAlgoOpt(std::string_view s);
|
||||
|
||||
/**
|
||||
* And the reverse.
|
||||
*/
|
||||
std::string_view printHashType(HashType ht);
|
||||
std::string_view printHashAlgo(HashAlgorithm ha);
|
||||
|
||||
|
||||
union Ctx;
|
||||
|
@ -222,12 +222,12 @@ struct AbstractHashSink : virtual Sink
|
|||
class HashSink : public BufferedSink, public AbstractHashSink
|
||||
{
|
||||
private:
|
||||
HashType ht;
|
||||
HashAlgorithm ha;
|
||||
Ctx * ctx;
|
||||
uint64_t bytes;
|
||||
|
||||
public:
|
||||
HashSink(HashType ht);
|
||||
HashSink(HashAlgorithm ha);
|
||||
HashSink(const HashSink & h);
|
||||
~HashSink();
|
||||
void writeUnbuffered(std::string_view data) override;
|
||||
|
|
|
@ -110,8 +110,8 @@ void RewritingSink::flush()
|
|||
prev.clear();
|
||||
}
|
||||
|
||||
HashModuloSink::HashModuloSink(HashType ht, const std::string & modulus)
|
||||
: hashSink(ht)
|
||||
HashModuloSink::HashModuloSink(HashAlgorithm ha, const std::string & modulus)
|
||||
: hashSink(ha)
|
||||
, rewritingSink(modulus, std::string(modulus.size(), 0), hashSink)
|
||||
{
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ struct HashModuloSink : AbstractHashSink
|
|||
HashSink hashSink;
|
||||
RewritingSink rewritingSink;
|
||||
|
||||
HashModuloSink(HashType ht, const std::string & modulus);
|
||||
HashModuloSink(HashAlgorithm ha, const std::string & modulus);
|
||||
|
||||
void operator () (std::string_view data) override;
|
||||
|
||||
|
|
|
@ -39,11 +39,11 @@ void SourceAccessor::readFile(
|
|||
}
|
||||
|
||||
Hash SourceAccessor::hashPath(
|
||||
const CanonPath & path,
|
||||
PathFilter & filter,
|
||||
HashType ht)
|
||||
const CanonPath & path,
|
||||
PathFilter & filter,
|
||||
HashAlgorithm ha)
|
||||
{
|
||||
HashSink sink(ht);
|
||||
HashSink sink(ha);
|
||||
dumpPath(path, sink, filter);
|
||||
return sink.finish().first;
|
||||
}
|
||||
|
|
|
@ -97,9 +97,9 @@ struct SourceAccessor
|
|||
PathFilter & filter = defaultPathFilter);
|
||||
|
||||
Hash hashPath(
|
||||
const CanonPath & path,
|
||||
PathFilter & filter = defaultPathFilter,
|
||||
HashType ht = htSHA256);
|
||||
const CanonPath & path,
|
||||
PathFilter & filter = defaultPathFilter,
|
||||
HashAlgorithm ha = HashAlgorithm::SHA256);
|
||||
|
||||
/**
|
||||
* Return a corresponding path in the root filesystem, if
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue