static int evp_digest(lua_State *L) { EVP_MD_CTX *c = evp_pget(L, 1); EVP_MD_CTX *d = NULL; unsigned char digest[EVP_MAX_MD_SIZE]; size_t written = 0; unsigned int i; char *hex; if (lua_isstring(L, 2)) { const char *s = luaL_checkstring(L, 2); EVP_DigestUpdate(c, s, lua_strlen(L, 2)); } d = EVP_MD_CTX_create(); EVP_MD_CTX_copy_ex(d, c); EVP_DigestFinal_ex(d, digest, &written); EVP_MD_CTX_destroy(d); if (lua_toboolean(L, 3)) lua_pushlstring(L, (char *)digest, written); else { hex = calloc(sizeof(char), written*2 + 1); for (i = 0; i < written; i++) sprintf(hex + 2*i, "%02x", digest[i]); lua_pushlstring(L, hex, written*2); free(hex); } return 1; }
static int evp_tostring(lua_State *L) { HANDLER_EVP *c = evp_pget(L, 1); char s[64]; sprintf(s, "%s %p", LUACRYPTO_EVPNAME, (void *)c); lua_pushstring(L, s); return 1; }
static int evp_clone(lua_State *L) { EVP_MD_CTX *c = evp_pget(L, 1); EVP_MD_CTX *d = evp_pnew(L); EVP_MD_CTX_init(d); EVP_MD_CTX_copy_ex(d, c); return 1; }
static int evp_reset(lua_State *L) { EVP_MD_CTX *c = evp_pget(L, 1); const EVP_MD *t = EVP_MD_CTX_md(c); EVP_MD_CTX_cleanup(c); EVP_MD_CTX_init(c); EVP_DigestInit_ex(c, t, NULL); return 0; }
static int evp_update(lua_State *L) { EVP_MD_CTX *c = evp_pget(L, 1); const char *s = luaL_checkstring(L, 2); EVP_DigestUpdate(c, s, lua_strlen(L, 2)); lua_settop(L, 1); return 1; }
static int evp_update(lua_State *L) { HANDLER_EVP *c = evp_pget(L, 1); size_t s_len; const char *s = luaL_checklstring(L, 2, &s_len); EVP_UPDATE(c, s, s_len); lua_settop(L, 1); return 1; }
static int evp_clone(lua_State *L) { HANDLER_EVP *c = evp_pget(L, 1); HANDLER_EVP *d = evp_pnew(L); #if CRYPTO_OPENSSL EVP_MD_CTX_init(d); EVP_MD_CTX_copy_ex(d, c); #elif CRYPTO_GCRYPT gcry_md_copy(d, *c); #endif return 1; }
static int evp_reset(lua_State *L) { HANDLER_EVP *c = evp_pget(L, 1); #if CRYPTO_OPENSSL const EVP_MD *t = EVP_MD_CTX_md(c); EVP_MD_CTX_cleanup(c); EVP_MD_CTX_init(c); EVP_DigestInit_ex(c, t, NULL); #elif CRYPTO_GCRYPT gcry_md_reset(*c); #endif return 0; }
static int evp_digest(lua_State *L) { HANDLER_EVP *c = evp_pget(L, 1); #if CRYPTO_OPENSSL HANDLER_EVP *d = NULL; unsigned char digest[EVP_MAX_MD_SIZE]; #elif CRYPTO_GCRYPT HANDLER_EVP d = NULL; unsigned char *digest; int algo; #endif size_t written = 0; if (lua_isstring(L, 2)) { size_t s_len; const char *s = luaL_checklstring(L, 2, &s_len); EVP_UPDATE(c, s, s_len); } #if CRYPTO_OPENSSL d = EVP_MD_CTX_create(); EVP_MD_CTX_copy_ex(d, c); EVP_DigestFinal_ex(d, digest, &written); EVP_MD_CTX_destroy(d); #elif CRYPTO_GCRYPT algo = gcry_md_get_algo(*c); gcry_md_copy(&d, *c); gcry_md_final(d); digest = gcry_md_read(d, algo); written = gcry_md_get_algo_dlen(algo); #endif if (lua_toboolean(L, 3)) lua_pushlstring(L, (char *)digest, written); else { char *hex = bin2hex(digest, written); lua_pushlstring(L, hex, written*2); free(hex); } #if CRYPTO_GCRYPT gcry_md_close(d); #endif return 1; }
static int evp_gc(lua_State *L) { HANDLER_EVP *c = evp_pget(L, 1); EVP_CLEANUP(c); return 1; }
static int evp_gc(lua_State *L) { EVP_MD_CTX *c = evp_pget(L, 1); EVP_MD_CTX_cleanup(c); return 1; }