From c0c5f9db2a0887f8fff1910cdc9ba0a726b50251 Mon Sep 17 00:00:00 2001 From: Graham Leggett Date: Sun, 17 Mar 2013 13:52:58 +0000 Subject: [PATCH] Expression parser: Add the ability to apply a SHA1 hash to strings within the parser. git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1457450 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/expr.xml | 3 +++ server/util_expr_eval.c | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/docs/manual/expr.xml b/docs/manual/expr.xml index 51bfbf3d4b..8079fd266f 100644 --- a/docs/manual/expr.xml +++ b/docs/manual/expr.xml @@ -477,6 +477,9 @@ listfunction ::= listfuncname "(" word ")" unbase64 Decode base64 encoded string, return truncated string if 0x00 is found + sha1 + Hash the string using SHA1, then encode the hash with base64 + encoding file Read contents from a fileyes filesize diff --git a/server/util_expr_eval.c b/server/util_expr_eval.c index 4088fc42ae..3ee0ff5a7f 100644 --- a/server/util_expr_eval.c +++ b/server/util_expr_eval.c @@ -28,6 +28,8 @@ #include "apr_lib.h" #include "apr_fnmatch.h" +#include "apr_base64.h" +#include "apr_sha1.h" #include /* for INT_MAX */ @@ -1031,6 +1033,25 @@ static const char *unbase64_func(ap_expr_eval_ctx_t *ctx, const void *data, return ap_pbase64decode(ctx->p, arg); } +static const char *sha1_func(ap_expr_eval_ctx_t *ctx, const void *data, + const char *arg) +{ + int l; + apr_sha1_ctx_t context; + apr_byte_t digest[APR_SHA1_DIGESTSIZE]; + char *out = apr_palloc(ctx->p, 28); + + apr_sha1_init(&context); + apr_sha1_update(&context, arg, strlen(arg)); + apr_sha1_final(digest, &context); + + /* SHA1 hash is always 20 chars */ + l = apr_base64_encode_binary(out, digest, sizeof(digest)); + out[l] = '\0'; + + return out; +} + #define MAX_FILE_SIZE 10*1024*1024 static const char *file_func(ap_expr_eval_ctx_t *ctx, const void *data, char *arg) @@ -1612,6 +1633,7 @@ static const struct expr_provider_single string_func_providers[] = { { filesize_func, "filesize", NULL, 1 }, { base64_func, "base64", NULL, 0 }, { unbase64_func, "unbase64", NULL, 0 }, + { sha1_func, "sha1", NULL, 0 }, { NULL, NULL, NULL} }; /* XXX: base64 encode/decode ? */ -- 2.40.0