-
Notifications
You must be signed in to change notification settings - Fork 857
TS-4491: support sha256 and sha512 in ts_lua #681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,13 +23,21 @@ | |
|
|
||
| #define TS_LUA_MD5_DIGEST_LENGTH 16 | ||
| #define TS_LUA_SHA_DIGEST_LENGTH 20 | ||
| #define TS_LUA_SHA256_DIGEST_LENGTH 32 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OpenSSL already has
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should change the other constants as well to use openssl. |
||
| #define TS_LUA_SHA512_DIGEST_LENGTH 64 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OpenSSL already has
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
|
|
||
| static int ts_lua_md5(lua_State *L); | ||
| static int ts_lua_md5_bin(lua_State *L); | ||
|
|
||
| static int ts_lua_sha1(lua_State *L); | ||
| static int ts_lua_sha1_bin(lua_State *L); | ||
|
|
||
| static int ts_lua_sha256(lua_State *L); | ||
| static int ts_lua_sha256_bin(lua_State *L); | ||
|
|
||
| static int ts_lua_sha512(lua_State *L); | ||
| static int ts_lua_sha512_bin(lua_State *L); | ||
|
|
||
| static int ts_lua_base64_encode(lua_State *L); | ||
| static int ts_lua_base64_decode(lua_State *L); | ||
|
|
||
|
|
@@ -47,14 +55,30 @@ ts_lua_inject_crypto_api(lua_State *L) | |
| lua_pushcfunction(L, ts_lua_md5_bin); | ||
| lua_setfield(L, -2, "md5_bin"); | ||
|
|
||
| /* ts.sha1_bin(...) */ | ||
| /* ts.sha1(...) */ | ||
| lua_pushcfunction(L, ts_lua_sha1); | ||
| lua_setfield(L, -2, "sha1"); | ||
|
|
||
| /* ts.sha1_bin(...) */ | ||
| lua_pushcfunction(L, ts_lua_sha1_bin); | ||
| lua_setfield(L, -2, "sha1_bin"); | ||
|
|
||
| /* ts.sha256(...) */ | ||
| lua_pushcfunction(L, ts_lua_sha256); | ||
| lua_setfield(L, -2, "sha256"); | ||
|
|
||
| /* ts.sha256_bin(...) */ | ||
| lua_pushcfunction(L, ts_lua_sha256_bin); | ||
| lua_setfield(L, -2, "sha256_bin"); | ||
|
|
||
| /* ts.sha512(...) */ | ||
| lua_pushcfunction(L, ts_lua_sha512); | ||
| lua_setfield(L, -2, "sha512"); | ||
|
|
||
| /* ts.sha512_bin(...) */ | ||
| lua_pushcfunction(L, ts_lua_sha512_bin); | ||
| lua_setfield(L, -2, "sha512_bin"); | ||
|
|
||
| /* ts.base64_encode(...) */ | ||
| lua_pushcfunction(L, ts_lua_base64_encode); | ||
| lua_setfield(L, -2, "base64_encode"); | ||
|
|
@@ -197,6 +221,130 @@ ts_lua_sha1_bin(lua_State *L) | |
| return 1; | ||
| } | ||
|
|
||
| static int | ||
| ts_lua_sha256(lua_State *L) | ||
| { | ||
| u_char *src; | ||
| size_t slen; | ||
|
|
||
| SHA256_CTX sha; | ||
| u_char sha_buf[TS_LUA_SHA256_DIGEST_LENGTH]; | ||
| u_char hex_buf[2 * sizeof(sha_buf)]; | ||
|
|
||
| if (lua_gettop(L) != 1) { | ||
| return luaL_error(L, "expecting one argument"); | ||
| } | ||
|
|
||
| if (lua_isnil(L, 1)) { | ||
| src = (u_char *)""; | ||
| slen = 0; | ||
|
|
||
| } else { | ||
| src = (u_char *)luaL_checklstring(L, 1, &slen); | ||
| } | ||
|
|
||
| SHA256_Init(&sha); | ||
| SHA256_Update(&sha, src, slen); | ||
| SHA256_Final(sha_buf, &sha); | ||
|
|
||
| ts_lua_hex_dump(hex_buf, sha_buf, sizeof(sha_buf)); | ||
| lua_pushlstring(L, (char *)hex_buf, sizeof(hex_buf)); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| static int | ||
| ts_lua_sha256_bin(lua_State *L) | ||
| { | ||
| u_char *src; | ||
| size_t slen; | ||
|
|
||
| SHA256_CTX sha; | ||
| u_char sha_buf[TS_LUA_SHA256_DIGEST_LENGTH]; | ||
|
|
||
| if (lua_gettop(L) != 1) { | ||
| return luaL_error(L, "expecting one argument"); | ||
| } | ||
|
|
||
| if (lua_isnil(L, 1)) { | ||
| src = (u_char *)""; | ||
| slen = 0; | ||
|
|
||
| } else { | ||
| src = (u_char *)luaL_checklstring(L, 1, &slen); | ||
| } | ||
|
|
||
| SHA256_Init(&sha); | ||
| SHA256_Update(&sha, src, slen); | ||
| SHA256_Final(sha_buf, &sha); | ||
|
|
||
| lua_pushlstring(L, (char *)sha_buf, sizeof(sha_buf)); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| static int | ||
| ts_lua_sha512(lua_State *L) | ||
| { | ||
| u_char *src; | ||
| size_t slen; | ||
|
|
||
| SHA512_CTX sha; | ||
| u_char sha_buf[TS_LUA_SHA512_DIGEST_LENGTH]; | ||
| u_char hex_buf[2 * sizeof(sha_buf)]; | ||
|
|
||
| if (lua_gettop(L) != 1) { | ||
| return luaL_error(L, "expecting one argument"); | ||
| } | ||
|
|
||
| if (lua_isnil(L, 1)) { | ||
| src = (u_char *)""; | ||
| slen = 0; | ||
|
|
||
| } else { | ||
| src = (u_char *)luaL_checklstring(L, 1, &slen); | ||
| } | ||
|
|
||
| SHA512_Init(&sha); | ||
| SHA512_Update(&sha, src, slen); | ||
| SHA512_Final(sha_buf, &sha); | ||
|
|
||
| ts_lua_hex_dump(hex_buf, sha_buf, sizeof(sha_buf)); | ||
| lua_pushlstring(L, (char *)hex_buf, sizeof(hex_buf)); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| static int | ||
| ts_lua_sha512_bin(lua_State *L) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than duplicating all these hashing APIs, can we pass flags to a common implementation? |
||
| { | ||
| u_char *src; | ||
| size_t slen; | ||
|
|
||
| SHA512_CTX sha; | ||
| u_char sha_buf[TS_LUA_SHA512_DIGEST_LENGTH]; | ||
|
|
||
| if (lua_gettop(L) != 1) { | ||
| return luaL_error(L, "expecting one argument"); | ||
| } | ||
|
|
||
| if (lua_isnil(L, 1)) { | ||
| src = (u_char *)""; | ||
| slen = 0; | ||
|
|
||
| } else { | ||
| src = (u_char *)luaL_checklstring(L, 1, &slen); | ||
| } | ||
|
|
||
| SHA512_Init(&sha); | ||
| SHA512_Update(&sha, src, slen); | ||
| SHA512_Final(sha_buf, &sha); | ||
|
|
||
| lua_pushlstring(L, (char *)sha_buf, sizeof(sha_buf)); | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| static int | ||
| ts_lua_base64_encode(lua_State *L) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a better syntax might be to explicitly pass a parameter to specify the result format, eg,
RAW,HEX,BASE64.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can add to that. And to keep backward compatibility, make those other functions to be alias to that.