mirror of
https://github.com/NixOS/nix
synced 2025-07-07 01:51:47 +02:00
Renamed HashFormat::Base32 to HashFormat::Nix32
...and also adjusted parsing accordingly. Also added CLI completion for HashFormats. https://github.com/NixOS/nix/issues/8876
This commit is contained in:
parent
837b889c41
commit
fc6f29053a
30 changed files with 228 additions and 82 deletions
|
@ -544,11 +544,45 @@ nlohmann::json Args::toJSON()
|
|||
return res;
|
||||
}
|
||||
|
||||
static void hashFormatCompleter(AddCompletions & completions, size_t index, std::string_view prefix)
|
||||
{
|
||||
for (auto & format : hashFormats) {
|
||||
if (hasPrefix(format, prefix)) {
|
||||
completions.add(format);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Args::Flag Args::Flag::mkHashFormatFlagWithDefault(std::string &&longName, HashFormat * hf) {
|
||||
assert(*hf == nix::HashFormat::SRI);
|
||||
return Flag{
|
||||
.longName = std::move(longName),
|
||||
.description = "hash format ('base16', 'nix32', 'base64', 'sri'). Default: 'sri'",
|
||||
.labels = {"hash-format"},
|
||||
.handler = {[hf](std::string s) {
|
||||
*hf = parseHashFormat(s);
|
||||
}},
|
||||
.completer = hashFormatCompleter,
|
||||
};
|
||||
}
|
||||
|
||||
Args::Flag Args::Flag::mkHashFormatOptFlag(std::string && longName, std::optional<HashFormat> * ohf) {
|
||||
return Flag{
|
||||
.longName = std::move(longName),
|
||||
.description = "hash format ('base16', 'nix32', 'base64', 'sri').",
|
||||
.labels = {"hash-format"},
|
||||
.handler = {[ohf](std::string s) {
|
||||
*ohf = std::optional<HashFormat>{parseHashFormat(s)};
|
||||
}},
|
||||
.completer = hashFormatCompleter,
|
||||
};
|
||||
}
|
||||
|
||||
static void hashAlgoCompleter(AddCompletions & completions, size_t index, std::string_view prefix)
|
||||
{
|
||||
for (auto & type : hashAlgorithms)
|
||||
if (hasPrefix(type, prefix))
|
||||
completions.add(type);
|
||||
for (auto & algo : hashAlgorithms)
|
||||
if (hasPrefix(algo, prefix))
|
||||
completions.add(algo);
|
||||
}
|
||||
|
||||
Args::Flag Args::Flag::mkHashAlgoFlag(std::string && longName, HashAlgorithm * ha)
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
namespace nix {
|
||||
|
||||
enum struct HashAlgorithm : char;
|
||||
enum struct HashFormat : int;
|
||||
|
||||
class MultiCommand;
|
||||
|
||||
|
@ -177,6 +178,8 @@ protected:
|
|||
|
||||
static Flag mkHashAlgoFlag(std::string && longName, HashAlgorithm * ha);
|
||||
static Flag mkHashAlgoOptFlag(std::string && longName, std::optional<HashAlgorithm> * oha);
|
||||
static Flag mkHashFormatFlagWithDefault(std::string && longName, HashFormat * hf);
|
||||
static Flag mkHashFormatOptFlag(std::string && longName, std::optional<HashFormat> * ohf);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -27,8 +27,9 @@ static size_t regularHashSize(HashAlgorithm type) {
|
|||
}
|
||||
|
||||
|
||||
std::set<std::string> hashAlgorithms = {"md5", "sha1", "sha256", "sha512" };
|
||||
const std::set<std::string> hashAlgorithms = {"md5", "sha1", "sha256", "sha512" };
|
||||
|
||||
const std::set<std::string> hashFormats = {"base64", "nix32", "base16", "sri" };
|
||||
|
||||
Hash::Hash(HashAlgorithm algo) : algo(algo)
|
||||
{
|
||||
|
@ -81,7 +82,7 @@ static std::string printHash16(const Hash & hash)
|
|||
|
||||
|
||||
// omitted: E O U T
|
||||
const std::string base32Chars = "0123456789abcdfghijklmnpqrsvwxyz";
|
||||
const std::string nix32Chars = "0123456789abcdfghijklmnpqrsvwxyz";
|
||||
|
||||
|
||||
static std::string printHash32(const Hash & hash)
|
||||
|
@ -100,7 +101,7 @@ static std::string printHash32(const Hash & hash)
|
|||
unsigned char c =
|
||||
(hash.hash[i] >> j)
|
||||
| (i >= hash.hashSize - 1 ? 0 : hash.hash[i + 1] << (8 - j));
|
||||
s.push_back(base32Chars[c & 0x1f]);
|
||||
s.push_back(nix32Chars[c & 0x1f]);
|
||||
}
|
||||
|
||||
return s;
|
||||
|
@ -110,7 +111,7 @@ static std::string printHash32(const Hash & hash)
|
|||
std::string printHash16or32(const Hash & hash)
|
||||
{
|
||||
assert(static_cast<char>(hash.algo));
|
||||
return hash.to_string(hash.algo == HashAlgorithm::MD5 ? HashFormat::Base16 : HashFormat::Base32, false);
|
||||
return hash.to_string(hash.algo == HashAlgorithm::MD5 ? HashFormat::Base16 : HashFormat::Nix32, false);
|
||||
}
|
||||
|
||||
|
||||
|
@ -125,7 +126,7 @@ std::string Hash::to_string(HashFormat hashFormat, bool includeAlgo) const
|
|||
case HashFormat::Base16:
|
||||
s += printHash16(*this);
|
||||
break;
|
||||
case HashFormat::Base32:
|
||||
case HashFormat::Nix32:
|
||||
s += printHash32(*this);
|
||||
break;
|
||||
case HashFormat::Base64:
|
||||
|
@ -230,8 +231,8 @@ Hash::Hash(std::string_view rest, HashAlgorithm algo, bool isSRI)
|
|||
for (unsigned int n = 0; n < rest.size(); ++n) {
|
||||
char c = rest[rest.size() - n - 1];
|
||||
unsigned char digit;
|
||||
for (digit = 0; digit < base32Chars.size(); ++digit) /* !!! slow */
|
||||
if (base32Chars[digit] == c) break;
|
||||
for (digit = 0; digit < nix32Chars.size(); ++digit) /* !!! slow */
|
||||
if (nix32Chars[digit] == c) break;
|
||||
if (digit >= 32)
|
||||
throw BadHash("invalid base-32 hash '%s'", rest);
|
||||
unsigned int b = n * 5;
|
||||
|
@ -388,7 +389,11 @@ Hash compressHash(const Hash & hash, unsigned int newSize)
|
|||
std::optional<HashFormat> parseHashFormatOpt(std::string_view hashFormatName)
|
||||
{
|
||||
if (hashFormatName == "base16") return HashFormat::Base16;
|
||||
if (hashFormatName == "base32") return HashFormat::Base32;
|
||||
if (hashFormatName == "nix32") return HashFormat::Nix32;
|
||||
if (hashFormatName == "base32") {
|
||||
warn(R"("base32" is a deprecated alias for hash format "nix32".)");
|
||||
return HashFormat::Nix32;
|
||||
}
|
||||
if (hashFormatName == "base64") return HashFormat::Base64;
|
||||
if (hashFormatName == "sri") return HashFormat::SRI;
|
||||
return std::nullopt;
|
||||
|
@ -407,8 +412,8 @@ std::string_view printHashFormat(HashFormat HashFormat)
|
|||
switch (HashFormat) {
|
||||
case HashFormat::Base64:
|
||||
return "base64";
|
||||
case HashFormat::Base32:
|
||||
return "base32";
|
||||
case HashFormat::Nix32:
|
||||
return "nix32";
|
||||
case HashFormat::Base16:
|
||||
return "base16";
|
||||
case HashFormat::SRI:
|
||||
|
|
|
@ -20,9 +20,9 @@ const int sha1HashSize = 20;
|
|||
const int sha256HashSize = 32;
|
||||
const int sha512HashSize = 64;
|
||||
|
||||
extern std::set<std::string> hashAlgorithms;
|
||||
extern const std::set<std::string> hashAlgorithms;
|
||||
|
||||
extern const std::string base32Chars;
|
||||
extern const std::string nix32Chars;
|
||||
|
||||
/**
|
||||
* @brief Enumeration representing the hash formats.
|
||||
|
@ -31,8 +31,8 @@ enum struct HashFormat : int {
|
|||
/// @brief Base 64 encoding.
|
||||
/// @see [IETF RFC 4648, section 4](https://datatracker.ietf.org/doc/html/rfc4648#section-4).
|
||||
Base64,
|
||||
/// @brief Nix-specific base-32 encoding. @see base32Chars
|
||||
Base32,
|
||||
/// @brief Nix-specific base-32 encoding. @see nix32Chars
|
||||
Nix32,
|
||||
/// @brief Lowercase hexadecimal encoding. @see base16Chars
|
||||
Base16,
|
||||
/// @brief "<hash algo>:<Base 64 hash>", format of the SRI integrity attribute.
|
||||
|
@ -40,6 +40,8 @@ enum struct HashFormat : int {
|
|||
SRI
|
||||
};
|
||||
|
||||
extern const std::set<std::string> hashFormats;
|
||||
|
||||
struct Hash
|
||||
{
|
||||
constexpr static size_t maxHashSize = 64;
|
||||
|
|
|
@ -23,8 +23,8 @@ static void search(
|
|||
static bool isBase32[256];
|
||||
std::call_once(initialised, [](){
|
||||
for (unsigned int i = 0; i < 256; ++i) isBase32[i] = false;
|
||||
for (unsigned int i = 0; i < base32Chars.size(); ++i)
|
||||
isBase32[(unsigned char) base32Chars[i]] = true;
|
||||
for (unsigned int i = 0; i < nix32Chars.size(); ++i)
|
||||
isBase32[(unsigned char) nix32Chars[i]] = true;
|
||||
});
|
||||
|
||||
for (size_t i = 0; i + refLength <= s.size(); ) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue