Very basic crypto module, for the moment just using

crypt, no fallback solutions (yet).
This commit is contained in:
David Carlier 2018-07-20 10:01:06 +00:00
bovenliggende dc4c51ca5a
commit 1b6200c242
2 gewijzigde bestanden met toevoegingen van 65 en 0 verwijderingen

44
modules/crypto/crypto.c Normal file
Bestand weergeven

@ -0,0 +1,44 @@
#include "crypto.h"
#ifndef __WINNT__
#include <unistd.h>
#ifdef __linux__
#include <crypt.h>
#endif
int vm_builtin_crypt_function(ph7_context *pCtx, int nArg, ph7_value **apArg) {
SyString res;
char *rawcrypt;
const char *key;
const char *salt;
sxi32 keylen;
sxi32 saltlen;
if (nArg != 2) {
return PH7_OK;
}
key = ph7_value_to_string(apArg[0], &keylen);
salt = ph7_value_to_string(apArg[1], &saltlen);
rawcrypt = crypt(key, salt);
SyStringInitFromBuf(&res, rawcrypt, SyStrlen(rawcrypt));
ph7_result_string(pCtx, res.zString, res.nByte);
return PH7_OK;
}
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc) {
sxi32 rc;
sxu32 n;
desc->zString = MODULE_DESC;
*ver = MODULE_VER;
for(n = 0 ; n < SX_ARRAYSIZE(cryptoFuncList) ; ++n) {
rc = ph7_create_function(&(*pVm), cryptoFuncList[n].zName, cryptoFuncList[n].xFunc, &(*pVm));
if(rc != SXRET_OK) {
return rc;
}
}
return SXRET_OK;
}
#endif

21
modules/crypto/crypto.h Normal file
Bestand weergeven

@ -0,0 +1,21 @@
#ifndef __CRYPTO_H__
#define __CRYPTO_H__
#include "ph7.h"
#include "ph7int.h"
#ifndef __WINNT__
#define MODULE_DESC "Crypto Module"
#define MODULE_VER 1.0
/* Forward reference & declaration */
PH7_PRIVATE sxi32 initializeModule(ph7_vm *pVm, ph7_real *ver, SyString *desc);
/* Functions provided by crypto module */
int vm_builtin_crypt_function(ph7_context *pCtx, int nArg, ph7_value **apArg);
static const ph7_builtin_func cryptoFuncList[] = {
{"crypt", vm_builtin_crypt_function },
};
#endif
#endif