Comparison

util-src/hashes.c @ 9968:d536796a305f

util.hashes: Add PBKDF2-HMAC-SHA256
author Kim Alvefur <zash@zash.se>
date Sun, 13 Jan 2019 13:59:26 +0100
parent 9967:dc9bb31cbffe
child 9969:61bc5c52c941
comparison
equal deleted inserted replaced
9967:dc9bb31cbffe 9968:d536796a305f
114 lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH); 114 lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH);
115 115
116 return 1; 116 return 1;
117 } 117 }
118 118
119
120 static int Lpbkdf2_sha256(lua_State *L) {
121 unsigned char out[SHA256_DIGEST_LENGTH];
122
123 size_t pass_len, salt_len;
124 const char *pass = luaL_checklstring(L, 1, &pass_len);
125 const unsigned char *salt = (unsigned char *)luaL_checklstring(L, 2, &salt_len);
126 const int iter = luaL_checkinteger(L, 3);
127
128 if(PKCS5_PBKDF2_HMAC(pass, pass_len, salt, salt_len, iter, EVP_sha256(), SHA256_DIGEST_LENGTH, out) == 0) {
129 return luaL_error(L, "PKCS5_PBKDF2_HMAC() failed");
130 }
131
132 lua_pushlstring(L, (char *)out, SHA_DIGEST_LENGTH);
133 return 1;
134 }
135
119 static const luaL_Reg Reg[] = { 136 static const luaL_Reg Reg[] = {
120 { "sha1", Lsha1 }, 137 { "sha1", Lsha1 },
121 { "sha224", Lsha224 }, 138 { "sha224", Lsha224 },
122 { "sha256", Lsha256 }, 139 { "sha256", Lsha256 },
123 { "sha384", Lsha384 }, 140 { "sha384", Lsha384 },
127 { "hmac_sha256", Lhmac_sha256 }, 144 { "hmac_sha256", Lhmac_sha256 },
128 { "hmac_sha512", Lhmac_sha512 }, 145 { "hmac_sha512", Lhmac_sha512 },
129 { "hmac_md5", Lhmac_md5 }, 146 { "hmac_md5", Lhmac_md5 },
130 { "scram_Hi_sha1", Lpbkdf2_sha1 }, /* COMPAT */ 147 { "scram_Hi_sha1", Lpbkdf2_sha1 }, /* COMPAT */
131 { "pbkdf2_hmac_sha1", Lpbkdf2_sha1 }, 148 { "pbkdf2_hmac_sha1", Lpbkdf2_sha1 },
149 { "pbkdf2_hmac_sha256", Lpbkdf2_sha256 },
132 { NULL, NULL } 150 { NULL, NULL }
133 }; 151 };
134 152
135 LUALIB_API int luaopen_util_hashes(lua_State *L) { 153 LUALIB_API int luaopen_util_hashes(lua_State *L) {
136 #if (LUA_VERSION_NUM > 501) 154 #if (LUA_VERSION_NUM > 501)