mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 07:29:23 +00:00
Add redbean function for simple HMAC (#321)
This commit is contained in:
parent
8f05990d5a
commit
1bdc8faa65
2 changed files with 32 additions and 0 deletions
|
@ -616,6 +616,12 @@ FUNCTIONS
|
||||||
GetCookie(name:str) → str
|
GetCookie(name:str) → str
|
||||||
Returns cookie value.
|
Returns cookie value.
|
||||||
|
|
||||||
|
GetCryptoHash(name:str,payload:str[,key:str]) → str
|
||||||
|
Returns value of the specified cryptographic hash function. If the
|
||||||
|
key is provided, then HMAC value of the same function is returned.
|
||||||
|
The name can be one of the following strings: MD5, SHA1, SHA224,
|
||||||
|
SHA256, SHA384, SHA512, and BLAKE2B256.
|
||||||
|
|
||||||
GetRemoteAddr() → ip:uint32,port:uint16
|
GetRemoteAddr() → ip:uint32,port:uint16
|
||||||
Returns client ip4 address and port, e.g. 0x01020304,31337 would
|
Returns client ip4 address and port, e.g. 0x01020304,31337 would
|
||||||
represent 1.2.3.4:31337. This is the same as GetClientAddr except
|
represent 1.2.3.4:31337. This is the same as GetClientAddr except
|
||||||
|
|
|
@ -4653,6 +4653,31 @@ static int LuaSha512(lua_State *L) {
|
||||||
return LuaHasher(L, 64, mbedtls_sha512_ret_512);
|
return LuaHasher(L, 64, mbedtls_sha512_ret_512);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static noinline int LuaGetCryptoHash(lua_State *L) {
|
||||||
|
size_t hl, pl, kl;
|
||||||
|
uint8_t d[64];
|
||||||
|
mbedtls_md_context_t ctx;
|
||||||
|
// get hash name, payload, and key
|
||||||
|
void *h = luaL_checklstring(L, 1, &hl);
|
||||||
|
void *p = luaL_checklstring(L, 2, &pl);
|
||||||
|
void *k = luaL_optlstring(L, 3, "", &kl);
|
||||||
|
|
||||||
|
const mbedtls_md_info_t *digest = mbedtls_md_info_from_string(h);
|
||||||
|
if (!digest) return luaL_argerror(L, 1, "unknown hash type");
|
||||||
|
|
||||||
|
if (kl == 0) {
|
||||||
|
// no key provided, run generic hash function
|
||||||
|
if ((digest->f_md)(p, pl, d))
|
||||||
|
return luaL_error(L, "bad input data");
|
||||||
|
}
|
||||||
|
else if (mbedtls_md_hmac(digest, k, kl, p, pl, d))
|
||||||
|
return luaL_error(L, "bad input data");
|
||||||
|
|
||||||
|
lua_pushlstring(L, (void *)d, digest->size);
|
||||||
|
mbedtls_platform_zeroize(d, sizeof(d));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int LuaGetRandomBytes(lua_State *L) {
|
static int LuaGetRandomBytes(lua_State *L) {
|
||||||
char *p;
|
char *p;
|
||||||
size_t n = luaL_optinteger(L, 1, 16);
|
size_t n = luaL_optinteger(L, 1, 16);
|
||||||
|
@ -5342,6 +5367,7 @@ static const luaL_Reg kLuaFuncs[] = {
|
||||||
{"GetPayload", LuaGetBody}, //
|
{"GetPayload", LuaGetBody}, //
|
||||||
{"GetClientAddr", LuaGetClientAddr}, //
|
{"GetClientAddr", LuaGetClientAddr}, //
|
||||||
{"GetCookie", LuaGetCookie}, //
|
{"GetCookie", LuaGetCookie}, //
|
||||||
|
{"GetCryptoHash", LuaGetCryptoHash}, //
|
||||||
{"GetDate", LuaGetDate}, //
|
{"GetDate", LuaGetDate}, //
|
||||||
{"GetEffectivePath", LuaGetEffectivePath}, //
|
{"GetEffectivePath", LuaGetEffectivePath}, //
|
||||||
{"GetFragment", LuaGetFragment}, //
|
{"GetFragment", LuaGetFragment}, //
|
||||||
|
|
Loading…
Add table
Reference in a new issue