Skip to content

Commit 01f3403

Browse files
committed
Added bool hash_equals(string $known, string $given)
1 parent 190c777 commit 01f3403

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

ext/hash/hash.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,29 @@ PHP_FUNCTION(hash_pbkdf2)
729729
}
730730
/* }}} */
731731

732+
PHP_FUNCTION(hash_equals)
733+
{
734+
char *known, *given, *known_padded;
735+
int known_len, given_len, i, result;
736+
737+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &known, &known_len, &given, &given_len) == FAILURE) {
738+
return;
739+
}
740+
741+
known_padded = (char *)ecalloc(1, known_len + given_len);
742+
memcpy(known, known_padded, known_len);
743+
744+
result = known_len - given_len;
745+
746+
for (i = 0; i < given_len; ++i) {
747+
result |= given[i] ^ known_padded[i];
748+
}
749+
750+
efree(known_padded);
751+
752+
RETVAL_BOOL(result == 0);
753+
}
754+
732755
/* Module Housekeeping */
733756

734757
static void php_hash_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ */
@@ -881,6 +904,7 @@ PHP_FUNCTION(mhash_get_block_size)
881904
}
882905
/* }}} */
883906

907+
884908
#define SALT_SIZE 8
885909

886910
/* {{{ proto string mhash_keygen_s2k(int hash, string input_password, string salt, int bytes)
@@ -1178,6 +1202,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_mhash, 0, 0, 2)
11781202
ZEND_END_ARG_INFO()
11791203
#endif
11801204

1205+
ZEND_BEGIN_ARG_INFO(arginfo_hash_equals, 0)
1206+
ZEND_ARG_INFO(0, known)
1207+
ZEND_ARG_INFO(0, given)
1208+
ZEND_END_ARG_INFO()
1209+
11811210
/* }}} */
11821211

11831212
/* {{{ hash_functions[]
@@ -1198,6 +1227,7 @@ const zend_function_entry hash_functions[] = {
11981227

11991228
PHP_FE(hash_algos, arginfo_hash_algos)
12001229
PHP_FE(hash_pbkdf2, arginfo_hash_pbkdf2)
1230+
PHP_FE(hash_equals, arginfo_hash_equals)
12011231

12021232
/* BC Land */
12031233
#ifdef PHP_HASH_MD5_NOT_IN_CORE

ext/hash/php_hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ PHP_FUNCTION(hash_update_file);
134134
PHP_FUNCTION(hash_final);
135135
PHP_FUNCTION(hash_algos);
136136
PHP_FUNCTION(hash_pbkdf2);
137+
PHP_FUNCTION(hash_equals);
137138

138139
PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, int algo_len);
139140
PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops);

0 commit comments

Comments
 (0)