PHP hash
-Sara Golemon, Rasmus Lerdorf, Stefan Esser, Michael Wallner
+Sara Golemon, Rasmus Lerdorf, Stefan Esser, Michael Wallner, Scott MacVicar
PHP_ARG_ENABLE(hash, whether to enable hash support,
[ --disable-hash Disable hash support], yes)
+if test "$PHP_MHASH" != "no"; then
+ if test "$PHP_HASH" == "no"; then
+ PHP_HASH="yes"
+ fi
+
+ AC_DEFINE(PHP_MHASH_BC, 1, [ ])
+fi
+
if test "$PHP_HASH" != "no"; then
AC_DEFINE(HAVE_HASH_EXT,1,[Have HASH Extension])
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Sara Golemon <pollita@php.net> |
+ | Scott MacVicar <scottmac@php.net> |
+----------------------------------------------------------------------+
*/
# define DEFAULT_CONTEXT NULL
#endif
+#ifdef PHP_MHASH_BC
+struct mhash_bc_entry {
+ char *mhash_name;
+ char *hash_name;
+ int value;
+};
+
+#define MHASH_NUM_ALGOS 29
+
+static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
+ {"CRC32", "crc32", 0},
+ {"MD5", "md5", 1},
+ {"SHA1", "sha1", 2},
+ {"HAVAL256", "haval256,3", 3},
+ {NULL, NULL, 4},
+ {"RIPEMD160", "ripemd160", 5},
+ {NULL, NULL, 6},
+ {"TIGER", "tiger192,3", 7},
+ {"GOST", "gost", 8},
+ {"CRC32B", "crc32b", 9},
+ {"HAVAL224", "haval224,3", 10},
+ {"HAVAL192", "haval192,3", 11},
+ {"HAVAL160", "haval160,3", 12},
+ {"HAVAL128", "haval128,3", 13},
+ {"TIGER128", "tiger128,3", 14},
+ {"TIGER160", "tiger160,3", 15},
+ {"MD4", "md4", 16},
+ {"SHA256", "sha256", 17},
+ {"ADLER32", "adler32", 18},
+ {"SHA224", "sha224", 19},
+ {"SHA512", "sha512", 20},
+ {"SHA384", "sha384", 21},
+ {"WHIRLPOOL", "whirlpool", 22},
+ {"RIPEMD128", "ripemd128", 23},
+ {"RIPEMD256", "ripemd256", 24},
+ {"RIPEMD320", "ripemd320", 25},
+ {NULL, NULL, 26}, /* support needs to be added for snefru 128 */
+ {"SNEFRU256", "snefru256", 27},
+ {"MD2", "md2", 28}
+};
+#endif
+
/* Hash Registry Access */
PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, int algo_len) /* {{{ */
/* Userspace */
-static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename) /* {{{ */
+static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
{
char *algo, *data, *digest;
int algo_len, data_len;
- zend_bool raw_output = 0;
+ zend_bool raw_output = raw_output_default;
const php_hash_ops *ops;
void *context;
php_stream *stream = NULL;
Returns lowercase hexits by default */
PHP_FUNCTION(hash)
{
- php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
+ php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
}
/* }}} */
Returns lowercase hexits by default */
PHP_FUNCTION(hash_file)
{
- php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
+ php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
}
/* }}} */
-static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename) /* {{{ */
+static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
{
char *algo, *data, *digest, *key, *K;
int algo_len, data_len, key_len, i;
- zend_bool raw_output = 0;
+ zend_bool raw_output = raw_output_default;
const php_hash_ops *ops;
void *context;
php_stream *stream = NULL;
Returns lowercase hexits by default */
PHP_FUNCTION(hash_hmac)
{
- php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
+ php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
}
/* }}} */
Returns lowercase hexits by default */
PHP_FUNCTION(hash_hmac_file)
{
- php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
+ php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
}
/* }}} */
#define PHP_HASH_HAVAL_REGISTER(p,b) php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
+#ifdef PHP_MHASH_BC
+
+static void mhash_init(INIT_FUNC_ARGS)
+{
+ char buf[128];
+ int len;
+ int algo_number = 0;
+
+ for (algo_number = 0; algo_number < MHASH_NUM_ALGOS; algo_number++) {
+ struct mhash_bc_entry algorithm = mhash_to_hash[algo_number];
+ if (algorithm.mhash_name == NULL) {
+ continue;
+ }
+
+ len = slprintf(buf, 127, "MHASH_%s", algorithm.mhash_name, strlen(algorithm.mhash_name));
+ {
+ char name[len+1];
+ memcpy(name, buf, len+1);
+ REGISTER_LONG_CONSTANT(name, algorithm.value, CONST_CS | CONST_PERSISTENT);
+ }
+ }
+}
+
+PHP_FUNCTION(mhash)
+{
+ zval **z_algorithm;
+ int algorithm;
+
+ if (ZEND_NUM_ARGS() == 0 || zend_get_parameters_ex(1, &z_algorithm) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+
+ algorithm = Z_LVAL_PP(z_algorithm);
+
+ /* need to conver the first parameter from int to string */
+ if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
+ struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
+ if (algorithm_lookup.hash_name) {
+ ZVAL_STRING(*z_algorithm, algorithm_lookup.hash_name, 1);
+ }
+ }
+
+ if (ZEND_NUM_ARGS() == 3) {
+ php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
+ } else if (ZEND_NUM_ARGS() == 2) {
+ php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
+ } else {
+ WRONG_PARAM_COUNT;
+ }
+}
+
+PHP_FUNCTION(mhash_get_hash_name)
+{
+ int algorithm;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &algorithm) == FAILURE) {
+ return;
+ }
+
+ if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
+ struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
+ if (algorithm_lookup.mhash_name) {
+ RETURN_STRING(algorithm_lookup.mhash_name, 1);
+ }
+ }
+ RETURN_FALSE;
+}
+
+PHP_FUNCTION(mhash_count)
+{
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+ RETURN_LONG(MHASH_NUM_ALGOS - 1);
+}
+
+PHP_FUNCTION(mhash_get_block_size)
+{
+ int algorithm;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &algorithm) == FAILURE) {
+ return;
+ }
+ RETVAL_FALSE;
+
+ if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
+ struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
+ if (algorithm_lookup.mhash_name) {
+ const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
+ if (ops) {
+ RETVAL_LONG(ops->digest_size);
+ }
+ }
+ }
+}
+
+#define SALT_SIZE 8
+
+PHP_FUNCTION(mhash_keygen_s2k)
+{
+ int algorithm, bytes;
+ char *password, *salt;
+ int password_len, salt_len;
+ char padded_salt[SALT_SIZE];
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &bytes) == FAILURE) {
+ return;
+ }
+
+ if (bytes <= 0){
+ php_error_docref(NULL TSRMLS_CC, E_WARNING, "the byte parameter must be greater than 0");
+ RETURN_FALSE;
+ }
+
+ salt_len = MIN(salt_len, SALT_SIZE);
+
+ memcpy(padded_salt, salt, salt_len);
+ if (salt_len < SALT_SIZE) {
+ memset(padded_salt + salt_len, 0, SALT_SIZE - salt_len);
+ }
+ salt_len = SALT_SIZE;
+
+ RETVAL_FALSE;
+ if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
+ struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
+ if (algorithm_lookup.mhash_name) {
+ const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
+ if (ops) {
+ unsigned char null = '\0';
+ void *context;
+ char *key, *digest;
+ int i = 0, j = 0;
+ int block_size = ops->digest_size;
+ int times = bytes / block_size;
+ if (bytes % block_size != 0) times++;
+
+ context = emalloc(ops->context_size);
+ ops->hash_init(context);
+
+ key = ecalloc(1, times * block_size);
+ digest = emalloc(ops->digest_size + 1);
+
+ for (i = 0; i < times; i++) {
+ ops->hash_init(context);
+
+ for (j=0;j<i;j++) {
+ ops->hash_update(context, &null, 1);
+ }
+ ops->hash_update(context, (unsigned char *)padded_salt, salt_len);
+ ops->hash_update(context, password, password_len);
+ ops->hash_final(digest, context);
+ memcpy( &key[i*block_size], digest, block_size);
+ }
+
+ RETVAL_STRINGL(key, bytes, 1);
+ memset(key, 0, bytes);
+ efree(digest);
+ efree(context);
+ efree(key);
+ }
+ }
+ }
+}
+
+#endif
+
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(hash)
REGISTER_LONG_CONSTANT("HASH_HMAC", PHP_HASH_HMAC, CONST_CS | CONST_PERSISTENT);
+#ifdef PHP_MHASH_BC
+ mhash_init(INIT_FUNC_ARGS_PASSTHRU);
+#endif
+
return SUCCESS;
}
/* }}} */
ZEND_BEGIN_ARG_INFO(arginfo_hash_algos, 0)
ZEND_END_ARG_INFO()
+/* BC Land */
+#ifdef PHP_MHASH_BC
+static
+ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_block_size, 0)
+ ZEND_ARG_INFO(0, hash)
+ZEND_END_ARG_INFO()
+
+static
+ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_hash_name, 0)
+ ZEND_ARG_INFO(0, hash)
+ZEND_END_ARG_INFO()
+
+static
+ZEND_BEGIN_ARG_INFO(arginfo_mhash_keygen_s2k, 0)
+ ZEND_ARG_INFO(0, hash)
+ ZEND_ARG_INFO(0, input_password)
+ ZEND_ARG_INFO(0, salt)
+ ZEND_ARG_INFO(0, bytes)
+ZEND_END_ARG_INFO()
+
+static
+ZEND_BEGIN_ARG_INFO(arginfo_mhash_count, 0)
+ZEND_END_ARG_INFO()
+
+static
+ZEND_BEGIN_ARG_INFO_EX(arginfo_mhash, 0, 0, 2)
+ ZEND_ARG_INFO(0, hash)
+ ZEND_ARG_INFO(0, data)
+ ZEND_ARG_INFO(0, key)
+ZEND_END_ARG_INFO()
+#endif
+
/* }}} */
/* {{{ hash_functions[]
PHP_NAMED_FE(sha1_file, php_if_sha1_file, arginfo_hash_sha1_file)
#endif /* PHP_HASH_SHA1_NOT_IN_CORE */
+#ifdef PHP_MHASH_BC
+ PHP_FE(mhash_keygen_s2k, arginfo_mhash_keygen_s2k)
+ PHP_FE(mhash_get_block_size, arginfo_mhash_get_block_size)
+ PHP_FE(mhash_get_hash_name, arginfo_mhash_get_hash_name)
+ PHP_FE(mhash_count, arginfo_mhash_count)
+ PHP_FE(mhash, arginfo_mhash)
+#endif
+
{NULL, NULL, NULL}
};
/* }}} */
--- /dev/null
+--TEST--
+mhash() test
+--INI--
+magic_quotes_runtime=0
+--SKIPIF--
+<?php
+ include "skip_mhash.inc";
+?>
+--FILE--
+<?php
+
+$supported_hash_al = array(
+"MHASH_MD5" => "2d9bdb91f94e96d9c4e2ae532acc936a",
+"MHASH_SHA1" => "2f9341e55a9083edf5497bf83ba3db812a7de0a3",
+"MHASH_HAVAL256" => "b255feff01ad641b27358dc7909bc695a1fca53bddfdfaf19020b275928793af",
+"MHASH_HAVAL192" => "4ce837de481e1e30092ab2c610057094c988dfd7db1e01cd",
+"MHASH_HAVAL224" => "5362d1856752bf2c139bb2d6fdd772b9c515c8ce5ec82695264b85e1",
+"MHASH_HAVAL160" => "c6b36f87750b18576981bc17b4f22271947bf9cb",
+"MHASH_RIPEMD160" => "6c47435aa1d359c4b7c6af46349f0c3e1258583d",
+"MHASH_GOST" => "101b0a2552cebdf5137cadf15147f21e55b6432935bb9c2c03c7e28d188b2d9e",
+"MHASH_TIGER" => "fdb9019a79c33a95677e2097abae91eb0de00b3054bb5c39",
+"MHASH_CRC32" => "83041db8",
+"MHASH_CRC32B" => "a4b75adf"
+);
+
+$data = "This is the test of the mhash extension...";
+
+foreach ($supported_hash_al as $hash=>$wanted) {
+ $result = mhash(constant($hash), $data);
+ if (bin2hex($result)==$wanted) {
+ echo "$hash\nok\n";
+ } else {
+ echo "$hash: ";
+ var_dump($wanted);
+ echo "$hash: ";
+ var_dump($result);
+ }
+ echo "\n";
+}
+?>
+--EXPECT--
+MHASH_MD5
+ok
+
+MHASH_SHA1
+ok
+
+MHASH_HAVAL256
+ok
+
+MHASH_HAVAL192
+ok
+
+MHASH_HAVAL224
+ok
+
+MHASH_HAVAL160
+ok
+
+MHASH_RIPEMD160
+ok
+
+MHASH_GOST
+ok
+
+MHASH_TIGER
+ok
+
+MHASH_CRC32
+ok
+
+MHASH_CRC32B
+ok
mhash_get_block_size() & mhash_get_hash_name() test
--SKIPIF--
<?php
- include "skip.inc";
+ include "skip_mhash.inc";
?>
--FILE--
<?php
--- /dev/null
+--TEST--
+mhash_keygen_s2k() test
+--SKIPIF--
+<?php
+ include "skip_mhash.inc";
+?>
+--FILE--
+<?php
+
+$supported_hash_al = array(
+"MHASH_MD5" => "8690154eaf9432cde9347aa15094b9c046eb06e6a0940c5479aa7a6367ae68b5e0e0745e5709fede2d9fe9739d9aad413759faa73acced821077b4ddb2788064e371eb53b3a9d55ed2839aab2655c82cfedbe83a208461c799d9d77ae481061c81539b01",
+"MHASH_SHA1" => "dd315c70061d07455d53c2fb0b08df0c61aa665c1ab1a701fa10955423248ba832a5ade406b39b78630aba3d1688e622494a0eae279d4ece9ad4bdf76e878fcb084a33c9153c2b48131d30a75b00a7c05b91f1ffeabf59bb1271c4d8a11990b84baf6d49",
+"MHASH_HAVAL256" => "0ede47009f87d5e9a24ecf5077d60c483657a5d98404ab2bb780f5872c90caf61c0d67645a848e55fee107296f4169c95b4e61f0aeeefab2648554c1171fb0a2fc32aa5aeed3d5c155d334367d4959622cdadefe43ae17bd1a75f9d4fef77bf192be5b78",
+"MHASH_HAVAL224" => "5c4aff3d825ad608f608c8eae779ee3868610bc60a98f3d770b311a6677c797fc2dadcab71dde0c0191e068397ab297f0de5cbbc6cbcd0c78ca8470c42401f6b77e81dc2ba8d51930ff982760335324fb850ac2d30b73514004c096d60472d320e0ec349",
+"MHASH_HAVAL192" => "22e0c27126023c852ef94107bb2f1ee132b064178b9dcbfb1c32e658760b8f70bdc5b1c52599031628c2433bee2b0870ab7a38aeb21215134ec1088975b9a96487642971ef9eb3d987baf9765fd9e6d64d494e1719aa84afe7e0a0784c74979ebab1c787",
+"MHASH_HAVAL160" => "d6e5f0ef07f3facced646eedb6364758ecde6dc6fb061e00a496f5ceb723f78ea135884d9682226ded69c11d8431240ef97cad583c4f29593bbf3dd3cab0b8792eb3d86022ca6002ebd0d9b4429909d4af85bed2b5a96b3e47b9b8cac919c1177ec40d7e",
+"MHASH_RIPEMD160" => "e4d5db469af29f78e2b90dc735c9cf020a1d5b19a6674458677794d4dca144d426c562aff98d8e866a8a924299ebf6b0ea9a1637f987a1fb5de9b647edc35b1447605e1babc3084be7a003931117eb33432d4142e225df044b033f3ff64bb4a18682a4f9",
+"MHASH_GOST" => "c044f669bd7e8643953d77c682fd179242d9df157dadf873be4d9601e4647c018234689359e7220ab0492a6240d184c478634073dea87f79be7f86fd4e2564f7d709b68a46440a121250e00fc7d57d45a9c07ee23a704ff4148c0dad7077ec527b194d87",
+"MHASH_TIGER" => "67eac97b9dca0a47b1f6262f330264e4ce1c233760fe3255f642512fd3127929baccf1e758236b2768a4c2c0c06e118b19e40e2f04a5f745820fb8a99bdbc00698702a4d3120171856c4c94bda79ba1b4f60d509d7f8954da818a29797368dd47c1122aa",
+"MHASH_CRC32" => "481c40148c26185f9a59ef18e86f51c5d2d0315b46711d22ae08c1ccdd669fe956c817380815e3a545f6ee453c9da48d1d994dbc3ac8ba85a572108412f06b2a16b1489cda75b118e82f7d9bdfdb68336957bbf19e4a3f76750d6985a53dd557229dfcf3",
+"MHASH_CRC32B" => "b56cab65a63e7dfb2aa95d7fb646d79b36138a6243cdcb8f2e0949af0f966a9ccea530d0db0d1f3c98c62e5179e796beb68d7469fdb07862d8247d830bf598c8b49309d7cfacc88c44c5444b8513e931754cf0dd36a7a160f7e6c98f907c4563f1047fb0"
+);
+
+foreach ($supported_hash_al as $hash=>$wanted) {
+ $passwd = str_repeat($hash, 10);
+ $salt = str_repeat($hash, 2);
+ $result = mhash_keygen_s2k(constant($hash), $passwd, $salt, 100);
+ if (!strcmp(bin2hex($result), $wanted)) {
+ echo "$hash\nok\n";
+ } else {
+ echo "$hash: ";
+ var_dump($wanted);
+ echo "$hash: ";
+ var_dump(bin2hex($result));
+ }
+ echo "\n";
+}
+?>
+--EXPECT--
+MHASH_MD5
+ok
+
+MHASH_SHA1
+ok
+
+MHASH_HAVAL256
+ok
+
+MHASH_HAVAL224
+ok
+
+MHASH_HAVAL192
+ok
+
+MHASH_HAVAL160
+ok
+
+MHASH_RIPEMD160
+ok
+
+MHASH_GOST
+ok
+
+MHASH_TIGER
+ok
+
+MHASH_CRC32
+ok
+
+MHASH_CRC32B
+ok
[ --with-mhash[=DIR] Include mhash support])
if test "$PHP_MHASH" != "no"; then
- for i in $PHP_MHASH /usr/local /usr /opt/mhash; do
- test -f $i/include/mhash.h && MHASH_DIR=$i && break
- done
-
- if test -z "$MHASH_DIR"; then
- AC_MSG_ERROR(Please reinstall libmhash - I cannot find mhash.h)
- fi
-
- PHP_ADD_INCLUDE($MHASH_DIR/include)
- PHP_ADD_LIBRARY_WITH_PATH(mhash, $MHASH_DIR/$PHP_LIBDIR, MHASH_SHARED_LIBADD)
-
PHP_NEW_EXTENSION(mhash, mhash.c, $ext_shared)
PHP_SUBST(MHASH_SHARED_LIBADD)
- AC_DEFINE(HAVE_LIBMHASH,1,[ ])
+ PHP_ADD_EXTENSION_DEP(mhash, hash, true)
fi
#endif
#include "php.h"
-
-#if HAVE_LIBMHASH
-
-#include "fcntl.h"
#include "php_mhash.h"
-#include "mhash.h"
#include "php_ini.h"
#include "php_globals.h"
#include "ext/standard/info.h"
const zend_function_entry mhash_functions[] = {
- PHP_FE(mhash_get_block_size, NULL)
- PHP_FE(mhash_get_hash_name, NULL)
- PHP_FE(mhash_keygen_s2k, NULL)
- PHP_FE(mhash_count, NULL)
- PHP_FE(mhash, NULL)
{NULL, NULL, NULL}
};
ZEND_GET_MODULE(mhash)
#endif
-/* SALTED S2K uses a fixed salt */
-#define SALT_SIZE 8
-
PHP_MINIT_FUNCTION(mhash)
{
- int i, n, l;
- char *name;
- char buf[128];
-
- n = mhash_count() + 1;
-
- for (i=0; i<n; i++) {
- if ((name = mhash_get_hash_name(i))) {
- l = slprintf(buf, 127, "MHASH_%s", name);
- zend_register_long_constant(buf, l + 1, i, CONST_PERSISTENT, module_number TSRMLS_CC);
- free(name);
- }
- }
-
return SUCCESS;
}
PHP_MINFO_FUNCTION(mhash)
{
- char version[32];
-
- snprintf(version, sizeof(version), "%d", MHASH_API_VERSION);
-
php_info_print_table_start();
php_info_print_table_row(2, "MHASH support", "Enabled");
- php_info_print_table_row(2, "MHASH API Version", version);
+ php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
php_info_print_table_end();
}
-/* {{{ proto int mhash_count(void)
- Gets the number of available hashes */
-PHP_FUNCTION(mhash_count)
-{
- if (zend_parse_parameters_none() == FAILURE) {
- return;
- }
-
- RETURN_LONG(mhash_count());
-}
-
-/* }}} */
-
-/* {{{ proto int mhash_get_block_size(int hash)
- Gets the block size of hash */
-PHP_FUNCTION(mhash_get_block_size)
-{
- long hash;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &hash) == FAILURE) {
- return;
- }
-
- RETURN_LONG(mhash_get_block_size(hash));
-}
-
-/* }}} */
-
-/* {{{ proto string mhash_get_hash_name(int hash)
- Gets the name of hash */
-PHP_FUNCTION(mhash_get_hash_name)
-{
- char *name;
- long hash;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &hash) == FAILURE) {
- return;
- }
-
- name = mhash_get_hash_name(hash);
- if (name) {
- RETVAL_STRING(name, 1);
- free(name);
- } else {
- RETVAL_FALSE;
- }
-}
-
-/* }}} */
-
-/* {{{ proto string mhash(int hash, string data [, string key])
- Hash data with hash */
-PHP_FUNCTION(mhash)
-{
- MHASH td;
- int bsize;
- unsigned char *hash_data;
- long hash;
- int data_len, key_len=0;
- char *data, *key=NULL;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls|s", &hash, &data, &data_len, &key, &key_len) == FAILURE) {
- return;
- }
-
- bsize = mhash_get_block_size(hash);
-
- if (key_len) {
- if (mhash_get_hash_pblock(hash) == 0) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "mhash initialization failed");
- RETURN_FALSE;
- }
- td = mhash_hmac_init(hash, key, key_len, mhash_get_hash_pblock(hash));
- } else {
- td = mhash_init(hash);
- }
-
- if (td == MHASH_FAILED) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "mhash initialization failed");
- RETURN_FALSE;
- }
-
- mhash(td, data, data_len);
-
- if (key_len) {
- hash_data = (unsigned char *) mhash_hmac_end(td);
- } else {
- hash_data = (unsigned char *) mhash_end(td);
- }
-
- if (hash_data) {
- RETVAL_STRINGL(hash_data, bsize, 1);
- mhash_free(hash_data);
- } else {
- RETURN_FALSE;
- }
-}
-
-/* }}} */
-
-/* {{{ proto string mhash_keygen_s2k(int hash, string input_password, string salt, int bytes)
- Generates a key using hash functions */
-PHP_FUNCTION(mhash_keygen_s2k)
-{
- KEYGEN keystruct;
- char salt[SALT_SIZE], *ret;
- long hash, bytes;
- char *password, *in_salt;
- int password_len, salt_len;
-
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lssl", &hash, &password, &password_len, &in_salt, &salt_len, &bytes) == FAILURE) {
- return;
- }
- if (bytes <= 0){
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "the byte parameter must be greater than 0");
- RETURN_FALSE;
- }
-
- salt_len = MIN(salt_len, SALT_SIZE);
-
- if (salt_len > mhash_get_keygen_salt_size(KEYGEN_S2K_SALTED)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING,
- "The specified salt [%d] is more bytes than the required by the algorithm [%d]\n",
- salt_len, mhash_get_keygen_salt_size(KEYGEN_S2K_SALTED));
- }
-
- memcpy(salt, in_salt, salt_len);
- if (salt_len < SALT_SIZE) {
- memset(salt + salt_len, 0, SALT_SIZE - salt_len);
- }
- salt_len = SALT_SIZE;
-
- keystruct.hash_algorithm[0] = hash;
- keystruct.hash_algorithm[1] = hash;
- keystruct.count = 0;
- keystruct.salt = salt;
- keystruct.salt_size = salt_len;
-
- ret = safe_emalloc(1, bytes, 1);
-
- if (mhash_keygen_ext(KEYGEN_S2K_SALTED, keystruct, ret, bytes, password, password_len) >= 0) {
- ret[bytes] = '\0';
- RETVAL_STRINGL(ret, bytes, 0);
- } else {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "mhash key generation failed");
- efree(ret);
- RETURN_FALSE;
- }
-}
-/* }}} */
-
-#endif
-
/*
* Local variables:
* tab-width: 4
+++ /dev/null
-# Microsoft Developer Studio Project File - Name="mhash" - Package Owner=<4>\r
-# Microsoft Developer Studio Generated Build File, Format Version 6.00\r
-# ** DO NOT EDIT **\r
-\r
-# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102\r
-\r
-CFG=mhash - Win32 Release_TS\r
-!MESSAGE This is not a valid makefile. To build this project using NMAKE,\r
-!MESSAGE use the Export Makefile command and run\r
-!MESSAGE \r
-!MESSAGE NMAKE /f "mhash.mak".\r
-!MESSAGE \r
-!MESSAGE You can specify a configuration when running NMAKE\r
-!MESSAGE by defining the macro CFG on the command line. For example:\r
-!MESSAGE \r
-!MESSAGE NMAKE /f "mhash.mak" CFG="mhash - Win32 Release_TS"\r
-!MESSAGE \r
-!MESSAGE Possible choices for configuration are:\r
-!MESSAGE \r
-!MESSAGE "mhash - Win32 Release_TS" (based on "Win32 (x86) Dynamic-Link Library")\r
-!MESSAGE "mhash - Win32 Debug_TS" (based on "Win32 (x86) Dynamic-Link Library")\r
-!MESSAGE \r
-\r
-# Begin Project\r
-# PROP AllowPerConfigDependencies 0\r
-# PROP Scc_ProjName ""\r
-# PROP Scc_LocalPath ""\r
-CPP=cl.exe\r
-MTL=midl.exe\r
-RSC=rc.exe\r
-\r
-!IF "$(CFG)" == "mhash - Win32 Release_TS"\r
-\r
-# PROP BASE Use_MFC 0\r
-# PROP BASE Use_Debug_Libraries 0\r
-# PROP BASE Output_Dir "Release_TS"\r
-# PROP BASE Intermediate_Dir "Release_TS"\r
-# PROP BASE Ignore_Export_Lib 0\r
-# PROP BASE Target_Dir ""\r
-# PROP Use_MFC 0\r
-# PROP Use_Debug_Libraries 0\r
-# PROP Output_Dir "..\..\Release_TS"\r
-# PROP Intermediate_Dir "Release_TS"\r
-# PROP Ignore_Export_Lib 0\r
-# PROP Target_Dir ""\r
-# ADD BASE CPP /nologo /MT /W3 /GX /O2 /I "..\.." /I "..\..\..\Zend" /I "..\..\..\TSRM" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "COMPILE_DL_MHASH" /D ZTS=1 /YX /FD /c\r
-# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\.." /I "..\..\Zend" /I "..\..\TSRM" /I "..\..\main" /I "..\..\win32" /D "WIN32" /D "MHASH_EXPORTS" /D "COMPILE_DL_MHASH" /D HAVE_LIBMHASH=1 /D ZEND_DEBUG=0 /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "ZEND_WIN32" /D "PHP_WIN32" /D ZTS=1 /YX /FD /c\r
-# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32\r
-# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32\r
-# ADD BASE RSC /l 0x406 /d "NDEBUG"\r
-# ADD RSC /l 0x406 /d "NDEBUG"\r
-BSC32=bscmake.exe\r
-# ADD BASE BSC32 /nologo\r
-# ADD BSC32 /nologo\r
-LINK32=link.exe\r
-# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php5ts.lib /nologo /dll /machine:I386\r
-# ADD LINK32 php5ts.lib libmhash.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"..\..\Release_TS/php_mhash.dll" /libpath:"..\..\Release_TS" /libpath:"..\..\Release_TS_Inline"\r
-# SUBTRACT LINK32 /pdb:none\r
-\r
-!ELSEIF "$(CFG)" == "mhash - Win32 Debug_TS"\r
-\r
-# PROP BASE Use_MFC 0\r
-# PROP BASE Use_Debug_Libraries 0\r
-# PROP BASE Output_Dir "Debug_TS"\r
-# PROP BASE Intermediate_Dir "Debug_TS"\r
-# PROP BASE Ignore_Export_Lib 0\r
-# PROP BASE Target_Dir ""\r
-# PROP Use_MFC 0\r
-# PROP Use_Debug_Libraries 0\r
-# PROP Output_Dir "..\..\Debug_TS"\r
-# PROP Intermediate_Dir "Debug_TS"\r
-# PROP Ignore_Export_Lib 0\r
-# PROP Target_Dir ""\r
-# ADD BASE CPP /nologo /MT /W3 /GX /O2 /I "..\.." /I "..\..\Zend" /I "..\..\TSRM" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "COMPILE_DL_MHASH" /D ZTS=1 /YX /FD /c\r
-# ADD CPP /nologo /MDd /W3 /GX /O2 /I "..\.." /I "..\..\Zend" /I "..\..\TSRM" /I "..\..\main" /I "..\..\win32" /D ZEND_DEBUG=1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "MHASH_EXPORTS" /D "COMPILE_DL_MHASH" /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_MHASH=1 /D ZTS=1 /YX /FD /c\r
-# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32\r
-# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32\r
-# ADD BASE RSC /l 0x406 /d "NDEBUG"\r
-# ADD RSC /l 0x406 /d "NDEBUG"\r
-BSC32=bscmake.exe\r
-# ADD BASE BSC32 /nologo\r
-# ADD BSC32 /nologo\r
-LINK32=link.exe\r
-# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php5ts_debug.lib /nologo /dll /machine:I386\r
-# ADD LINK32 php5ts_debug.lib libmhash.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"..\..\Debug_TS/php_mhash.dll" /libpath:"..\..\Debug_TS"\r
-# SUBTRACT LINK32 /pdb:none\r
-\r
-!ENDIF \r
-\r
-# Begin Target\r
-\r
-# Name "mhash - Win32 Release_TS"\r
-# Name "mhash - Win32 Debug_TS"\r
-# Begin Group "Source Files"\r
-\r
-# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"\r
-# Begin Source File\r
-\r
-SOURCE=.\mhash.c\r
-# End Source File\r
-# End Group\r
-# Begin Group "Header Files"\r
-\r
-# PROP Default_Filter "h;hpp;hxx;hm;inl"\r
-# Begin Source File\r
-\r
-SOURCE=.\php_mhash.h\r
-# End Source File\r
-# End Group\r
-# Begin Group "Resource Files"\r
-\r
-# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"\r
-# End Group\r
-# End Target\r
-# End Project\r
#ifndef PHP_MHASH_H
#define PHP_MHASH_H
-#if HAVE_LIBMHASH
-
#if PHP_API_VERSION < 19990421
#define zend_module_entry zend_module_entry
#include "zend_modules.h"
PHP_MINIT_FUNCTION(mhash);
PHP_MINFO_FUNCTION(mhash);
-PHP_FUNCTION(mhash_get_block_size);
-PHP_FUNCTION(mhash_get_hash_name);
-PHP_FUNCTION(mhash_count);
-PHP_FUNCTION(mhash_keygen_s2k);
-PHP_FUNCTION(mhash);
-
-#else
-#define mhash_module_ptr NULL
-#endif
#define phpext_mhash_ptr mhash_module_ptr
+++ /dev/null
---TEST--
-mhash() test
---INI--
-magic_quotes_runtime=0
---SKIPIF--
-<?php
- include "skip.inc";
-?>
---FILE--
-<?php
-
-$supported_hash_al = array(
-"MHASH_MD5" => "-\9bÛ\91ùN\96ÙÄâ®S*Ì\93j",
-"MHASH_SHA1" => "/\93AåZ\90\83íõI{ø;£Û\81*}à£",
-"MHASH_HAVAL256" => "²Uþÿ\ 1d\e'5\8dÇ\90\9bÆ\95¡ü¥;Ýýúñ\90 ²u\92\87\93¯",
-"MHASH_HAVAL192" => "Lè7ÞH\1e\1e0 *²Æ\10\ 5p\94É\88ß×Û\1e\ 1Í",
-"MHASH_HAVAL224" => "SbÑ\85gR¿,\13\9b²Öý×r¹Å\15ÈÎ^È&\95&K\85á",
-"MHASH_HAVAL160" => "Ƴo\87u\v\18Wi\81¼\17´ò\"q\94{ùË",
-"MHASH_RIPEMD160" => "lGCZ¡ÓYķƯF4\9f\x0C>\12XX=",
-"MHASH_GOST" => "\10\e\x0A%Rνõ\13|ñQGò\1eU¶C)5»\9c,\ 3Çâ\8d\18\8b-\9e",
-"MHASH_TIGER" => "ý¹\ 1\9ayÃ:\95g~ \97«®\91ë
-à\v0T»\9",
-"MHASH_CRC32" => "\83\ 4\1d¸",
-"MHASH_CRC32B" => "¤·Zß"
-);
-
-$data = "This is the test of the mhash extension...";
-
-foreach ($supported_hash_al as $hash=>$wanted) {
- $result = mhash(constant($hash), $data);
- if ($result==$wanted) {
- echo "$hash\nok\n";
- } else {
- echo "$hash: ";
- var_dump($wanted);
- echo "$hash: ";
- var_dump($result);
- }
- echo "\n";
-}
-?>
---EXPECT--
-MHASH_MD5
-ok
-
-MHASH_SHA1
-ok
-
-MHASH_HAVAL256
-ok
-
-MHASH_HAVAL192
-ok
-
-MHASH_HAVAL224
-ok
-
-MHASH_HAVAL160
-ok
-
-MHASH_RIPEMD160
-ok
-
-MHASH_GOST
-ok
-
-MHASH_TIGER
-ok
-
-MHASH_CRC32
-ok
-
-MHASH_CRC32B
-ok
+++ /dev/null
---TEST--
-mhash_keygen_s2k() test
---SKIPIF--
-<?php
- include "skip.inc";
-?>
---FILE--
-<?php
-
-$supported_hash_al = array(
-"MHASH_MD5" => "\86\90\x15N¯\942Íé4z¡P\94¹ÀFë\x06æ \94\x0CTyªzcg®hµààt^W\x09þÞ-\9fés\9d\9aA7Yú§:Ìí\82\x10w´Ý²x\80dãqëS³©Õ^Ò\83\9a«&UÈ,þÛè: \84aÇ\99Ù×zä\81\x06\x1C\81S\9b\x01",
-"MHASH_SHA1" => "Ý1\\p\x06\x1D\x07E]SÂû\x0B\x08ß\x0Caªf\\\x1A±§\x01ú\x10\95T#\$\8b¨2¥ä\x06³\9bxc\x0Aº=\x16\88æ\"IJ\x0E®'\9dNÎ\9aÔ½÷n\87\8fË\x08J3É\x15<+H\x13\x1D0§[\x00§À[\91ñÿê¿Y»\x12qÄØ¡\x19\90¸K¯mI",
-"MHASH_HAVAL256" => "\ eÞG\x00\9f\87Õé¢NÏPwÖ\x0CH6W¥Ù\84\ 4«+·\80õ\87,\90Êö\x1C\x0DgdZ\84\8eUþá\x07)oAiÉ[Nað®îú²d\85TÁ\x17\x1F°¢ü2ªZîÓÕÁUÓ46}IYb,ÚÞþC®\x17½\x1AuùÔþ÷{ñ\92¾[x",
-"MHASH_HAVAL224" => "\\Jÿ=\82ZÖ\x08ö\x08Èêçyî8ha\vÆ\x0A\98ó×p³\11¦g|y\7fÂÚÜ«qÝàÀ\x19\x1E\x06\83\97«)\7f
-å˼l¼ÐÇ\8c¨G\x0CB@\1fkwè\1dº\8dQ\93\ fù\82v\ 352O¸P¬-0·5\14\x00L m`G-2\ e\ eÃI",
-"MHASH_HAVAL192" => "\"àÂq&\x02<\85.ùA\x07»/\x1Eá2°d\x17\8b\9dËû\x1C2æXv\x0B\8fp½Å±Å%\99\x03\x16(ÂC;î+\x08p«z8®²\x12\x15\x13NÁ\x08\89u¹©d\87d)qï\9e³Ù\87ºùv_ÙæÖMIN\17\19ª\84¯çà xLt\97\9eº±Ç\87",
-"MHASH_HAVAL160" => "Öåðï\x07óúÌídní¶6GXìÞmÆû\x06\x1E\x00¤\96õη#÷\8e¡5\88M\96\82\"míiÁ\x1D\841\$\x0Eù|X<O)Y;¿=ÓÊ°¸y.³Ø`\"Ê`\x02ëÐÙ´B\99 Ô¯\85¾Òµ©k>G¹¸ÊÉ\x19Á\x17~Ä\x0D~",
-"MHASH_RIPEMD160" => "äÕÛF\9aò\9fxâ¹\x0DÇ5ÉÏ\x02\x0a\x1D[\x19¦gDXgw\94ÔÜ¡DÔ&Åb¯ù\8d\8e\86j\8a\92B\99ëö°ê\9a\167ù\87¡û]é¶GíÃ[\x14G`^\x1B«Ã\x08Kç \ 3\93\x11\x17ë3C-ABâ%ß\x04K\x03??öK´¡\86\82¤ù",
-"MHASH_GOST" => "ÀDöi½~\86C\95=wÆ\82ý\x17\92BÙß\x15}øs¾M\96\x01äd|\x01\824h\93Yç\"\x0A°I*b@Ñ\84Äxc@sÞ¨\7fy¾\7f\86ýN%d÷×\x09¶\8aFD\x0a\x12\x12Pà\x0FÇÕ}E©À~â:pOô\14\8c\x0DpwìR{\x19M\87",
-"MHASH_TIGER" => "gêÉ{\9dÊ\nG±ö&/3\ 2däÎ\1c#7`þ2UöBQ/Ó\12y)ºÌñçX#k'h¤ÂÀÀn\11\8b\19ä\ e/\ 4¥÷E\82\ f¸©\9bÛÀ\ 6\98p*M1 \17\18VÄÉKÚyº\eO`Õ ×ø\95M¨\18¢\97\976\8dÔ|\11\"ª",
-"MHASH_CRC32" => "H\1c@\14\8c&\18_\9aYï\18èoQÅÒÐ1[Fq\1d\"®\x08ÁÌÝf\9féVÈ\178\x08\15ã¥EöîE<\9d¤\8d\1d\99M¼:Ⱥ\85¥r\10\84\12ðk*\16±H\9cÚu±\18è/}\9bßÛh3iW»ñ\9eJ?vu
-i\85¥=ÕW\"\9düó",
-"MHASH_CRC32B" => "µl«e¦>}û*©]\7f¶F×\9b6\x13\8abCÍË\8f. I¯\x0F\96j\9cÎ¥0ÐÛ\x0D\x1F<\98Æ.Qyç\96¾¶\8dtiý°xbØ\$}\83\x0Bõ\98È´\93 ×ϬÈ\8cDÅDK\85\x13é1uLðÝ6§¡`÷æÉ\8f\90|Ecñ\x04\7f°",
-);
-
-foreach ($supported_hash_al as $hash=>$wanted) {
- $passwd = str_repeat($hash, 10);
- $salt = str_repeat($hash, 2);
- $result = mhash_keygen_s2k(constant($hash), $passwd, $salt, 100);
- if (!strcmp($result, $wanted)) {
- echo "$hash\nok\n";
- } else {
- echo "$hash: ";
- var_dump(bin2hex($wanted));
- echo "$hash: ";
- var_dump(bin2hex($result));
- }
- echo "\n";
-}
-?>
---EXPECT--
-MHASH_MD5
-ok
-
-MHASH_SHA1
-ok
-
-MHASH_HAVAL256
-ok
-
-MHASH_HAVAL224
-ok
-
-MHASH_HAVAL192
-ok
-
-MHASH_HAVAL160
-ok
-
-MHASH_RIPEMD160
-ok
-
-MHASH_GOST
-ok
-
-MHASH_TIGER
-ok
-
-MHASH_CRC32
-ok
-
-MHASH_CRC32B
-ok