From 70dee0f8ca9d7236008e161d1d1210b945ec832e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 1 Mar 2006 18:05:47 +0000 Subject: * Flags `--to-base32' and `--to-base16' to convert between hex and base-32 hashes. --- src/nix-hash/nix-hash.cc | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/nix-hash/nix-hash.cc b/src/nix-hash/nix-hash.cc index 4a7c943963d9..dd2ed8d4f4fa 100644 --- a/src/nix-hash/nix-hash.cc +++ b/src/nix-hash/nix-hash.cc @@ -17,7 +17,10 @@ void run(Strings args) bool flat = false; bool base32 = false; bool truncate = false; - + enum { opHash, opTo32, opTo16 } op = opHash; + + Strings ss; + for (Strings::iterator i = args.begin(); i != args.end(); i++) { @@ -31,13 +34,27 @@ void run(Strings args) if (ht == htUnknown) throw UsageError(format("unknown hash type `%1%'") % *i); } - else { + else if (*i == "--to-base16") op = opTo16; + else if (*i == "--to-base32") op = opTo32; + else ss.push_back(*i); + } + + if (op == opHash) { + for (Strings::iterator i = ss.begin(); i != ss.end(); ++i) { Hash h = flat ? hashFile(ht, *i) : hashPath(ht, *i); if (truncate && h.hashSize > 20) h = compressHash(h, 20); cout << format("%1%\n") % (base32 ? printHash32(h) : printHash(h)); } } + + else { + for (Strings::iterator i = ss.begin(); i != ss.end(); ++i) { + Hash h = op == opTo16 ? parseHash32(ht, *i) : parseHash(ht, *i); + cout << format("%1%\n") % + (op == opTo16 ? printHash(h) : printHash32(h)); + } + } } -- cgit 1.4.1