about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMarc Weber <marco-oweber@gmx.de>2013-02-06T23·03+0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-02-08T18·26+0100
commit01a5ea9914b3933e63a88184861900615be76e12 (patch)
tree3c491ce644fe995d67fe53c0d33bb3162421de78 /src
parent8add116acd050bdcca84b8a092420566a6e6692c (diff)
experimental/hash
adding primop function calculating hash of a string

Signed-off-by: Marc Weber <marco-oweber@gmx.de>
Diffstat (limited to 'src')
-rw-r--r--src/libexpr/primops.cc26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index f8f893d69683..84c4bbb883ce 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);