Move all JS interface things into .js.c files with _register() functions.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3671 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
42
src/bcrypt.js.c
Normal file
42
src/bcrypt.js.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include "bcrypt.js.h"
|
||||
|
||||
#include "ow-crypt.h"
|
||||
|
||||
#include <sys/random.h>
|
||||
|
||||
JSValue _crypt_hashpw(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
||||
JSValue _crypt_gensalt(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
|
||||
|
||||
void tf_bcrypt_register(JSContext* context)
|
||||
{
|
||||
JSValue global = JS_GetGlobalObject(context);
|
||||
JSValue bcrypt = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, global, "bCrypt", bcrypt);
|
||||
JS_SetPropertyStr(context, bcrypt, "hashpw", JS_NewCFunction(context, _crypt_hashpw, "hashpw", 2));
|
||||
JS_SetPropertyStr(context, bcrypt, "gensalt", JS_NewCFunction(context, _crypt_gensalt, "gensalt", 0));
|
||||
JS_FreeValue(context, global);
|
||||
}
|
||||
|
||||
JSValue _crypt_hashpw(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
const char* key = JS_ToCString(context, argv[0]);
|
||||
const char* salt = JS_ToCString(context, argv[1]);
|
||||
char output[7 + 22 + 31 + 1];
|
||||
char* hash = crypt_rn(key, salt, output, sizeof(output));
|
||||
JSValue result = JS_NewString(context, hash);
|
||||
JS_FreeCString(context, key);
|
||||
JS_FreeCString(context, salt);
|
||||
return result;
|
||||
}
|
||||
|
||||
JSValue _crypt_gensalt(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
int length;
|
||||
JS_ToInt32(context, &length, argv[0]);
|
||||
char buffer[16];
|
||||
ssize_t bytes = getrandom(buffer, sizeof(buffer), 0);
|
||||
char output[7 + 22 + 1];
|
||||
char* salt = crypt_gensalt_rn("$2b$", length, buffer, bytes, output, sizeof(output));
|
||||
JSValue result = JS_NewString(context, salt);
|
||||
return result;
|
||||
}
|
Reference in New Issue
Block a user