From 6e5a9ca94830ba40ac97a43e6232114601e1ae6f Mon Sep 17 00:00:00 2001 From: Dragan Dosen Date: Tue, 24 Oct 2017 09:18:23 +0200 Subject: [PATCH] MINOR: sample: add the sha1 converter 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 | 4 ++++ src/sample.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/doc/configuration.txt b/doc/configuration.txt index ed3f3f6e5..818931e37 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -12912,6 +12912,10 @@ set-var() 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() Subtracts from the input value of type signed integer, and returns the result as an signed integer. Note: in order to subtract the input from diff --git a/src/sample.c b/src/sample.c index 28a5fcb26..6b767a17d 100644 --- a/src/sample.c +++ b/src/sample.c @@ -33,6 +33,7 @@ #include #include +#include #include /* 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 },