From 01a5ea9914b3933e63a88184861900615be76e12 Mon Sep 17 00:00:00 2001 From: Marc Weber Date: Thu, 7 Feb 2013 00:03:46 +0100 Subject: [PATCH] experimental/hash adding primop function calculating hash of a string Signed-off-by: Marc Weber --- src/libexpr/primops.cc | 26 ++++++++++++++++++++++++++ tests/lang/eval-okay-hash.exp | 1 + tests/lang/eval-okay-hash.nix | 6 ++++++ 3 files changed, 33 insertions(+) create mode 100644 tests/lang/eval-okay-hash.exp create mode 100644 tests/lang/eval-okay-hash.nix diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index f8f893d69..84c4bbb88 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1107,6 +1107,30 @@ static void prim_unsafeDiscardOutputDependency(EvalState & state, Value * * args } +static void prim_hash(EvalState & state, Value * * args, Value & v) +{ + PathSet context; + + string type = state.forceStringNoCtx(*args[0]); + string s = state.forceStringNoCtx(*args[1]); + + HashType ht; + if (type == "md5"){ + ht = htMD5; + } else if (type == "sha256"){ + ht = htSHA256; + } else { + throw Error(format("bad hash type `%1%'") % type); + } + + Hash h = hashString(ht, s); + + string hash = printHash(h); + + mkString(v, hash, context); +}; + + /************************************************************* * Versions *************************************************************/ @@ -1235,6 +1259,8 @@ void EvalState::createBaseEnv() addPrimOp("__unsafeDiscardStringContext", 1, prim_unsafeDiscardStringContext); addPrimOp("__unsafeDiscardOutputDependency", 1, prim_unsafeDiscardOutputDependency); + addPrimOp("__hash", 2, prim_hash); + // Versions addPrimOp("__parseDrvName", 1, prim_parseDrvName); addPrimOp("__compareVersions", 2, prim_compareVersions); diff --git a/tests/lang/eval-okay-hash.exp b/tests/lang/eval-okay-hash.exp new file mode 100644 index 000000000..369b983d5 --- /dev/null +++ b/tests/lang/eval-okay-hash.exp @@ -0,0 +1 @@ +[ "6c69ee7f211c640419d5366cc076ae46" "bb3438fbabd460ea6dbd27d153e2233b" "900a4469df00ccbfd0c145c6d1e4b7953dd0afafadd7534e3a4019e8d38fc663" "ad0387b3bd8652f730ca46d25f9c170af0fd589f42e7f23f5a9e6412d97d7e56" ] diff --git a/tests/lang/eval-okay-hash.nix b/tests/lang/eval-okay-hash.nix new file mode 100644 index 000000000..65cd8afeb --- /dev/null +++ b/tests/lang/eval-okay-hash.nix @@ -0,0 +1,6 @@ +let + md5 = builtins.hash "md5"; + sha256 = builtins.hash "sha256"; + strings = [ "text 1" "text 2" ]; +in + (builtins.map md5 strings) ++ (builtins.map sha256 strings)