mirror of
https://github.com/NixOS/nix
synced 2025-07-07 01:51:47 +02:00
Merge pull request #12013 from DeterminateSystems/fix-11996
nix hash convert: Support SRI hashes that lack trailing '=' characters
This commit is contained in:
commit
a7cdb55b44
3 changed files with 30 additions and 14 deletions
|
@ -134,7 +134,8 @@ std::string Hash::to_string(HashFormat hashFormat, bool includeAlgo) const
|
|||
|
||||
Hash Hash::dummy(HashAlgorithm::SHA256);
|
||||
|
||||
Hash Hash::parseSRI(std::string_view original) {
|
||||
Hash Hash::parseSRI(std::string_view original)
|
||||
{
|
||||
auto rest = original;
|
||||
|
||||
// Parse the has type before the separater, if there was one.
|
||||
|
|
|
@ -163,8 +163,11 @@ struct CmdToBase : Command
|
|||
HashFormat hashFormat;
|
||||
std::optional<HashAlgorithm> hashAlgo;
|
||||
std::vector<std::string> args;
|
||||
bool legacyCli;
|
||||
|
||||
CmdToBase(HashFormat hashFormat) : hashFormat(hashFormat)
|
||||
CmdToBase(HashFormat hashFormat, bool legacyCli = false)
|
||||
: hashFormat(hashFormat)
|
||||
, legacyCli(legacyCli)
|
||||
{
|
||||
addFlag(flag::hashAlgoOpt("type", &hashAlgo));
|
||||
expectArgs("strings", &args);
|
||||
|
@ -181,7 +184,8 @@ struct CmdToBase : Command
|
|||
|
||||
void run() override
|
||||
{
|
||||
warn("The old format conversion sub commands of `nix hash` were deprecated in favor of `nix hash convert`.");
|
||||
if (!legacyCli)
|
||||
warn("The old format conversion subcommands of `nix hash` were deprecated in favor of `nix hash convert`.");
|
||||
for (const auto & s : args)
|
||||
logger->cout(Hash::parseAny(s, hashAlgo).to_string(hashFormat, hashFormat == HashFormat::SRI));
|
||||
}
|
||||
|
@ -222,11 +226,18 @@ struct CmdHashConvert : Command
|
|||
Category category() override { return catUtility; }
|
||||
|
||||
void run() override {
|
||||
for (const auto& s: hashStrings) {
|
||||
Hash h = Hash::parseAny(s, algo);
|
||||
if (from && h.to_string(*from, from == HashFormat::SRI) != s) {
|
||||
for (const auto & s : hashStrings) {
|
||||
Hash h =
|
||||
from == HashFormat::SRI
|
||||
? Hash::parseSRI(s)
|
||||
: Hash::parseAny(s, algo);
|
||||
if (from
|
||||
&& from != HashFormat::SRI
|
||||
&& h.to_string(*from, false) !=
|
||||
(from == HashFormat::Base16 ? toLower(s) : s))
|
||||
{
|
||||
auto from_as_string = printHashFormat(*from);
|
||||
throw BadHash("input hash '%s' does not have the expected format '--from %s'", s, from_as_string);
|
||||
throw BadHash("input hash '%s' does not have the expected format for '--from %s'", s, from_as_string);
|
||||
}
|
||||
logger->cout(h.to_string(to, to == HashFormat::SRI));
|
||||
}
|
||||
|
@ -321,7 +332,7 @@ static int compatNixHash(int argc, char * * argv)
|
|||
}
|
||||
|
||||
else {
|
||||
CmdToBase cmd(hashFormat);
|
||||
CmdToBase cmd(hashFormat, true);
|
||||
cmd.args = ss;
|
||||
if (hashAlgo.has_value()) cmd.hashAlgo = hashAlgo;
|
||||
cmd.run();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue