forked from lix-project/lix
treewide: Rename hashBase to hashFormat
hashBase is ambiguous, since it's not about the digital bases, but about the format of hashes. Base16, Base32 and Base64 are all character maps for binary encoding. Rename the enum Base to HashFormat. Rename variables of type HashFormat from [hash]Base to hashFormat, including CmdHashBase::hashFormat and CmdToBase::hashFormat.
This commit is contained in:
parent
aff177d860
commit
838c70f621
|
@ -938,7 +938,7 @@ StorePathSet Store::exportReferences(const StorePathSet & storePaths, const Stor
|
|||
|
||||
json Store::pathInfoToJSON(const StorePathSet & storePaths,
|
||||
bool includeImpureInfo, bool showClosureSize,
|
||||
Base hashBase,
|
||||
HashFormat hashFormat,
|
||||
AllowInvalidFlag allowInvalid)
|
||||
{
|
||||
json::array_t jsonList = json::array();
|
||||
|
@ -951,7 +951,7 @@ json Store::pathInfoToJSON(const StorePathSet & storePaths,
|
|||
|
||||
jsonPath["path"] = printStorePath(info->path);
|
||||
jsonPath["valid"] = true;
|
||||
jsonPath["narHash"] = info->narHash.to_string(hashBase, true);
|
||||
jsonPath["narHash"] = info->narHash.to_string(hashFormat, true);
|
||||
jsonPath["narSize"] = info->narSize;
|
||||
|
||||
{
|
||||
|
@ -993,7 +993,7 @@ json Store::pathInfoToJSON(const StorePathSet & storePaths,
|
|||
if (!narInfo->url.empty())
|
||||
jsonPath["url"] = narInfo->url;
|
||||
if (narInfo->fileHash)
|
||||
jsonPath["downloadHash"] = narInfo->fileHash->to_string(hashBase, true);
|
||||
jsonPath["downloadHash"] = narInfo->fileHash->to_string(hashFormat, true);
|
||||
if (narInfo->fileSize)
|
||||
jsonPath["downloadSize"] = narInfo->fileSize;
|
||||
if (showClosureSize)
|
||||
|
|
|
@ -676,7 +676,7 @@ public:
|
|||
*/
|
||||
nlohmann::json pathInfoToJSON(const StorePathSet & storePaths,
|
||||
bool includeImpureInfo, bool showClosureSize,
|
||||
Base hashBase = Base32,
|
||||
HashFormat hashFormat = Base32,
|
||||
AllowInvalidFlag allowInvalid = DisallowInvalid);
|
||||
|
||||
/**
|
||||
|
|
|
@ -115,14 +115,14 @@ std::string printHash16or32(const Hash & hash)
|
|||
}
|
||||
|
||||
|
||||
std::string Hash::to_string(Base base, bool includeType) const
|
||||
std::string Hash::to_string(HashFormat hashFormat, bool includeType) const
|
||||
{
|
||||
std::string s;
|
||||
if (base == SRI || includeType) {
|
||||
if (hashFormat == SRI || includeType) {
|
||||
s += printHashType(type);
|
||||
s += base == SRI ? '-' : ':';
|
||||
s += hashFormat == SRI ? '-' : ':';
|
||||
}
|
||||
switch (base) {
|
||||
switch (hashFormat) {
|
||||
case Base16:
|
||||
s += printHash16(*this);
|
||||
break;
|
||||
|
|
|
@ -23,7 +23,7 @@ extern std::set<std::string> hashTypes;
|
|||
|
||||
extern const std::string base32Chars;
|
||||
|
||||
enum Base : int { Base64, Base32, Base16, SRI };
|
||||
enum HashFormat : int { Base64, Base32, Base16, SRI };
|
||||
|
||||
|
||||
struct Hash
|
||||
|
@ -114,7 +114,7 @@ public:
|
|||
* or base-64. By default, this is prefixed by the hash type
|
||||
* (e.g. "sha256:").
|
||||
*/
|
||||
std::string to_string(Base base, bool includeType) const;
|
||||
std::string to_string(HashFormat hashFormat, bool includeType) const;
|
||||
|
||||
std::string gitRev() const
|
||||
{
|
||||
|
|
|
@ -18,28 +18,28 @@ namespace nix {
|
|||
// values taken from: https://tools.ietf.org/html/rfc1321
|
||||
auto s1 = "";
|
||||
auto hash = hashString(HashType::htMD5, s1);
|
||||
ASSERT_EQ(hash.to_string(Base::Base16, true), "md5:d41d8cd98f00b204e9800998ecf8427e");
|
||||
ASSERT_EQ(hash.to_string(HashFormat::Base16, true), "md5:d41d8cd98f00b204e9800998ecf8427e");
|
||||
}
|
||||
|
||||
TEST(hashString, testKnownMD5Hashes2) {
|
||||
// values taken from: https://tools.ietf.org/html/rfc1321
|
||||
auto s2 = "abc";
|
||||
auto hash = hashString(HashType::htMD5, s2);
|
||||
ASSERT_EQ(hash.to_string(Base::Base16, true), "md5:900150983cd24fb0d6963f7d28e17f72");
|
||||
ASSERT_EQ(hash.to_string(HashFormat::Base16, true), "md5:900150983cd24fb0d6963f7d28e17f72");
|
||||
}
|
||||
|
||||
TEST(hashString, testKnownSHA1Hashes1) {
|
||||
// values taken from: https://tools.ietf.org/html/rfc3174
|
||||
auto s = "abc";
|
||||
auto hash = hashString(HashType::htSHA1, s);
|
||||
ASSERT_EQ(hash.to_string(Base::Base16, true),"sha1:a9993e364706816aba3e25717850c26c9cd0d89d");
|
||||
ASSERT_EQ(hash.to_string(HashFormat::Base16, true),"sha1:a9993e364706816aba3e25717850c26c9cd0d89d");
|
||||
}
|
||||
|
||||
TEST(hashString, testKnownSHA1Hashes2) {
|
||||
// values taken from: https://tools.ietf.org/html/rfc3174
|
||||
auto s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
auto hash = hashString(HashType::htSHA1, s);
|
||||
ASSERT_EQ(hash.to_string(Base::Base16, true),"sha1:84983e441c3bd26ebaae4aa1f95129e5e54670f1");
|
||||
ASSERT_EQ(hash.to_string(HashFormat::Base16, true),"sha1:84983e441c3bd26ebaae4aa1f95129e5e54670f1");
|
||||
}
|
||||
|
||||
TEST(hashString, testKnownSHA256Hashes1) {
|
||||
|
@ -47,7 +47,7 @@ namespace nix {
|
|||
auto s = "abc";
|
||||
|
||||
auto hash = hashString(HashType::htSHA256, s);
|
||||
ASSERT_EQ(hash.to_string(Base::Base16, true),
|
||||
ASSERT_EQ(hash.to_string(HashFormat::Base16, true),
|
||||
"sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
||||
}
|
||||
|
||||
|
@ -55,7 +55,7 @@ namespace nix {
|
|||
// values taken from: https://tools.ietf.org/html/rfc4634
|
||||
auto s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
auto hash = hashString(HashType::htSHA256, s);
|
||||
ASSERT_EQ(hash.to_string(Base::Base16, true),
|
||||
ASSERT_EQ(hash.to_string(HashFormat::Base16, true),
|
||||
"sha256:248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@ namespace nix {
|
|||
// values taken from: https://tools.ietf.org/html/rfc4634
|
||||
auto s = "abc";
|
||||
auto hash = hashString(HashType::htSHA512, s);
|
||||
ASSERT_EQ(hash.to_string(Base::Base16, true),
|
||||
ASSERT_EQ(hash.to_string(HashFormat::Base16, true),
|
||||
"sha512:ddaf35a193617abacc417349ae20413112e6fa4e89a9"
|
||||
"7ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd"
|
||||
"454d4423643ce80e2a9ac94fa54ca49f");
|
||||
|
@ -74,7 +74,7 @@ namespace nix {
|
|||
auto s = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
|
||||
|
||||
auto hash = hashString(HashType::htSHA512, s);
|
||||
ASSERT_EQ(hash.to_string(Base::Base16, true),
|
||||
ASSERT_EQ(hash.to_string(HashFormat::Base16, true),
|
||||
"sha512:8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa1"
|
||||
"7299aeadb6889018501d289e4900f7e4331b99dec4b5433a"
|
||||
"c7d329eeb6dd26545e96e55b874be909");
|
||||
|
|
|
@ -11,7 +11,7 @@ using namespace nix;
|
|||
struct CmdHashBase : Command
|
||||
{
|
||||
FileIngestionMethod mode;
|
||||
Base base = SRI;
|
||||
HashFormat hashFormat = SRI;
|
||||
bool truncate = false;
|
||||
HashType ht = htSHA256;
|
||||
std::vector<std::string> paths;
|
||||
|
@ -22,25 +22,25 @@ struct CmdHashBase : Command
|
|||
addFlag({
|
||||
.longName = "sri",
|
||||
.description = "Print the hash in SRI format.",
|
||||
.handler = {&base, SRI},
|
||||
.handler = {&hashFormat, SRI},
|
||||
});
|
||||
|
||||
addFlag({
|
||||
.longName = "base64",
|
||||
.description = "Print the hash in base-64 format.",
|
||||
.handler = {&base, Base64},
|
||||
.handler = {&hashFormat, Base64},
|
||||
});
|
||||
|
||||
addFlag({
|
||||
.longName = "base32",
|
||||
.description = "Print the hash in base-32 (Nix-specific) format.",
|
||||
.handler = {&base, Base32},
|
||||
.handler = {&hashFormat, Base32},
|
||||
});
|
||||
|
||||
addFlag({
|
||||
.longName = "base16",
|
||||
.description = "Print the hash in base-16 format.",
|
||||
.handler = {&base, Base16},
|
||||
.handler = {&hashFormat, Base16},
|
||||
});
|
||||
|
||||
addFlag(Flag::mkHashTypeFlag("type", &ht));
|
||||
|
@ -94,18 +94,18 @@ struct CmdHashBase : Command
|
|||
|
||||
Hash h = hashSink->finish().first;
|
||||
if (truncate && h.hashSize > 20) h = compressHash(h, 20);
|
||||
logger->cout(h.to_string(base, base == SRI));
|
||||
logger->cout(h.to_string(hashFormat, hashFormat == SRI));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct CmdToBase : Command
|
||||
{
|
||||
Base base;
|
||||
HashFormat hashFormat;
|
||||
std::optional<HashType> ht;
|
||||
std::vector<std::string> args;
|
||||
|
||||
CmdToBase(Base base) : base(base)
|
||||
CmdToBase(HashFormat hashFormat) : hashFormat(hashFormat)
|
||||
{
|
||||
addFlag(Flag::mkHashTypeOptFlag("type", &ht));
|
||||
expectArgs("strings", &args);
|
||||
|
@ -114,16 +114,16 @@ struct CmdToBase : Command
|
|||
std::string description() override
|
||||
{
|
||||
return fmt("convert a hash to %s representation",
|
||||
base == Base16 ? "base-16" :
|
||||
base == Base32 ? "base-32" :
|
||||
base == Base64 ? "base-64" :
|
||||
hashFormat == Base16 ? "base-16" :
|
||||
hashFormat == Base32 ? "base-32" :
|
||||
hashFormat == Base64 ? "base-64" :
|
||||
"SRI");
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
for (auto s : args)
|
||||
logger->cout(Hash::parseAny(s, ht).to_string(base, base == SRI));
|
||||
logger->cout(Hash::parseAny(s, ht).to_string(hashFormat, hashFormat == SRI));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -162,7 +162,7 @@ static int compatNixHash(int argc, char * * argv)
|
|||
{
|
||||
std::optional<HashType> ht;
|
||||
bool flat = false;
|
||||
Base base = Base16;
|
||||
HashFormat hashFormat = Base16;
|
||||
bool truncate = false;
|
||||
enum { opHash, opTo } op = opHash;
|
||||
std::vector<std::string> ss;
|
||||
|
@ -173,10 +173,10 @@ static int compatNixHash(int argc, char * * argv)
|
|||
else if (*arg == "--version")
|
||||
printVersion("nix-hash");
|
||||
else if (*arg == "--flat") flat = true;
|
||||
else if (*arg == "--base16") base = Base16;
|
||||
else if (*arg == "--base32") base = Base32;
|
||||
else if (*arg == "--base64") base = Base64;
|
||||
else if (*arg == "--sri") base = SRI;
|
||||
else if (*arg == "--base16") hashFormat = Base16;
|
||||
else if (*arg == "--base32") hashFormat = Base32;
|
||||
else if (*arg == "--base64") hashFormat = Base64;
|
||||
else if (*arg == "--sri") hashFormat = SRI;
|
||||
else if (*arg == "--truncate") truncate = true;
|
||||
else if (*arg == "--type") {
|
||||
std::string s = getArg(*arg, arg, end);
|
||||
|
@ -184,19 +184,19 @@ static int compatNixHash(int argc, char * * argv)
|
|||
}
|
||||
else if (*arg == "--to-base16") {
|
||||
op = opTo;
|
||||
base = Base16;
|
||||
hashFormat = Base16;
|
||||
}
|
||||
else if (*arg == "--to-base32") {
|
||||
op = opTo;
|
||||
base = Base32;
|
||||
hashFormat = Base32;
|
||||
}
|
||||
else if (*arg == "--to-base64") {
|
||||
op = opTo;
|
||||
base = Base64;
|
||||
hashFormat = Base64;
|
||||
}
|
||||
else if (*arg == "--to-sri") {
|
||||
op = opTo;
|
||||
base = SRI;
|
||||
hashFormat = SRI;
|
||||
}
|
||||
else if (*arg != "" && arg->at(0) == '-')
|
||||
return false;
|
||||
|
@ -209,14 +209,14 @@ static int compatNixHash(int argc, char * * argv)
|
|||
CmdHashBase cmd(flat ? FileIngestionMethod::Flat : FileIngestionMethod::Recursive);
|
||||
if (!ht.has_value()) ht = htMD5;
|
||||
cmd.ht = ht.value();
|
||||
cmd.base = base;
|
||||
cmd.hashFormat = hashFormat;
|
||||
cmd.truncate = truncate;
|
||||
cmd.paths = ss;
|
||||
cmd.run();
|
||||
}
|
||||
|
||||
else {
|
||||
CmdToBase cmd(base);
|
||||
CmdToBase cmd(hashFormat);
|
||||
cmd.args = ss;
|
||||
if (ht.has_value()) cmd.ht = ht;
|
||||
cmd.run();
|
||||
|
|
Loading…
Reference in a new issue