Github user jpeach commented on a diff in the pull request: https://github.com/apache/trafficserver/pull/681#discussion_r65223795 --- Diff: plugins/experimental/ts_lua/ts_lua_crypto.c --- @@ -198,6 +222,130 @@ ts_lua_sha1_bin(lua_State *L) } 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) --- End diff -- Rather than duplicating all these hashing APIs, can we pass flags to a common implementation?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---