|
|
@@ -13,8 +13,10 @@
|
|
|
#include "luat_str.h"
|
|
|
#include <time.h>
|
|
|
#include "luat_zbuff.h"
|
|
|
+#include "mbedtls/md.h"
|
|
|
|
|
|
#define LUAT_LOG_TAG "crypto"
|
|
|
+#define LUAT_CRYPTO_TYPE "crypto"
|
|
|
#include "luat_log.h"
|
|
|
|
|
|
static const unsigned char hexchars[] = "0123456789ABCDEF";
|
|
|
@@ -662,6 +664,86 @@ static int l_crypto_md(lua_State *L) {
|
|
|
return 1;
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+创建流式hash用的stream
|
|
|
+@api crypto.hash_stream_init()
|
|
|
+@string hash类型, 大写字母, 例如 "MD5" "SHA1" "SHA256"
|
|
|
+@string hmac值,可选
|
|
|
+@return userdata 成功返回一个数据结构,否则返回nil
|
|
|
+@usage
|
|
|
+-- 无hmac的hash stream
|
|
|
+log.info("md5", crypto.hash_stream_init("MD5"))
|
|
|
+log.info("sha1", crypto.hash_stream_init("SHA1"))
|
|
|
+log.info("sha256", crypto.hash_stream_init("SHA256"))
|
|
|
+
|
|
|
+-- 带hmac的hash stream
|
|
|
+log.info("hmac_md5", crypto.hash_stream_init("MD5", "123456"))
|
|
|
+log.info("hmac_sha1", crypto.hash_stream_init("SHA1", "123456"))
|
|
|
+log.info("hmac_sha256", crypto.hash_stream_init("SHA256", "123456"))
|
|
|
+local stream = crypto.hash_stream_init()
|
|
|
+*/
|
|
|
+static int l_crypt_hash_stream_init(lua_State *L) {
|
|
|
+ luat_crypt_stream_t *stream = (luat_crypt_stream_t *)lua_newuserdata(L, sizeof(luat_crypt_stream_t));
|
|
|
+ if(stream == NULL) {
|
|
|
+ lua_pushnil(L);
|
|
|
+ } else {
|
|
|
+ memset(stream, 0x00, sizeof(luat_crypt_stream_t));
|
|
|
+ const char* key = NULL;
|
|
|
+ const char* md = luaL_checkstring(L, 1);
|
|
|
+ if(lua_type(L, 2) == LUA_TSTRING) {
|
|
|
+ key = luaL_checklstring(L, 2, &(stream->key_len));
|
|
|
+ }
|
|
|
+ int ret = luat_crypto_md_stream_init(md, key, stream);
|
|
|
+ if (ret < 0) {
|
|
|
+ lua_pushnil(L);
|
|
|
+ } else {
|
|
|
+ luaL_setmetatable(L, LUAT_CRYPTO_TYPE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+流式hash更新数据
|
|
|
+@api crypto.hash_stream_update(stream, data)
|
|
|
+@userdata crypto.hash_stream_init()创建的stream, 必选
|
|
|
+@string 待计算的数据,必选
|
|
|
+@return 无
|
|
|
+@usage
|
|
|
+crypto.hash_stream_update(stream, "OK")
|
|
|
+*/
|
|
|
+static int l_crypt_hash_stream_update(lua_State *L) {
|
|
|
+ luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 1, LUAT_CRYPTO_TYPE);
|
|
|
+ const char *data = NULL;
|
|
|
+ size_t data_len = 0;
|
|
|
+ data = luaL_checklstring(L, 2, &data_len);
|
|
|
+ luat_crypto_md_stream_update(data, data_len ,stream);
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+获取流式hash校验值并释放创建的stream
|
|
|
+@api crypto.l_crypt_hash_stream_finish(stream)
|
|
|
+@userdata crypto.hash_stream_init()创建的stream,必选
|
|
|
+@return string 成功返回计算得出的流式hash值的hex字符串,失败无返回
|
|
|
+@usage
|
|
|
+local hashResult = crypto.hash_stream_finish(stream)
|
|
|
+*/
|
|
|
+static int l_crypt_hash_stream_finish(lua_State *L) {
|
|
|
+ luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 1, LUAT_CRYPTO_TYPE);
|
|
|
+ char buff[128] = {0};
|
|
|
+ char output[64];
|
|
|
+ int ret = luat_crypto_md_stream_finish(output, stream);
|
|
|
+ LLOGD("finish result %d", ret);
|
|
|
+ if (ret < 1) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ fixhex(output, buff, ret);
|
|
|
+ lua_pushlstring(L, buff, ret * 2);
|
|
|
+ return 1;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
/*
|
|
|
计算checksum校验和
|
|
|
@api crypto.checksum(data, mode)
|
|
|
@@ -725,12 +807,17 @@ static const rotable_Reg_t reg_crypto[] =
|
|
|
{ "md_file", ROREG_FUNC(l_crypto_md_file)},
|
|
|
{ "md", ROREG_FUNC(l_crypto_md)},
|
|
|
{ "checksum", ROREG_FUNC(l_crypt_checksum)},
|
|
|
+ { "hash_stream_init",ROREG_FUNC(l_crypt_hash_stream_init)},
|
|
|
+ { "hash_stream_update",ROREG_FUNC(l_crypt_hash_stream_update)},
|
|
|
+ { "hash_stream_finish",ROREG_FUNC(l_crypt_hash_stream_finish)},
|
|
|
|
|
|
{ NULL, ROREG_INT(0) }
|
|
|
};
|
|
|
|
|
|
LUAMOD_API int luaopen_crypto( lua_State *L ) {
|
|
|
luat_newlib2(L, reg_crypto);
|
|
|
+ luaL_newmetatable(L, LUAT_CRYPTO_TYPE);
|
|
|
+ lua_pop(L, 1);
|
|
|
return 1;
|
|
|
}
|
|
|
|