PHP Password generator extension
18/09/2011
Και το αντίστοιχο password generator σε php extension για να υπάρχει παντού διαθέσιμο χωρίς να χρειάζεται includes και copy/paste, αλλά και να είναι λίγο ή πολύ πιο γρήγορο από την αντίστοιχη function.
Δημιουργούμε ένα νέο φάκελο passwd και εκεί μέσα δημιουργούμε τα εξής αρχεία:
config.m4
PHP_ARG_ENABLE(passwd, whether to enable passwd support, [ --enable-passwd Enable passwd support])
if test "$PHP_PASSWD" = "yes"; then
AC_DEFINE(HAVE_PASSWD, 1, [Whether you have passwd])
PHP_NEW_EXTENSION(passwd, passwd.c, $ext_shared)
fi
INC_CHECK_DIRS="/usr /usr/local"
for i in $INC_CHECK_DIRS ; do
if test -f $i/include/php/ext/standard/php_rand.h; then
PHP_ADD_INCLUDE($i/include/php/ext/standard)
break
fi
done
passwd.h
#ifndef __PASSWD_H__ #define __PASSWD_H__ 1 #ifndef _WIN32 #include #endif #include #ifdef ZTS #include "TSRM.h" #endif #ifndef BOOL #define BOOL short #endif //BOOL #ifndef FALSE #define FALSE (0) #endif #ifndef TRUE #define TRUE (1) #endif #define PHP_PASSWD_WORLD_VERSION "1.0.0" #define PHP_PASSWD_WORLD_EXTNAME "passwd" PHP_FUNCTION(passwd_version); PHP_FUNCTION(passwd_create); PHP_MINIT_FUNCTION(passwd); PHP_MINFO_FUNCTION(passwd); extern zend_module_entry passwd_module_entry; #define phpext_passwd_ptr &passwd_module_entry #endif //__PASSWD_H__
passwd.c
#include "php.h"
#include "php_rand.h"
#ifdef _WIN32
#include "ext/standard/info.h"
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "passwd.h"
#ifndef LONG
typedef long LONG;
#endif
static function_entry passwd_functions[] = {
PHP_FE(passwd_version, NULL)
PHP_FE(passwd_create, NULL)
{NULL, NULL, NULL}
};
zend_module_entry passwd_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_PASSWD_WORLD_EXTNAME,
passwd_functions,
PHP_MINIT(passwd),
NULL,
NULL,
NULL,
PHP_MINFO(passwd),
#if ZEND_MODULE_API_NO >= 20010901
PHP_PASSWD_WORLD_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_PASSWD
ZEND_GET_MODULE(passwd)
#endif
char* passwordChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()-_=+[]{}\\|,<.>/?~;:";
int passwordCharsLen = 0;
static void ZEND_MODULE_GLOBALS_CTOR_N(passwd)(void *passwd_globals TSRMLS_DC)
{
//php_srand( time(NULL) );
}
PHP_MINIT_FUNCTION(passwd)
{
passwordCharsLen = strlen(passwordChars);
return SUCCESS;
}
PHP_MINFO_FUNCTION(passwd)
{
php_info_print_table_start();
php_info_print_table_row(2, "passwd", "passwd Functions");
php_info_print_table_row(2, "passwd_create", "return a random password string");
php_info_print_table_end();
}
PHP_FUNCTION(passwd_version)
{
RETURN_STRING(PHP_PASSWD_WORLD_VERSION, 1);
}
PHP_FUNCTION(passwd_create)
{
long i, llen, rnd_idx;
char* tmp;
if (ZEND_NUM_ARGS() != 1) {
WRONG_PARAM_COUNT;
}
llen = 8;
if ((zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &llen) == FAILURE) ||
(llen < 4) || (llen > 128))
{
RETURN_STRING("len should be an integer between 4 and 128", 1);
}
tmp = emalloc(llen+1);
for (i=0; i<llen; i++)
{
rnd_idx = php_rand(TSRMLS_C);
RAND_RANGE(rnd_idx, 0, passwordCharsLen-1, PHP_RAND_MAX);
tmp[i] = passwordChars[rnd_idx];
}
tmp[i]=(char)0;
RETURN_STRING(tmp, 0);
}
Τώρα o αντίστοιχος κώδικας του σχετικού post θα μπορούσε να γίνει ως εξής:
<?php
header('Content-type: text/plain');
if (isset($_GET['len'])) $len = (int)$_GET['len'];
if (($len < 4) || ($len > 128)) $len = 8;
echo passwd_create($len);
?>
No comments yet