Skip to content
Browse files

Fix bug with hash functions

  • Loading branch information...
1 parent d8e2750 commit b908c8564e109d5ca0677df718d538101a040b52 Marcus Clyne committed Oct 13, 2010
Showing with 73 additions and 46 deletions.
  1. +1 −0 auto/data/module_dependencies
  2. +28 −28 src/ndk_hash.c
  3. +23 −12 src/ndk_hash.h
  4. +4 −4 src/ndk_set_var.c
  5. +1 −1 src/ndk_set_var.h
  6. +16 −1 src/ndk_string.h
View
1 auto/data/module_dependencies
@@ -1,4 +1,5 @@
complex_path complex_value path
conf_file string
core log
+hash string
set_var expose_rewrite_functions
View
56 src/ndk_hash.c
@@ -1,52 +1,51 @@
-#include "ndk_hash.h"
+
+#include <ndk_hash.h>
+
+
// openssl hashes
-#define NDK_OPENSSL_HASH(type,ctxt_type,upper) \
- u_char md [ctxt_type ## _DIGEST_LENGTH]; \
- ngx_uint_t i; \
- ctxt_type ##_CTX c; \
- \
- type ## _Init (&c); \
- type ## _Update (&c, data, len); \
- type ## _Final (md, &c); \
- \
- ngx_hex_dump((u_char *) p, (u_char *) md, ctxt_type ## _DIGEST_LENGTH); \
- if (upper) { \
- for (i = 0; i < ctxt_type ## _DIGEST_LENGTH; i++) { \
- *p = ngx_toupper(md[i]); \
- } \
+#define NDK_OPENSSL_HASH(type,ctxt_type,upper) \
+ u_char md [ctxt_type ## _DIGEST_LENGTH]; \
+ ctxt_type ##_CTX c; \
+ \
+ type ## _Init (&c); \
+ type ## _Update (&c, data, len); \
+ type ## _Final (md, &c); \
+ \
+ ndk_hex_dump (p, (u_char *) md, ctxt_type ## _DIGEST_LENGTH); \
+ if (upper) { \
+ ndk_strtoupper (p, (ctxt_type ## _DIGEST_LENGTH) *2); \
}
-// TODO : check to see if can do consistent printing using "%08X" or in single go
#ifdef NDK_MD5
void
-ndk_md5_hash (char *p, char *data, size_t len)
+ndk_md5_hash (u_char *p, char *data, size_t len)
{
- NDK_OPENSSL_HASH (MD5, MD5, 1);
+ NDK_OPENSSL_HASH (MD5, MD5, 0);
}
void
-ndk_md5_lower_hash (char *p, char *data, size_t len)
+ndk_md5_hash_upper (u_char *p, char *data, size_t len)
{
- NDK_OPENSSL_HASH (MD5, MD5, 0);
+ NDK_OPENSSL_HASH (MD5, MD5, 1);
}
#endif
#ifdef NDK_SHA1
void
-ndk_sha1_hash (char *p, char *data, size_t len)
+ndk_sha1_hash (u_char *p, char *data, size_t len)
{
- NDK_OPENSSL_HASH (SHA1, SHA, 1);
+ NDK_OPENSSL_HASH (SHA1, SHA, 0);
}
void
-ndk_sha1_lower_hash (char *p, char *data, size_t len)
+ndk_sha1_hash_upper (u_char *p, char *data, size_t len)
{
- NDK_OPENSSL_HASH (SHA1, SHA, 0);
+ NDK_OPENSSL_HASH (SHA1, SHA, 1);
}
#endif
@@ -60,23 +59,24 @@ ndk_sha1_lower_hash (char *p, char *data, size_t len)
#include "hash/murmurhash2.c"
void
-ndk_murmur2_hash (char *p, char *data, size_t len)
+ndk_murmur2_hash (u_char *p, char *data, size_t len)
{
uint32_t hash;
hash = MurmurHash2 (data, len, 47);
- (void) ngx_sprintf ((u_char *) p, "%08X%Z", hash);
+ ndk_hex_dump (p, (u_char*) &hash, 4);
}
void
-ndk_murmur2_lower_hash (char *p, char *data, size_t len)
+ndk_murmur2_hash_upper (u_char *p, char *data, size_t len)
{
uint32_t hash;
hash = MurmurHash2 (data, len, 47);
- (void) ngx_sprintf ((u_char *) p, "%08x%Z", hash);
+ ndk_hex_dump (p, (u_char*) &hash, 4);
+ ndk_strtoupper (p, 8);
}
#endif
View
35 src/ndk_hash.h
@@ -1,33 +1,44 @@
+
#ifndef NDK_HASH_H
#define NDK_HASH_H
-// TODO : add more hashes
+#ifdef NDK_HASH_ALL
-#include <ngx_core.h>
-#include <ngx_md5.h>
-#include <ngx_sha1.h>
+#ifndef NDK_MD5
+#define NDK_MD5
+#endif
+#ifndef NDK_SHA1
+#define NDK_SHA1
+#endif
-#define NDK_MURMUR2 // NOTE : for now, always include
+#ifndef NDK_MURMUR2
+#define NDK_MURMUR2
+#endif
+#endif
-typedef void (*ndk_hash_pt) (char *p, char *data, size_t len);
+#include <ngx_config.h>
+#include <ngx_core.h>
+typedef void (*ndk_hash_pt) (u_char *p, char *data, size_t len);
#ifdef NDK_MD5
-void ndk_md5_hash (char *p, char *data, size_t len);
-void ndk_md5_lower_hash (char *p, char *data, size_t len);
+#include <ngx_md5.h>
+void ndk_md5_hash (u_char *p, char *data, size_t len);
+void ndk_md5_hash_upper (u_char *p, char *data, size_t len);
#endif
#ifdef NDK_MURMUR2
#define MURMURHASH2_DIGEST_LENGTH 4
-void ndk_murmur2_hash (char *p, char *data, size_t len);
-void ndk_murmur2_lower_hash (char *p, char *data, size_t len);
+void ndk_murmur2_hash (u_char *p, char *data, size_t len);
+void ndk_murmur2_hash_upper (u_char *p, char *data, size_t len);
#endif
#ifdef NDK_SHA1
-void ndk_sha1_hash (char *p, char *data, size_t len);
-void ndk_sha1_lower_hash (char *p, char *data, size_t len);
+#include <ngx_sha1.h>
+void ndk_sha1_hash (u_char *p, char *data, size_t len);
+void ndk_sha1_hash_upper (u_char *p, char *data, size_t len);
#endif
#endif /* NDK_HASH_H */
View
8 src/ndk_set_var.c
@@ -220,7 +220,7 @@ ndk_set_var_multi_value_data_code (ngx_http_script_engine_t *e)
static void
ndk_set_var_hash_code (ngx_http_script_engine_t *e)
{
- char *p;
+ u_char *p;
ngx_http_variable_value_t *v;
ndk_set_var_size_code_t *svs;
ndk_set_var_hash_pt func;
@@ -229,7 +229,7 @@ ndk_set_var_hash_code (ngx_http_script_engine_t *e)
e->ip += sizeof (ndk_set_var_size_code_t);
- p = ngx_pnalloc (e->request->pool, svs->size);
+ p = ngx_palloc (e->request->pool, svs->size);
if (p == NULL) {
e->ip = ngx_http_script_exit;
e->status = NGX_HTTP_INTERNAL_SERVER_ERROR;
@@ -239,14 +239,14 @@ ndk_set_var_hash_code (ngx_http_script_engine_t *e)
v = e->sp - 1;
func = svs->func;
-
+
func (p, (char *) v->data, v->len);
v->data = (u_char *) p;
v->len = svs->size;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, e->request->connection->log, 0,
- "http script value (post filter): \"%v\"", v);
+ "http script hashed value: \"%v\"", v);
}
View
2 src/ndk_set_var.h
@@ -9,7 +9,7 @@ typedef ngx_int_t (*ndk_set_var_pt) (ngx_http_request_t *r, ngx_s
typedef ngx_int_t (*ndk_set_var_data_pt) (ngx_http_request_t *r, ngx_str_t *val, void *data);
typedef ngx_int_t (*ndk_set_var_value_pt) (ngx_http_request_t *r, ngx_str_t *val, ngx_http_variable_value_t *v);
typedef ngx_int_t (*ndk_set_var_value_data_pt) (ngx_http_request_t *r, ngx_str_t *val, ngx_http_variable_value_t *v, void *data);
-typedef void (*ndk_set_var_hash_pt) (char *p, char *data, size_t len);
+typedef void (*ndk_set_var_hash_pt) (u_char *p, char *data, size_t len);
typedef struct {
View
17 src/ndk_string.h
@@ -1,6 +1,5 @@
-
#define ndk_str_init(ns,s) {(ns).data = (u_char*) s; (ns).len = sizeof (s) - 1;}
#define ndk_strp_init(ns,s) {(ns)->data = (u_char*) s; (ns)->len = sizeof (s) - 1;}
@@ -9,9 +8,25 @@
#define ndk_zeropn(p,n) ndk_zero (p,sizeof(*p)*(n))
#define ndk_zerov(v) ndk_zero (&v,sizeof(v))
+#if 1
+// TODO : set ndk_hex_dump for older versions of Nginx
+#define ndk_hex_dump ngx_hex_dump
+#endif
+
int64_t ndk_atoi64 (u_char *line, size_t n);
ngx_int_t ndk_strcntc (ngx_str_t *s, char c);
ngx_int_t ndk_strccnt (char *s, char c);
ngx_array_t * ndk_str_array_create (ngx_pool_t *pool, char **s, ngx_int_t n);
+
+
+static inline void
+ndk_strtoupper (u_char *p, size_t len)
+{
+ u_char *e = p + len;
+ for ( ; p<e; p++) {
+ *p = ngx_toupper(*p);
+ }
+}
+

0 comments on commit b908c85

Please sign in to comment.
Something went wrong with that request. Please try again.