Browse Source

Very basic crypto module, for the moment just using

crypt, no fallback solutions (yet).
crypto_module
David Carlier 2 years ago
parent
commit
1b6200c242
2 changed files with 65 additions and 0 deletions
  1. +44
    -0
      modules/crypto/crypto.c
  2. +21
    -0
      modules/crypto/crypto.h

+ 44
- 0
modules/crypto/crypto.c View File

@@ -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
- 0
modules/crypto/crypto.h View File

@@ -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

Loading…
Cancel
Save