Skip to content
This repository
Browse code

tweaked drdrxp's hmac-sha1 patch a bit and also added a test case for…

… it.
  • Loading branch information...
commit 09fc7c73ee51a221d831490eb433d071fe7784c1 1 parent fd47016
Yichun Zhang authored November 17, 2011
10  src/ngx_http_lua_common.h
@@ -9,6 +9,11 @@
9 9
 
10 10
 #include "ddebug.h"
11 11
 
  12
+#include <nginx.h>
  13
+#include <ngx_core.h>
  14
+#include <ngx_http.h>
  15
+#include <ngx_md5.h>
  16
+
12 17
 #include <assert.h>
13 18
 #include <setjmp.h>
14 19
 
@@ -16,11 +21,6 @@
16 21
 #include <lualib.h>
17 22
 #include <lauxlib.h>
18 23
 
19  
-#include <nginx.h>
20  
-#include <ngx_core.h>
21  
-#include <ngx_http.h>
22  
-#include <ngx_md5.h>
23  
-
24 24
 #if defined(NDK) && NDK
25 25
 #include <ndk.h>
26 26
 #endif
20  src/ngx_http_lua_string.c
@@ -8,6 +8,11 @@
8 8
 #include "ngx_http_lua_util.h"
9 9
 #include "ngx_crc32.h"
10 10
 
  11
+#if (NGX_OPENSSL)
  12
+#include <openssl/evp.h>
  13
+#include <openssl/hmac.h>
  14
+#endif
  15
+
11 16
 
12 17
 static uintptr_t ngx_http_lua_ngx_escape_sql_str(u_char *dst, u_char *src,
13 18
         size_t size);
@@ -63,7 +68,6 @@ ngx_http_lua_inject_string_api(lua_State *L)
63 68
     lua_pushcfunction(L, ngx_http_lua_ngx_hmac_sha1);
64 69
     lua_setfield(L, -2, "hmac_sha1");
65 70
 #endif
66  
-
67 71
 }
68 72
 
69 73
 
@@ -518,14 +522,15 @@ ngx_http_lua_ngx_encode_args(lua_State *L) {
518 522
 }
519 523
 
520 524
 #if (NGX_OPENSSL)
521  
-#include <openssl/evp.h>
522  
-#include <openssl/hmac.h>
523 525
 
524 526
 static int
525 527
 ngx_http_lua_ngx_hmac_sha1(lua_State *L)
526 528
 {
527 529
     u_char                  *sec, *sts;
528 530
     size_t                   lsec, lsts;
  531
+    unsigned int             md_len;
  532
+    unsigned char            md[EVP_MAX_MD_SIZE];
  533
+    const EVP_MD            *evp_md;
529 534
 
530 535
     if (lua_gettop(L) != 2) {
531 536
         return luaL_error(L, "expecting one argument, but got %d",
@@ -535,20 +540,13 @@ ngx_http_lua_ngx_hmac_sha1(lua_State *L)
535 540
     sec = (u_char *) luaL_checklstring(L, 1, &lsec);
536 541
     sts = (u_char *) luaL_checklstring(L, 2, &lsts);
537 542
 
538  
-
539  
-
540  
-    unsigned int                 md_len;
541  
-    unsigned char                md[EVP_MAX_MD_SIZE];
542  
-    const EVP_MD                *evp_md;
543  
-
544 543
     evp_md = EVP_sha1();
545 544
 
546 545
     HMAC(evp_md, sec, lsec, sts, lsts, md, &md_len);
547 546
 
548 547
     lua_pushlstring(L, (char *) md, md_len);
549 548
 
550  
-
551 549
     return 1;
552  
-
553 550
 }
554 551
 #endif
  552
+
33  t/046-hmac.t
... ...
@@ -0,0 +1,33 @@
  1
+# vim:set ft= ts=4 sw=4 et fdm=marker:
  2
+use lib 'lib';
  3
+use Test::Nginx::Socket;
  4
+
  5
+#worker_connections(1014);
  6
+#master_on();
  7
+#workers(2);
  8
+#log_level('warn');
  9
+
  10
+repeat_each(2);
  11
+#repeat_each(1);
  12
+
  13
+plan tests => repeat_each() * (blocks() * 2);
  14
+
  15
+#no_diff();
  16
+#no_long_string();
  17
+run_tests();
  18
+
  19
+__DATA__
  20
+
  21
+=== TEST 1: sanity
  22
+--- config
  23
+    location /lua {
  24
+        content_by_lua '
  25
+            local digest = ngx.hmac_sha1("thisisverysecretstuff", "some string we want to sign")
  26
+            ngx.say(ngx.encode_base64(digest))
  27
+        ';
  28
+    }
  29
+--- request
  30
+GET /lua
  31
+--- response_body
  32
+R/pvxzHC4NLtj7S+kXFg/NePTmk=
  33
+
1  util/build2.sh
@@ -13,6 +13,7 @@ force=$2
13 13
             #--without-http_rewrite_module \
14 14
 
15 15
 ngx-build $force $version \
  16
+        --with-http_ssl_module \
16 17
             --add-module=$root/../ndk-nginx-module \
17 18
             --add-module=$root/../set-misc-nginx-module \
18 19
             --with-cc-opt=$'-O0' \

0 notes on commit 09fc7c7

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