Ver Fonte

add: 把crypto.hmac_md5的库实现加上, 但rtt实现未添加哦

Wendal Chen há 5 anos atrás
pai
commit
b26024ab8d
3 ficheiros alterados com 33 adições e 1 exclusões
  1. 3 0
      luat/include/luat_crypto.h
  2. 11 0
      luat/modules/luat_lib_crypto.c
  3. 19 1
      luat/rtt/luat_crypto_rtt.c

+ 3 - 0
luat/include/luat_crypto.h

@@ -3,3 +3,6 @@
 
 int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr);
 int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr);
+
+int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr);
+

+ 11 - 0
luat/modules/luat_lib_crypto.c

@@ -26,6 +26,17 @@ static int l_crypto_md5(lua_State *L) {
 }
 
 static int l_crypto_hmac_md5(lua_State *L) {
+    size_t str_size = 0;
+    size_t key_size = 0;
+    const char* str = luaL_checklstring(L, 1, &str_size);
+    const char* key = luaL_checklstring(L, 2, &key_size);
+    char tmp[32];
+    char dst[32];
+    if (luat_crypto_hmac_md5_simple(str, str_size, key, key_size, tmp) == 0) {
+        fixhex(tmp, dst, 16);
+        lua_pushlstring(L, dst, 32);
+        return 1;
+    }
     return 0;
 }
 

+ 19 - 1
luat/rtt/luat_crypto_rtt.c

@@ -20,8 +20,26 @@ int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr) {
     rt_hwcrypto_hash_destroy(ctx);
     return 0;
 }
+
+int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) {
+    return -1; // 未完成
+}
+
 #endif
 
-int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr);
+#ifdef RT_HWCRYPTO_USING_SHA1
+int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr) {
+    struct rt_hwcrypto_ctx *ctx;
+
+    ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA1);
+    if (ctx == NULL) {
+        return -1; // 内存爆了??
+    }
+    rt_hwcrypto_hash_update(ctx, str, str_size);
+    rt_hwcrypto_hash_finish(ctx, out_ptr, 32);
+    rt_hwcrypto_hash_destroy(ctx);
+    return 0;
+}
+#endif
 
 #endif