From 52172607cfc33867c0cdb526bef99c315e98baa2 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 8 Feb 2013 19:36:23 +0100 Subject: [PATCH] Rename "hash" to "hashString" and handle SHA-1 --- src/libexpr/primops.cc | 28 +++++++++------------------- tests/lang/eval-okay-hash.exp | 2 +- tests/lang/eval-okay-hash.nix | 9 +++++---- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index 84c4bbb88..f66b24b77 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1107,27 +1107,18 @@ static void prim_unsafeDiscardOutputDependency(EvalState & state, Value * * args } -static void prim_hash(EvalState & state, Value * * args, Value & v) +/* Return the cryptographic hash of a string in base-16. */ +static void prim_hashString(EvalState & state, Value * * args, Value & v) { - PathSet context; - string type = state.forceStringNoCtx(*args[0]); - string s = state.forceStringNoCtx(*args[1]); + HashType ht = parseHashType(type); + if (ht == htUnknown) + throw Error(format("unknown hash type `%1%'") % type); - HashType ht; - if (type == "md5"){ - ht = htMD5; - } else if (type == "sha256"){ - ht = htSHA256; - } else { - throw Error(format("bad hash type `%1%'") % type); - } + PathSet context; // discarded + string s = state.forceString(*args[1], context); - Hash h = hashString(ht, s); - - string hash = printHash(h); - - mkString(v, hash, context); + mkString(v, printHash(hashString(ht, s)), context); }; @@ -1258,8 +1249,7 @@ void EvalState::createBaseEnv() addPrimOp("__stringLength", 1, prim_stringLength); addPrimOp("__unsafeDiscardStringContext", 1, prim_unsafeDiscardStringContext); addPrimOp("__unsafeDiscardOutputDependency", 1, prim_unsafeDiscardOutputDependency); - - addPrimOp("__hash", 2, prim_hash); + addPrimOp("__hashString", 2, prim_hashString); // Versions addPrimOp("__parseDrvName", 1, prim_parseDrvName); diff --git a/tests/lang/eval-okay-hash.exp b/tests/lang/eval-okay-hash.exp index 369b983d5..7bbe452bc 100644 --- a/tests/lang/eval-okay-hash.exp +++ b/tests/lang/eval-okay-hash.exp @@ -1 +1 @@ -[ "6c69ee7f211c640419d5366cc076ae46" "bb3438fbabd460ea6dbd27d153e2233b" "900a4469df00ccbfd0c145c6d1e4b7953dd0afafadd7534e3a4019e8d38fc663" "ad0387b3bd8652f730ca46d25f9c170af0fd589f42e7f23f5a9e6412d97d7e56" ] +[ "d41d8cd98f00b204e9800998ecf8427e" "6c69ee7f211c640419d5366cc076ae46" "bb3438fbabd460ea6dbd27d153e2233b" "da39a3ee5e6b4b0d3255bfef95601890afd80709" "cd54e8568c1b37cf1e5badb0779bcbf382212189" "6d12e10b1d331dad210e47fd25d4f260802b7e77" "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" "900a4469df00ccbfd0c145c6d1e4b7953dd0afafadd7534e3a4019e8d38fc663" "ad0387b3bd8652f730ca46d25f9c170af0fd589f42e7f23f5a9e6412d97d7e56" ] diff --git a/tests/lang/eval-okay-hash.nix b/tests/lang/eval-okay-hash.nix index 65cd8afeb..2fff17f84 100644 --- a/tests/lang/eval-okay-hash.nix +++ b/tests/lang/eval-okay-hash.nix @@ -1,6 +1,7 @@ let - md5 = builtins.hash "md5"; - sha256 = builtins.hash "sha256"; - strings = [ "text 1" "text 2" ]; + md5 = builtins.hashString "md5"; + sha1 = builtins.hashString "sha1"; + sha256 = builtins.hashString "sha256"; + strings = [ "" "text 1" "text 2" ]; in - (builtins.map md5 strings) ++ (builtins.map sha256 strings) + (builtins.map md5 strings) ++ (builtins.map sha1 strings) ++ (builtins.map sha256 strings)