Skip to content

Commit

Permalink
feature: enabled the FFI-based APIs for 'ngx.md5', 'ngx.md5_bin', and…
Browse files Browse the repository at this point in the history
… 'ngx.sha1_bin' in the stream subsystem.
  • Loading branch information
thibaultcha committed Aug 27, 2019
1 parent 4b0dd53 commit cdb9616
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/resty/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ require "resty.core.regex"
require "resty.core.shdict"
require "resty.core.time"
require "resty.core.misc"
require "resty.core.hash"


if subsystem == 'http' then
require "resty.core.uri"
require "resty.core.hash"
require "resty.core.base64"
require "resty.core.exit"
require "resty.core.var"
Expand Down
50 changes: 40 additions & 10 deletions lib/resty/core/hash.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
-- Copyright (C) Yichun Zhang (agentzh)


local ffi = require 'ffi'
local ffi_string = ffi.string
local ffi_new = ffi.new
local ffi = require "ffi"
local base = require "resty.core.base"


local C = ffi.C
local ffi_new = ffi.new
local ffi_string = ffi.string
local ngx = ngx
local type = type
local tostring = tostring
local error = error
local base = require "resty.core.base"
local tostring = tostring
local subsystem = ngx.config.subsystem


local ngx_lua_ffi_md5
local ngx_lua_ffi_md5_bin
local ngx_lua_ffi_sha1_bin


ffi.cdef[[
if subsystem == "http" then
ffi.cdef[[
void ngx_http_lua_ffi_md5_bin(const unsigned char *src, size_t len,
unsigned char *dst);

Expand All @@ -21,7 +30,28 @@ ffi.cdef[[

int ngx_http_lua_ffi_sha1_bin(const unsigned char *src, size_t len,
unsigned char *dst);
]]
]]

ngx_lua_ffi_md5 = C.ngx_http_lua_ffi_md5
ngx_lua_ffi_md5_bin = C.ngx_http_lua_ffi_md5_bin
ngx_lua_ffi_sha1_bin = C.ngx_http_lua_ffi_sha1_bin

elseif subsystem == "stream" then
ffi.cdef[[
void ngx_stream_lua_ffi_md5_bin(const unsigned char *src, size_t len,
unsigned char *dst);

void ngx_stream_lua_ffi_md5(const unsigned char *src, size_t len,
unsigned char *dst);

int ngx_stream_lua_ffi_sha1_bin(const unsigned char *src, size_t len,
unsigned char *dst);
]]

ngx_lua_ffi_md5 = C.ngx_stream_lua_ffi_md5
ngx_lua_ffi_md5_bin = C.ngx_stream_lua_ffi_md5_bin
ngx_lua_ffi_sha1_bin = C.ngx_stream_lua_ffi_sha1_bin
end


local MD5_DIGEST_LEN = 16
Expand All @@ -35,7 +65,7 @@ ngx.md5_bin = function (s)
s = tostring(s)
end
end
C.ngx_http_lua_ffi_md5_bin(s, #s, md5_buf)
ngx_lua_ffi_md5_bin(s, #s, md5_buf)
return ffi_string(md5_buf, MD5_DIGEST_LEN)
end

Expand All @@ -51,7 +81,7 @@ ngx.md5 = function (s)
s = tostring(s)
end
end
C.ngx_http_lua_ffi_md5(s, #s, md5_hex_buf)
ngx_lua_ffi_md5(s, #s, md5_hex_buf)
return ffi_string(md5_hex_buf, MD5_HEX_DIGEST_LEN)
end

Expand All @@ -67,7 +97,7 @@ ngx.sha1_bin = function (s)
s = tostring(s)
end
end
local ok = C.ngx_http_lua_ffi_sha1_bin(s, #s, sha_buf)
local ok = ngx_lua_ffi_sha1_bin(s, #s, sha_buf)
if ok == 0 then
error("SHA-1 support missing in Nginx")
end
Expand Down

0 comments on commit cdb9616

Please sign in to comment.