Jelajahi Sumber

update:流式hash接口定义修改

doudou 2 tahun lalu
induk
melakukan
ce6779776e

+ 3 - 3
components/crypto/luat_crypto_mbedtls.c

@@ -257,7 +257,7 @@ int luat_crypto_md_file(const char* md, void* out_ptr, const char* key, size_t k
     return ret;
 }
 
-int luat_crypto_md_stream_init(const char* md, const char* key, luat_crypt_stream_t *stream) {
+int luat_crypto_md_init(const char* md, const char* key, luat_crypt_stream_t *stream) {
     const mbedtls_md_info_t * info = mbedtls_md_info_from_string(md);
     if (info == NULL) {
         return -1;
@@ -274,7 +274,7 @@ int luat_crypto_md_stream_init(const char* md, const char* key, luat_crypt_strea
     return 0;
 }
 
-int luat_crypto_md_stream_update(const char * md, const char* str, size_t str_size, luat_crypt_stream_t *stream) {
+int luat_crypto_md_update(const char * md, const char* str, size_t str_size, luat_crypt_stream_t *stream) {
     const mbedtls_md_info_t * info = mbedtls_md_info_from_string(md);
     if (info == NULL) {
         return -1;
@@ -288,7 +288,7 @@ int luat_crypto_md_stream_update(const char * md, const char* str, size_t str_si
     return 0;
 }
 
-int luat_crypto_md_stream_finish(const char* md, void* out_ptr, luat_crypt_stream_t *stream) {
+int luat_crypto_md_finish(const char* md, void* out_ptr, luat_crypt_stream_t *stream) {
     const mbedtls_md_info_t * info = mbedtls_md_info_from_string(md);
     if (info == NULL) {
         return -1;

+ 4 - 3
luat/include/luat_crypto.h

@@ -15,6 +15,7 @@
 
 typedef struct
 {
+    char tp[16];
     size_t key_len;
 	mbedtls_md_context_t *ctx;
 }luat_crypt_stream_t;
@@ -39,7 +40,7 @@ int luat_crypto_cipher_suites(const char** list, size_t* len);
 int luat_crypto_md(const char* md, const char* str, size_t str_size, void* out_ptr, const char* key, size_t key_len);
 int luat_crypto_md_file(const char* md, void* out_ptr, const char* key, size_t key_len, const char* path);
 
-int luat_crypto_md_stream_init(const char* md, const char* key, luat_crypt_stream_t *stream);
-int luat_crypto_md_stream_update(const char* md, const char* str, size_t str_size, luat_crypt_stream_t *stream);
-int luat_crypto_md_stream_finish(const char* md, void* out_ptr, luat_crypt_stream_t *stream);
+int luat_crypto_md_init(const char* md, const char* key, luat_crypt_stream_t *stream);
+int luat_crypto_md_update(const char* md, const char* str, size_t str_size, luat_crypt_stream_t *stream);
+int luat_crypto_md_finish(const char* md, void* out_ptr, luat_crypt_stream_t *stream);
 #endif

+ 27 - 30
luat/modules/luat_lib_crypto.c

@@ -666,23 +666,23 @@ static int l_crypto_md(lua_State *L) {
 
 /*
 创建流式hash用的stream
-@api crypto.hash_stream_init(tp)
+@api crypto.hash_init(tp)
 @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"))
+log.info("md5", crypto.hash_init("MD5"))
+log.info("sha1", crypto.hash_init("SHA1"))
+log.info("sha256", crypto.hash_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()
+log.info("hmac_md5", crypto.hash_init("MD5", "123456"))
+log.info("hmac_sha1", crypto.hash_init("SHA1", "123456"))
+log.info("hmac_sha256", crypto.hash_init("SHA256", "123456"))
+local stream = crypto.hash_init()
 */
-static int l_crypt_hash_stream_init(lua_State *L) {
+static int l_crypt_hash_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);
@@ -690,10 +690,11 @@ static int l_crypt_hash_stream_init(lua_State *L) {
         memset(stream, 0x00, sizeof(luat_crypt_stream_t));
         const char* key = NULL;
         const char* md = luaL_checkstring(L, 1);
+        strncpy(stream->tp, md, strlen(md));
         if(lua_type(L, 2) == LUA_TSTRING) {
             key = luaL_checklstring(L, 3, &(stream->key_len));
         }
-        int ret = luat_crypto_md_stream_init(md, key, stream);
+        int ret = luat_crypto_md_init(md, key, stream);
         if (ret < 0) {
             lua_pushnil(L);
         } else {
@@ -705,38 +706,34 @@ static int l_crypt_hash_stream_init(lua_State *L) {
 
 /*
 流式hash更新数据
-@api crypto.hash_stream_update(tp, stream, data)
-@string hash类型, 大写字母, 例如 "MD5" "SHA1" "SHA256"
-@userdata crypto.hash_stream_init()创建的stream, 必选
+@api crypto.hash_update(stream, data)
+@userdata crypto.hash_init()创建的stream, 必选
 @string 待计算的数据,必选
 @return 无
 @usage
-crypto.hash_stream_update("MD5", stream, "OK")
+crypto.hash_update(stream, "OK")
 */
-static int l_crypt_hash_stream_update(lua_State *L) {
-    const char* md = luaL_checkstring(L, 1);
-    luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 2, LUAT_CRYPTO_TYPE);
+static int l_crypt_hash_update(lua_State *L) {
+    luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 1, LUAT_CRYPTO_TYPE);
     size_t data_len = 0;
-    const char *data = luaL_checklstring(L, 3, &data_len);
-    luat_crypto_md_stream_update(md, data, data_len ,stream);
+    const char *data = luaL_checklstring(L, 2, &data_len);
+    luat_crypto_md_update(stream->tp, data, data_len ,stream);
     return 0;
 }
 
 /*
 获取流式hash校验值并释放创建的stream
-@api crypto.l_crypt_hash_stream_finish(tp, stream)
-@string hash类型, 大写字母, 例如 "MD5" "SHA1" "SHA256"
-@userdata crypto.hash_stream_init()创建的stream,必选
+@api crypto.l_crypt_hash_finish(stream)
+@userdata crypto.hash_init()创建的stream,必选
 @return string 成功返回计算得出的流式hash值的hex字符串,失败无返回
 @usage
-local hashResult = crypto.hash_stream_finish("MD5", stream)
+local hashResult = crypto.hash_finish("MD5", stream)
 */
-static int l_crypt_hash_stream_finish(lua_State *L) {
-    const char* md = luaL_checkstring(L, 1);
-    luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 2, LUAT_CRYPTO_TYPE);
+static int l_crypt_hash_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(md, output, stream);
+    int ret = luat_crypto_md_finish(stream->tp, output, stream);
     LLOGD("finish result %d", ret);
     if (ret < 1) {
         return 0;
@@ -810,9 +807,9 @@ 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)},
+    { "hash_init",      ROREG_FUNC(l_crypt_hash_init)},
+    { "hash_update",    ROREG_FUNC(l_crypt_hash_update)},
+    { "hash_finish",    ROREG_FUNC(l_crypt_hash_finish)},
 
 	{ NULL,             ROREG_INT(0) }
 };