]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: add the sha1 converter
authorDragan Dosen <ddosen@haproxy.com>
Tue, 24 Oct 2017 07:18:23 +0000 (09:18 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 25 Oct 2017 02:45:58 +0000 (04:45 +0200)
This converter can be used to generate a SHA1 digest from binary type
sample. The result is a binary sample with length of 20 bytes.

doc/configuration.txt
src/sample.c

index ed3f3f6e5419f52980505533f73a27f55b8b63b4..818931e37e3d93bf969b06e70e42ed7524d9e36c 100644 (file)
@@ -12912,6 +12912,10 @@ set-var(<var name>)
   This prefix is followed by a name. The separator is a '.'. The name may only
   contain characters 'a-z', 'A-Z', '0-9', '.' and '_'.
 
+sha1
+  Converts a binary input sample to a SHA1 digest. The result is a binary
+  sample with length of 20 bytes.
+
 sub(<value>)
   Subtracts <value> from the input value of type signed integer, and returns
   the result as an signed integer. Note: in order to subtract the input from
index 28a5fcb26286b7030e9780bffb3f027c2b2f30da..6b767a17db09fd05679feb8cceb2224c3386a78a 100644 (file)
@@ -33,6 +33,7 @@
 #include <proto/stick_table.h>
 #include <proto/vars.h>
 
+#include <import/sha1.h>
 #include <import/xxhash.h>
 
 /* sample type names */
@@ -1504,6 +1505,24 @@ static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, v
        return 1;
 }
 
+static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
+{
+       blk_SHA_CTX ctx;
+       struct chunk *trash = get_trash_chunk();
+
+       memset(&ctx, 0, sizeof(ctx));
+
+       blk_SHA1_Init(&ctx);
+       blk_SHA1_Update(&ctx, smp->data.u.str.str, smp->data.u.str.len);
+       blk_SHA1_Final((unsigned char *)trash->str, &ctx);
+
+       trash->len = 20;
+       smp->data.u.str = *trash;
+       smp->data.type = SMP_T_BIN;
+       smp->flags &= ~SMP_F_CONST;
+       return 1;
+}
+
 static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
 {
        struct chunk *trash = get_trash_chunk();
@@ -2756,6 +2775,7 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH, {
        { "field",  sample_conv_field,     ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR,  SMP_T_STR },
        { "word",   sample_conv_word,      ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR,  SMP_T_STR },
        { "regsub", sample_conv_regsub,    ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
+       { "sha1",   sample_conv_sha1,      0,            NULL, SMP_T_BIN,  SMP_T_BIN  },
 
        { "and",    sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT  },
        { "or",     sample_conv_binary_or,  ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT  },