aboutsummaryrefslogtreecommitdiff
path: root/src/nix/hash.cc
diff options
context:
space:
mode:
authorYueh-Shun Li <44064051+ShamrockLee@users.noreply.github.com>2023-03-16 00:33:58 +0800
committerYueh-Shun Li <44064051+ShamrockLee@users.noreply.github.com>2023-03-16 03:08:42 +0800
commitc27d358abba7154f16795d48d2a44fa856ab6b1e (patch)
treec66d05dc9a430c8fe7026493f2f2c57401c255c6 /src/nix/hash.cc
parent08510c5ed0fe4b9f8266bb37c859f0ed7fa342db (diff)
nix-hash: support base-64 and SRI format
Add the --base64 and --sri flags for the Base64 and SRI format output. Add the --base16 flag to explicitly specify the hexadecimal format. Add the --to-base64 and --to-sri flag to convert a hash to the above mentioned format.
Diffstat (limited to 'src/nix/hash.cc')
-rw-r--r--src/nix/hash.cc38
1 files changed, 28 insertions, 10 deletions
diff --git a/src/nix/hash.cc b/src/nix/hash.cc
index 60d9593a7..e2630505b 100644
--- a/src/nix/hash.cc
+++ b/src/nix/hash.cc
@@ -161,11 +161,11 @@ static auto rCmdHash = registerCommand<CmdHash>("hash");
/* Legacy nix-hash command. */
static int compatNixHash(int argc, char * * argv)
{
- HashType ht = htMD5;
+ std::optional<HashType> ht;
bool flat = false;
- bool base32 = false;
+ Base base = Base16;
bool truncate = false;
- enum { opHash, opTo32, opTo16 } op = opHash;
+ enum { opHash, opTo } op = opHash;
std::vector<std::string> ss;
parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) {
@@ -174,14 +174,31 @@ static int compatNixHash(int argc, char * * argv)
else if (*arg == "--version")
printVersion("nix-hash");
else if (*arg == "--flat") flat = true;
- else if (*arg == "--base32") base32 = 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 == "--truncate") truncate = true;
else if (*arg == "--type") {
std::string s = getArg(*arg, arg, end);
ht = parseHashType(s);
}
- else if (*arg == "--to-base16") op = opTo16;
- else if (*arg == "--to-base32") op = opTo32;
+ else if (*arg == "--to-base16") {
+ op = opTo;
+ base = Base16;
+ }
+ else if (*arg == "--to-base32") {
+ op = opTo;
+ base = Base32;
+ }
+ else if (*arg == "--to-base64") {
+ op = opTo;
+ base = Base64;
+ }
+ else if (*arg == "--to-sri") {
+ op = opTo;
+ base = SRI;
+ }
else if (*arg != "" && arg->at(0) == '-')
return false;
else
@@ -191,17 +208,18 @@ static int compatNixHash(int argc, char * * argv)
if (op == opHash) {
CmdHashBase cmd(flat ? FileIngestionMethod::Flat : FileIngestionMethod::Recursive);
- cmd.ht = ht;
- cmd.base = base32 ? Base32 : Base16;
+ if (!ht.has_value()) ht = htMD5;
+ cmd.ht = ht.value();
+ cmd.base = base;
cmd.truncate = truncate;
cmd.paths = ss;
cmd.run();
}
else {
- CmdToBase cmd(op == opTo32 ? Base32 : Base16);
+ CmdToBase cmd(base);
cmd.args = ss;
- cmd.ht = ht;
+ if (ht.has_value()) cmd.ht = ht;
cmd.run();
}