Permalink
Browse files

now we no longer depend on NDK_HASH, NDK_MD5, or NDK_SHA1. we now sti…

…ck with ngx_md5.h and ngx_sha1.h instead. thus no longer require openssl.
  • Loading branch information...
1 parent cfb305d commit 7b6aa70707e64ae114f27f4c0205a888f061f02f @agentzh agentzh committed Feb 8, 2011
View
@@ -1,47 +1,7 @@
-ngx_feature="OpenSSL md5 crypto library"
-ngx_feature_libs="-lcrypto"
-ngx_feature_name=
-ngx_feature_run=no
-ngx_feature_incs="#include <openssl/md5.h>"
-ngx_feature_path=
-ngx_feature_test="MD5(NULL, 0, NULL);"
-
-. auto/feature
-
-if [ $ngx_found = no ]; then
- # Solaris and others
- ngx_feature="OpenSSL md5 crypto library under /lib"
- ngx_feature_libs="-L/lib -lcrypto"
-
- . auto/feature
-
- if [ $ngx_found = no ]; then
- cat << END
- $0: error: the ngx_set_misc addon requires OpenSSL library but we cannot find it.
-END
- exit 1
- fi
-fi
-
-if [ $ngx_found = yes ]; then
- CORE_INCS="$CORE_INCS $ngx_feature_path"
- CORE_LIBS="$CORE_LIBS $ngx_feature_libs"
-else
- cat << END
- $0: error: the ngx_set_misc addon requires the OpenSSL crypto library.
-END
- exit 1
-fi
-
ngx_addon_name=ngx_http_set_misc_module
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_set_misc_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_set_base32.c $ngx_addon_dir/src/ngx_http_set_default_value.c $ngx_addon_dir/src/ngx_http_set_hashed_upstream.c $ngx_addon_dir/src/ngx_http_set_quote_sql.c $ngx_addon_dir/src/ngx_http_set_quote_json.c $ngx_addon_dir/src/ngx_http_set_unescape_uri.c $ngx_addon_dir/src/ngx_http_set_misc_module.c $ngx_addon_dir/src/ngx_http_set_escape_uri.c $ngx_addon_dir/src/ngx_http_set_hash.c $ngx_addon_dir/src/ngx_http_set_local_today.c"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/src/ddebug.h $ngx_addon_dir/src/ngx_http_set_default_value.h $ngx_addon_dir/src/ngx_http_set_hashed_upstream.h $ngx_addon_dir/src/ngx_http_set_quote_sql.h $ngx_addon_dir/src/ngx_http_set_quote_json.h $ngx_addon_dir/src/ngx_http_set_unescape_uri.h $ngx_addon_dir/src/ngx_http_set_escape_uri.h $ngx_addon_dir/src/ngx_http_set_hash.h $ngx_addon_dir/src/ngx_http_set_local_today.h"
-CFLAGS="$CFLAGS -DNDK_SET_VAR -DNGX_OPENSSL_MD5 -DNGX_HAVE_OPENSSL_MD5_H -DNGX_HAVE_OPENSSL_SHA1_H -DNDK_SET_VAR -DNDK_HASH -DNDK_SHA1 -DNDK_MD5"
-
-have=NDK_HASH . auto/have
-have=NDK_UPSTREAM_LIST . auto/have
-
-#USE_OPENSSL=YES
-USE_MD5=YES
+CFLAGS="$CFLAGS -DNDK_SET_VAR -DNDK_UPSTREAM_LIST"
USE_SHA1=YES
+USE_MD5=YES
@@ -81,11 +81,14 @@ ngx_http_set_misc_decode_base32(ngx_http_request_t *r,
}
-/* 实现参考 src/core/ngx_string.c 中的 ngx_(encode|decode)_base64() 例程 */
+/* 实现参考 src/core/ngx_string.c 中的 ngx_(encode|decode)_base64()
+ * 例程 */
/**
- * 将给定字符串转换成对应的 Base32 编码形式. 目标字符串必须保证有充足的空间容纳编码后的数据.
- * 可以用宏 base32_encoded_length() 预估编码后数据长度并预先为目标字符串分配空间.
+ * 将给定字符串转换成对应的 Base32 编码形式.
+ * 目标字符串必须保证有充足的空间容纳编码后的数据.
+ * 可以用宏 base32_encoded_length() 预估编码后数据
+ * 长度并预先为目标字符串分配空间.
* <code>
* char *src, *dst;
* int slen, dlen;
@@ -113,20 +116,6 @@ encode_base32(int slen, const char *src, int *dlen, char *dst)
d = (unsigned char*)dst;
while (len > 4) {
- /*
- * According to RFC 3548, The layout for input data is:
- *
- * Lower Addr --------------------> Higher Addr
- * msb lsb msb lsb msb lsb msb lsb msb lsb
- * 76543210 76543210 76543210 76543210 76543210
- *
- * After segmenting:
- *
- * Lower Addr -----------------------------------------------> Higher Addr
- * msb lsb msb lsb msb lsb msb lsb msb lsb msb lsb msb lsb msb lsb
- * ---76543 ---21076 ---54321 ---07654 ---32107 ---65432 ---10765 ---43210
- *
- * */
*d++ = basis32[s[0] >> 3];
*d++ = basis32[((s[0] & 0x07) << 2) | (s[1] >> 6)];
*d++ = basis32[(s[1] >> 1) & 0x1f];
@@ -140,33 +129,6 @@ encode_base32(int slen, const char *src, int *dlen, char *dst)
len -= 5;
}
- /* 处理非 5 整倍数的剩余字节串 */
- /**
- * Remain 1 byte:
- *
- * Lower Addr -----------------------------------------------> Higher Addr
- * msb lsb msb lsb padding0 padding1 padding2 padding3 padding4 padding5
- * ---76543 ---210-- ======== ======== ======== ======== ======== ========
- *
- * Remain 2 bytes:
- *
- * Lower Addr -----------------------------------------------> Higher Addr
- * msb lsb msb lsb msb lsb msb lsb padding0 padding1 padding2 padding3
- * ---76543 ---21076 ---54321 ---0---- ======== ======== ======== ========
- *
- * Remain 3 bytes:
- *
- * Lower Addr -----------------------------------------------> Higher Addr
- * msb lsb msb lsb msb lsb msb lsb msb lsb padding0 padding1 padding2
- * ---76543 ---21076 ---54321 ---07654 ---3210- ======== ======== ========
- *
- * Remain 4 bytes:
- *
- * Lower Addr -----------------------------------------------> Higher Addr
- * msb lsb msb lsb msb lsb msb lsb msb lsb msb lsb msb lsb padding0
- * ---76543 ---21076 ---54321 ---07654 ---32107 ---65432 ---10--- ========
- *
- **/
if (len) {
*d++ = basis32[s[0] >> 3];
@@ -220,48 +182,57 @@ encode_base32(int slen, const char *src, int *dlen, char *dst)
}
-/**
- * 将给定的 Base32 编码(大小写不敏感)数据转换成对应的原始字符串. 目标字符串必须保证有充足的空间容纳解码后的数据.
- * 可以用宏 base32_decoded_length() 预估解码后数据长度并预先为目标字符串分配空间.
- * <code>
- * char *src, *dst;
- * int slen, dlen;
- * slen = sizeof("6RRU6H5CI3======") - 1;
- * src = (char*)"6RRU6H5CI3======";
- * dst = malloc(base32_decoded_length(slen));
- * if (!decode_base32(slen, src, &(dlen), dst)) {
- * ...do something with dst
- * } else {
- * ...decoding error
- * }
- * </code>
- * @param slen 源数据串长度.
- * @param src 源数据串指针.
- * @param dlen 目标数据长度指针, 保存 Base32 解码后的数据长度.
- * @param dst 目标数据串指针, 保存 Base32 解码后的数据.
- * @retval 解码成功时返回0,失败时返回-1.
- * */
static int
decode_base32(int slen, const char *src, int *dlen, char *dst)
{
static unsigned char basis32[] = {
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 0 - 15 */
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 16 - 31 */
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 32 - 47 */
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 77, 77, 77, 77, 77, 77, /* 48 - 63 */
- 77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 64 - 79 */
- 25, 26, 27, 28, 29, 30, 31, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 80 - 95 */
- 77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 96 - 111 */
- 25, 26, 27, 28, 29, 30, 31, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 112 - 127 */
-
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 128 - 143 */
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 144 - 159 */
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 160 - 175 */
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 176 - 191 */
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 192 - 207 */
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 208 - 223 */
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, /* 224 - 239 */
- 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77 /* 240 - 255 */
+ /* 0 - 15 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 16 - 31 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 32 - 47 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 48 - 63 */
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 77, 77, 77, 77, 77, 77,
+
+ /* 64 - 79 */
+ 77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
+
+ /* 80 - 95 */
+ 25, 26, 27, 28, 29, 30, 31, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 96 - 111 */
+ 77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
+
+ /* 112 - 127 */
+ 25, 26, 27, 28, 29, 30, 31, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 128 - 143 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 144 - 159 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 160 - 175 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 176 - 191 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 192 - 207 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 208 - 223 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 224 - 239 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
+
+ /* 240 - 255 */
+ 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
};
int len, mod;
@@ -281,77 +252,45 @@ decode_base32(int slen, const char *src, int *dlen, char *dst)
mod = len % 8;
if (mod == 1 || mod == 3 || mod == 6) {
- /* Base32 编码串有效长度错误 */
+ /* bad Base32 digest length */
return -1;
}
while (len > 7) {
- /*
- * According to RFC 3548, The layout for input data is:
- *
- * Lower Addr -----------------------------------------------> Higher Addr
- * msb lsb msb lsb msb lsb msb lsb msb lsb msb lsb msb lsb msb lsb
- * ---76543 ---21076 ---54321 ---07654 ---32107 ---65432 ---10765 ---43210
- *
- * After assembling:
- *
- * Lower Addr --------------------> Higher Addr
- * msb lsb msb lsb msb lsb msb lsb msb lsb
- * 76543210 76543210 76543210 76543210 76543210
- *
- * */
*d++ = (basis32[s[0]] << 3) | ((basis32[s[1]] >> 2) & 0x07);
- *d++ = ((basis32[s[1]] & 0x03) << 6) | (basis32[s[2]] << 1) | ((basis32[s[3]] >> 4) & 1);
+
+ *d++ = ((basis32[s[1]] & 0x03) << 6) | (basis32[s[2]] << 1) |
+ ((basis32[s[3]] >> 4) & 1);
+
*d++ = ((basis32[s[3]] & 0x0f) << 4) | ((basis32[s[4]] >> 1) & 0x0f);
- *d++ = ((basis32[s[4]] & 1) << 7) | ((basis32[s[5]] & 0x1f) << 2) | ((basis32[s[6]] >> 3) & 0x03);
+
+ *d++ = ((basis32[s[4]] & 1) << 7) | ((basis32[s[5]] & 0x1f) << 2) |
+ ((basis32[s[6]] >> 3) & 0x03);
*d++ = ((basis32[s[6]] & 0x07) << 5) | (basis32[s[7]] & 0x1f);
s += 8;
len -= 8;
}
- /* 处理非 8 整倍数的 Base32 编码串 */
- /**
- * Remain 1 byte:
- *
- * Lower Addr -----------------------------------------------> Higher Addr
- * msb lsb msb lsb padding0 padding1 padding2 padding3 padding4 padding5
- * ---76543 ---210-- ======== ======== ======== ======== ======== ========
- *
- * Remain 2 bytes:
- *
- * Lower Addr -----------------------------------------------> Higher Addr
- * msb lsb msb lsb msb lsb msb lsb padding0 padding1 padding2 padding3
- * ---76543 ---21076 ---54321 ---0---- ======== ======== ======== ========
- *
- * Remain 3 bytes:
- *
- * Lower Addr -----------------------------------------------> Higher Addr
- * msb lsb msb lsb msb lsb msb lsb msb lsb padding0 padding1 padding2
- * ---76543 ---21076 ---54321 ---07654 ---3210- ======== ======== ========
- *
- * Remain 4 bytes:
- *
- * Lower Addr -----------------------------------------------> Higher Addr
- * msb lsb msb lsb msb lsb msb lsb msb lsb msb lsb msb lsb padding0
- * ---76543 ---21076 ---54321 ---07654 ---32107 ---65432 ---10--- ========
- *
- **/
if (len) {
/* 剩余 2 个字节 */
*d++ = (basis32[s[0]] << 3) | ((basis32[s[1]] >> 2) & 0x07);
if (len > 2) {
- /* 剩余 4 个字节 */
- *d++ = ((basis32[s[1]] & 0x03) << 6) | ((basis32[s[2]] & 0x1f) << 1) | ((basis32[s[3]] >> 4) & 1);
+ /* 4 bytes left */
+ *d++ = ((basis32[s[1]] & 0x03) << 6) | ((basis32[s[2]] & 0x1f) << 1)
+ | ((basis32[s[3]] >> 4) & 1);
if (len > 4) {
- /* 剩余 5 个字节 */
- *d++ = ((basis32[s[3]] & 0x0f) << 4) | ((basis32[s[4]] >> 1) & 0x0f);
+ /* 5 bytes left */
+ *d++ = ((basis32[s[3]] & 0x0f) << 4) |
+ ((basis32[s[4]] >> 1) & 0x0f);
if (len > 5) {
- /* 剩余 7 个字节 */
- *d++ = ((basis32[s[4]] & 1) << 7) | ((basis32[s[5]] & 0x1f) << 2) | ((basis32[s[6]] >> 3) & 0x03);
+ /* 7 bytes left */
+ *d++ = ((basis32[s[4]] & 1) << 7) |
+ ((basis32[s[5]] & 0x1f) << 2) |
+ ((basis32[s[6]] >> 3) & 0x03);
}
}
}
@@ -1,44 +1,72 @@
#define DDEBUG 0
#include "ddebug.h"
-#include <ndk.h>
#include "ngx_http_set_hash.h"
+#include "ngx_sha1.h"
+#include "ngx_md5.h"
+
+
+#ifndef SHA_DIGEST_LENGTH
+#define SHA_DIGEST_LENGTH 20
+#endif
+
+#ifndef MD5_DIGEST_LENGTH
+#define MD5_DIGEST_LENGTH 16
+#endif
+
+enum {
+ SHA_HEX_LENGTH = SHA_DIGEST_LENGTH * 2,
+ MD5_HEX_LENGTH = MD5_DIGEST_LENGTH * 2
+};
+
-#ifndef NGX_HTTP_SET_HASH
ngx_int_t
ngx_http_set_misc_set_sha1(ngx_http_request_t *r,
ngx_str_t *res, ngx_http_variable_value_t *v)
{
u_char *p;
+ ngx_sha1_t sha;
+ u_char sha_buf[SHA_DIGEST_LENGTH];
- p = ngx_palloc(r->pool, SHA_DIGEST_LENGTH * 2);
+ p = ngx_palloc(r->pool, SHA_HEX_LENGTH);
if (p == NULL) {
return NGX_ERROR;
}
- ndk_sha1_hash(p, (char *) v->data, v->len);
+ ngx_sha1_init(&sha);
+ ngx_sha1_update(&sha, v->data, v->len);
+ ngx_sha1_final(sha_buf, &sha);
+
+ ngx_hex_dump(p, sha_buf, sizeof(sha_buf));
res->data = p;
- res->len = SHA_DIGEST_LENGTH * 2;
+ res->len = SHA_HEX_LENGTH;
return NGX_OK;
}
+
ngx_int_t
ngx_http_set_misc_set_md5(ngx_http_request_t *r,
ngx_str_t *res, ngx_http_variable_value_t *v)
{
u_char *p;
- p = ngx_palloc(r->pool, MD5_DIGEST_LENGTH * 2);
+ ngx_md5_t md5;
+ u_char md5_buf[MD5_DIGEST_LENGTH];
+
+ p = ngx_palloc(r->pool, MD5_HEX_LENGTH);
if (p == NULL) {
return NGX_ERROR;
}
- ndk_md5_hash(p, (char *) v->data, v->len);
+ ngx_md5_init(&md5);
+ ngx_md5_update(&md5, v->data, v->len);
+ ngx_md5_final(md5_buf, &md5);
+
+ ngx_hex_dump(p, md5_buf, sizeof(md5_buf));
res->data = p;
- res->len = MD5_DIGEST_LENGTH * 2;
+ res->len = MD5_HEX_LENGTH;
return NGX_OK;
}
-#endif
Oops, something went wrong.

0 comments on commit 7b6aa70

Please sign in to comment.