PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/hash - hash.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 442 497 88.9 %
Date: 2014-10-30 Functions: 31 33 93.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2014 The PHP Group                                |
       6             :   +----------------------------------------------------------------------+
       7             :   | This source file is subject to version 3.01 of the PHP license,      |
       8             :   | that is bundled with this package in the file LICENSE, and is        |
       9             :   | available through the world-wide-web at the following url:           |
      10             :   | http://www.php.net/license/3_01.txt                                  |
      11             :   | If you did not receive a copy of the PHP license and are unable to   |
      12             :   | obtain it through the world-wide-web, please send a note to          |
      13             :   | license@php.net so we can mail you a copy immediately.               |
      14             :   +----------------------------------------------------------------------+
      15             :   | Author: Sara Golemon <pollita@php.net>                               |
      16             :   |         Scott MacVicar <scottmac@php.net>                            |
      17             :   +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #ifdef HAVE_CONFIG_H
      23             : #include "config.h"
      24             : #endif
      25             : 
      26             : #include <math.h>
      27             : #include "php_hash.h"
      28             : #include "ext/standard/info.h"
      29             : #include "ext/standard/file.h"
      30             : 
      31             : static int php_hash_le_hash;
      32             : HashTable php_hash_hashtable;
      33             : 
      34             : #if (PHP_MAJOR_VERSION >= 5)
      35             : # define DEFAULT_CONTEXT FG(default_context)
      36             : #else
      37             : # define DEFAULT_CONTEXT NULL
      38             : #endif
      39             : 
      40             : #ifdef PHP_MHASH_BC
      41             : struct mhash_bc_entry {
      42             :         char *mhash_name;
      43             :         char *hash_name;
      44             :         int value;
      45             : };
      46             : 
      47             : #define MHASH_NUM_ALGOS 34
      48             : 
      49             : static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
      50             :         {"CRC32", "crc32", 0},
      51             :         {"MD5", "md5", 1},
      52             :         {"SHA1", "sha1", 2},
      53             :         {"HAVAL256", "haval256,3", 3},
      54             :         {NULL, NULL, 4},
      55             :         {"RIPEMD160", "ripemd160", 5},
      56             :         {NULL, NULL, 6},
      57             :         {"TIGER", "tiger192,3", 7},
      58             :         {"GOST", "gost", 8},
      59             :         {"CRC32B", "crc32b", 9},
      60             :         {"HAVAL224", "haval224,3", 10},
      61             :         {"HAVAL192", "haval192,3", 11},
      62             :         {"HAVAL160", "haval160,3", 12},
      63             :         {"HAVAL128", "haval128,3", 13},
      64             :         {"TIGER128", "tiger128,3", 14},
      65             :         {"TIGER160", "tiger160,3", 15},
      66             :         {"MD4", "md4", 16},
      67             :         {"SHA256", "sha256", 17},
      68             :         {"ADLER32", "adler32", 18},
      69             :         {"SHA224", "sha224", 19},
      70             :         {"SHA512", "sha512", 20},
      71             :         {"SHA384", "sha384", 21},
      72             :         {"WHIRLPOOL", "whirlpool", 22},
      73             :         {"RIPEMD128", "ripemd128", 23},
      74             :         {"RIPEMD256", "ripemd256", 24},
      75             :         {"RIPEMD320", "ripemd320", 25},
      76             :         {NULL, NULL, 26}, /* support needs to be added for snefru 128 */
      77             :         {"SNEFRU256", "snefru256", 27},
      78             :         {"MD2", "md2", 28},
      79             :         {"FNV132", "fnv132", 29},
      80             :         {"FNV1A32", "fnv1a32", 30},
      81             :         {"FNV164", "fnv164", 31},
      82             :         {"FNV1A64", "fnv1a64", 32},
      83             :         {"JOAAT", "joaat", 33},
      84             : };
      85             : #endif
      86             : 
      87             : /* Hash Registry Access */
      88             : 
      89        1345 : PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, size_t algo_len) /* {{{ */
      90             : {
      91        1345 :         char *lower = zend_str_tolower_dup(algo, algo_len);
      92        1345 :         php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, lower, algo_len);
      93        1345 :         efree(lower);
      94             : 
      95        1345 :         return ops;
      96             : }
      97             : /* }}} */
      98             : 
      99      939458 : PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops) /* {{{ */
     100             : {
     101      939458 :         int algo_len = strlen(algo);
     102      939458 :         char *lower = zend_str_tolower_dup(algo, algo_len);
     103      939458 :         zend_hash_str_add_ptr(&php_hash_hashtable, lower, algo_len, (void *) ops);
     104      939458 :         efree(lower);
     105      939458 : }
     106             : /* }}} */
     107             : 
     108          87 : PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_context) /* {{{ */
     109             : {
     110          87 :         php_hash_ops *hash_ops = (php_hash_ops *)ops;
     111             : 
     112          87 :         memcpy(dest_context, orig_context, hash_ops->context_size);
     113          87 :         return SUCCESS;
     114             : }
     115             : /* }}} */
     116             : 
     117             : /* Userspace */
     118             : 
     119        1162 : static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
     120             : {
     121             :         zend_string *digest;
     122             :         char *algo, *data;
     123             :         size_t algo_len, data_len;
     124        1162 :         zend_bool raw_output = raw_output_default;
     125             :         const php_hash_ops *ops;
     126             :         void *context;
     127        1162 :         php_stream *stream = NULL;
     128             : 
     129        1162 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", &algo, &algo_len, &data, &data_len, &raw_output) == FAILURE) {
     130           5 :                 return;
     131             :         }
     132             : 
     133        1157 :         ops = php_hash_fetch_ops(algo, algo_len);
     134        1157 :         if (!ops) {
     135           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown hashing algorithm: %s", algo);
     136           2 :                 RETURN_FALSE;
     137             :         }
     138        1155 :         if (isfilename) {
     139          27 :                 if (CHECK_NULL_PATH(data, data_len)) {
     140           0 :                         RETURN_FALSE;
     141             :                 }
     142          27 :                 stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, DEFAULT_CONTEXT);
     143          27 :                 if (!stream) {
     144             :                         /* Stream will report errors opening file */
     145           1 :                         RETURN_FALSE;
     146             :                 }
     147             :         }
     148             : 
     149        1154 :         context = emalloc(ops->context_size);
     150        1154 :         ops->hash_init(context);
     151             : 
     152        1154 :         if (isfilename) {
     153             :                 char buf[1024];
     154             :                 int n;
     155             : 
     156          78 :                 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
     157          26 :                         ops->hash_update(context, (unsigned char *) buf, n);
     158             :                 }
     159          26 :                 php_stream_close(stream);
     160             :         } else {
     161        1128 :                 ops->hash_update(context, (unsigned char *) data, data_len);
     162             :         }
     163             : 
     164        2308 :         digest = zend_string_alloc(ops->digest_size, 0);
     165        1154 :         ops->hash_final((unsigned char *) digest->val, context);
     166        1154 :         efree(context);
     167             : 
     168        1154 :         if (raw_output) {
     169          18 :                 digest->val[ops->digest_size] = 0;
     170          18 :                 RETURN_STR(digest);
     171             :         } else {
     172        2272 :                 zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
     173             : 
     174        1136 :                 php_hash_bin2hex(hex_digest->val, (unsigned char *) digest->val, ops->digest_size);
     175        1136 :                 hex_digest->val[2 * ops->digest_size] = 0;
     176             :                 zend_string_release(digest);
     177        1136 :                 RETURN_STR(hex_digest);
     178             :         }
     179             : }
     180             : /* }}} */
     181             : 
     182             : /* {{{ proto string hash(string algo, string data[, bool raw_output = false])
     183             : Generate a hash of a given input string
     184             : Returns lowercase hexits by default */
     185        1120 : PHP_FUNCTION(hash)
     186             : {
     187        1120 :         php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
     188        1120 : }
     189             : /* }}} */
     190             : 
     191             : /* {{{ proto string hash_file(string algo, string filename[, bool raw_output = false])
     192             : Generate a hash of a given file
     193             : Returns lowercase hexits by default */
     194          30 : PHP_FUNCTION(hash_file)
     195             : {
     196          30 :         php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
     197          30 : }
     198             : /* }}} */
     199             : 
     200         110 : static inline void php_hash_string_xor_char(unsigned char *out, const unsigned char *in, const unsigned char xor_with, const int length) {
     201             :         int i;
     202        6750 :         for (i=0; i < length; i++) {
     203        6640 :                 out[i] = in[i] ^ xor_with;
     204             :         }
     205         110 : }
     206             : 
     207       24570 : static inline void php_hash_string_xor(unsigned char *out, const unsigned char *in, const unsigned char *xor_with, const int length) {
     208             :         int i;
     209      663390 :         for (i=0; i < length; i++) {
     210      638820 :                 out[i] = in[i] ^ xor_with[i];
     211             :         }
     212       24570 : }
     213             : 
     214          55 : static inline void php_hash_hmac_prep_key(unsigned char *K, const php_hash_ops *ops, void *context, const unsigned char *key, const size_t key_len) {
     215          55 :         memset(K, 0, ops->block_size);
     216          55 :         if (key_len > ops->block_size) {
     217             :                 /* Reduce the key first */
     218           6 :                 ops->hash_init(context);
     219           6 :                 ops->hash_update(context, key, key_len);
     220           6 :                 ops->hash_final(K, context);
     221             :         } else {
     222          49 :                 memcpy(K, key, key_len);
     223             :         }
     224             :         /* XOR the key with 0x36 to get the ipad) */
     225          55 :         php_hash_string_xor_char(K, K, 0x36, ops->block_size);
     226          55 : }
     227             : 
     228       49233 : static inline void php_hash_hmac_round(unsigned char *final, const php_hash_ops *ops, void *context, const unsigned char *key, const unsigned char *data, const zend_long data_size) {
     229       49233 :         ops->hash_init(context);
     230       49233 :         ops->hash_update(context, key, ops->block_size);
     231       49233 :         ops->hash_update(context, data, data_size);
     232       49233 :         ops->hash_final(final, context);
     233       49233 : }
     234             : 
     235          56 : static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
     236             : {
     237             :         zend_string *digest;
     238             :         char *algo, *data, *key;
     239             :         unsigned char *K;
     240             :         size_t algo_len, data_len, key_len;
     241          56 :         zend_bool raw_output = raw_output_default;
     242             :         const php_hash_ops *ops;
     243             :         void *context;
     244          56 :         php_stream *stream = NULL;
     245             : 
     246          56 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sss|b", &algo, &algo_len, &data, &data_len, 
     247             :                                                                                                                                   &key, &key_len, &raw_output) == FAILURE) {
     248          11 :                 return;
     249             :         }
     250             : 
     251          45 :         ops = php_hash_fetch_ops(algo, algo_len);
     252          45 :         if (!ops) {
     253           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown hashing algorithm: %s", algo);
     254           2 :                 RETURN_FALSE;
     255             :         }
     256          43 :         if (isfilename) {
     257          21 :                 stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, DEFAULT_CONTEXT);
     258          21 :                 if (!stream) {
     259             :                         /* Stream will report errors opening file */
     260           0 :                         RETURN_FALSE;
     261             :                 }
     262             :         }
     263             : 
     264          43 :         context = emalloc(ops->context_size);
     265             : 
     266          43 :         K = emalloc(ops->block_size);
     267          86 :         digest = zend_string_alloc(ops->digest_size, 0);
     268             : 
     269          43 :         php_hash_hmac_prep_key(K, ops, context, (unsigned char *) key, key_len);                
     270             : 
     271          43 :         if (isfilename) {
     272             :                 char buf[1024];
     273             :                 int n;
     274          21 :                 ops->hash_init(context);
     275          21 :                 ops->hash_update(context, K, ops->block_size);
     276          63 :                 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
     277          21 :                         ops->hash_update(context, (unsigned char *) buf, n);
     278             :                 }
     279          21 :                 php_stream_close(stream);
     280          21 :                 ops->hash_final((unsigned char *) digest->val, context);
     281             :         } else {
     282          22 :                 php_hash_hmac_round((unsigned char *) digest->val, ops, context, K, (unsigned char *) data, data_len);
     283             :         }
     284             : 
     285          43 :         php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
     286             : 
     287          43 :         php_hash_hmac_round((unsigned char *) digest->val, ops, context, K, (unsigned char *) digest->val, ops->digest_size);
     288             : 
     289             :         /* Zero the key */
     290          43 :         ZEND_SECURE_ZERO(K, ops->block_size);
     291          43 :         efree(K);
     292          43 :         efree(context);
     293             : 
     294          43 :         if (raw_output) {
     295           6 :                 digest->val[ops->digest_size] = 0;
     296           6 :                 RETURN_STR(digest);
     297             :         } else {
     298          74 :                 zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
     299             : 
     300          37 :                 php_hash_bin2hex(hex_digest->val, (unsigned char *) digest->val, ops->digest_size);
     301          37 :                 hex_digest->val[2 * ops->digest_size] = 0;
     302             :                 zend_string_release(digest);
     303          37 :                 RETURN_STR(hex_digest);
     304             :         }
     305             : }
     306             : /* }}} */
     307             : 
     308             : /* {{{ proto string hash_hmac(string algo, string data, string key[, bool raw_output = false])
     309             : Generate a hash of a given input string with a key using HMAC
     310             : Returns lowercase hexits by default */
     311          27 : PHP_FUNCTION(hash_hmac)
     312             : {
     313          27 :         php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
     314          27 : }
     315             : /* }}} */
     316             : 
     317             : /* {{{ proto string hash_hmac_file(string algo, string filename, string key[, bool raw_output = false])
     318             : Generate a hash of a given file with a key using HMAC
     319             : Returns lowercase hexits by default */
     320          29 : PHP_FUNCTION(hash_hmac_file)
     321             : {
     322          29 :         php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
     323          29 : }
     324             : /* }}} */
     325             : 
     326             : 
     327             : /* {{{ proto resource hash_init(string algo[, int options, string key])
     328             : Initialize a hashing context */
     329          96 : PHP_FUNCTION(hash_init)
     330             : {
     331          96 :         char *algo, *key = NULL;
     332          96 :         size_t algo_len, key_len = 0;
     333          96 :         int argc = ZEND_NUM_ARGS();
     334          96 :         zend_long options = 0;
     335             :         void *context;
     336             :         const php_hash_ops *ops;
     337             :         php_hash_data *hash;
     338             : 
     339          96 :         if (zend_parse_parameters(argc TSRMLS_CC, "s|ls", &algo, &algo_len, &options, &key, &key_len) == FAILURE) {
     340           0 :                 return;
     341             :         }
     342             : 
     343          96 :         ops = php_hash_fetch_ops(algo, algo_len);
     344          96 :         if (!ops) {
     345           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown hashing algorithm: %s", algo);
     346           0 :                 RETURN_FALSE;
     347             :         }
     348             : 
     349          99 :         if (options & PHP_HASH_HMAC &&
     350           3 :                 key_len <= 0) {
     351             :                 /* Note: a zero length key is no key at all */
     352           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "HMAC requested without a key");
     353           0 :                 RETURN_FALSE;
     354             :         }
     355             : 
     356          96 :         context = emalloc(ops->context_size);
     357          96 :         ops->hash_init(context);
     358             : 
     359          96 :         hash = emalloc(sizeof(php_hash_data));
     360          96 :         hash->ops = ops;
     361          96 :         hash->context = context;
     362          96 :         hash->options = options;
     363          96 :         hash->key = NULL;
     364             : 
     365          96 :         if (options & PHP_HASH_HMAC) {
     366           3 :                 char *K = emalloc(ops->block_size);
     367             :                 int i;
     368             : 
     369           3 :                 memset(K, 0, ops->block_size);
     370             : 
     371           3 :                 if (key_len > ops->block_size) {
     372             :                         /* Reduce the key first */
     373           1 :                         ops->hash_update(context, (unsigned char *) key, key_len);
     374           1 :                         ops->hash_final((unsigned char *) K, context);
     375             :                         /* Make the context ready to start over */
     376           1 :                         ops->hash_init(context);
     377             :                 } else {
     378           2 :                         memcpy(K, key, key_len);
     379             :                 }
     380             :                         
     381             :                 /* XOR ipad */
     382         135 :                 for(i=0; i < ops->block_size; i++) {
     383         132 :                         K[i] ^= 0x36;
     384             :                 }
     385           3 :                 ops->hash_update(context, (unsigned char *) K, ops->block_size);
     386           3 :                 hash->key = (unsigned char *) K;
     387             :         }
     388             : 
     389          96 :         ZEND_REGISTER_RESOURCE(return_value, hash, php_hash_le_hash);
     390             : }
     391             : /* }}} */
     392             : 
     393             : /* {{{ proto bool hash_update(resource context, string data)
     394             : Pump data into the hashing algorithm */
     395         140 : PHP_FUNCTION(hash_update)
     396             : {
     397             :         zval *zhash;
     398             :         php_hash_data *hash;
     399             :         char *data;
     400             :         size_t data_len;
     401             : 
     402         140 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zhash, &data, &data_len) == FAILURE) {
     403           0 :                 return;
     404             :         }
     405             : 
     406         140 :         ZEND_FETCH_RESOURCE(hash, php_hash_data*, zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
     407             : 
     408         140 :         hash->ops->hash_update(hash->context, (unsigned char *) data, data_len);
     409             : 
     410         140 :         RETURN_TRUE;
     411             : }
     412             : /* }}} */
     413             : 
     414             : /* {{{ proto int hash_update_stream(resource context, resource handle[, integer length])
     415             : Pump data into the hashing algorithm from an open stream */
     416           0 : PHP_FUNCTION(hash_update_stream)
     417             : {
     418             :         zval *zhash, *zstream;
     419             :         php_hash_data *hash;
     420           0 :         php_stream *stream = NULL;
     421           0 :         zend_long length = -1, didread = 0;
     422             : 
     423           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr|l", &zhash, &zstream, &length) == FAILURE) {
     424           0 :                 return;
     425             :         }
     426             : 
     427           0 :         ZEND_FETCH_RESOURCE(hash, php_hash_data*, zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
     428           0 :         php_stream_from_zval(stream, zstream);
     429             : 
     430           0 :         while (length) {
     431             :                 char buf[1024];
     432           0 :                 zend_long n, toread = 1024;
     433             : 
     434           0 :                 if (length > 0 && toread > length) {
     435           0 :                         toread = length;
     436             :                 }
     437             : 
     438           0 :                 if ((n = php_stream_read(stream, buf, toread)) <= 0) {
     439             :                         /* Nada mas */
     440           0 :                         RETURN_LONG(didread);
     441             :                 }
     442           0 :                 hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
     443           0 :                 length -= n;
     444           0 :                 didread += n;
     445             :         } 
     446             : 
     447           0 :         RETURN_LONG(didread);
     448             : }
     449             : /* }}} */
     450             : 
     451             : /* {{{ proto bool hash_update_file(resource context, string filename[, resource context])
     452             : Pump data into the hashing algorithm from a file */
     453           0 : PHP_FUNCTION(hash_update_file)
     454             : {
     455           0 :         zval *zhash, *zcontext = NULL;
     456             :         php_hash_data *hash;
     457             :         php_stream_context *context;
     458             :         php_stream *stream;
     459             :         char *filename, buf[1024];
     460             :         size_t filename_len, n;
     461             : 
     462           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|r", &zhash, &filename, &filename_len, &zcontext) == FAILURE) {
     463           0 :                 return;
     464             :         }
     465             : 
     466           0 :         ZEND_FETCH_RESOURCE(hash, php_hash_data*, zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
     467           0 :         context = php_stream_context_from_zval(zcontext, 0);
     468             : 
     469           0 :         stream = php_stream_open_wrapper_ex(filename, "rb", REPORT_ERRORS, NULL, context);
     470           0 :         if (!stream) {
     471             :                 /* Stream will report errors opening file */
     472           0 :                 RETURN_FALSE;
     473             :         }
     474             : 
     475           0 :         while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
     476           0 :                 hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
     477             :         }
     478           0 :         php_stream_close(stream);
     479             : 
     480           0 :         RETURN_TRUE;
     481             : }
     482             : /* }}} */
     483             : 
     484             : /* {{{ proto string hash_final(resource context[, bool raw_output=false])
     485             : Output resulting digest */
     486         189 : PHP_FUNCTION(hash_final)
     487             : {
     488             :         zval *zhash;
     489             :         php_hash_data *hash;
     490         189 :         zend_bool raw_output = 0;
     491             :         zend_string *digest;
     492             :         int digest_len;
     493             : 
     494         189 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|b", &zhash, &raw_output) == FAILURE) {
     495           0 :                 return;
     496             :         }
     497             : 
     498         189 :         ZEND_FETCH_RESOURCE(hash, php_hash_data*, zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
     499             : 
     500         189 :         digest_len = hash->ops->digest_size;
     501         378 :         digest = zend_string_alloc(digest_len, 0);
     502         189 :         hash->ops->hash_final((unsigned char *) digest->val, hash->context);
     503         189 :         if (hash->options & PHP_HASH_HMAC) {
     504             :                 int i;
     505             : 
     506             :                 /* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
     507         145 :                 for(i=0; i < hash->ops->block_size; i++) {
     508         140 :                         hash->key[i] ^= 0x6A;
     509             :                 }
     510             : 
     511             :                 /* Feed this result into the outter hash */
     512           5 :                 hash->ops->hash_init(hash->context);
     513           5 :                 hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size);
     514           5 :                 hash->ops->hash_update(hash->context, (unsigned char *) digest->val, hash->ops->digest_size);
     515           5 :                 hash->ops->hash_final((unsigned char *) digest->val, hash->context);
     516             : 
     517             :                 /* Zero the key */
     518           5 :                 ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
     519           5 :                 efree(hash->key);
     520           5 :                 hash->key = NULL;
     521             :         }
     522         189 :         digest->val[digest_len] = 0;
     523         189 :         efree(hash->context);
     524         189 :         hash->context = NULL;
     525             : 
     526             :         //???
     527             :         //??? /* zend_list_REAL_delete() */
     528             :         //??? if (zend_hash_index_find(&EG(regular_list), Z_RESVAL_P(zhash), (void *) &le)==SUCCESS) {
     529             :         //???   /* This is a hack to avoid letting the resource hide elsewhere (like in separated vars)
     530             :         //???           FETCH_RESOURCE is intelligent enough to handle dealing with any issues this causes */
     531             :         //???   le->refcount = 1;
     532             :         //??? } /* FAILURE is not an option */
     533         189 :         zend_list_close(Z_RES_P(zhash));
     534             : 
     535         189 :         if (raw_output) {
     536           0 :                 RETURN_STR(digest);
     537             :         } else {
     538         378 :                 zend_string *hex_digest = zend_string_safe_alloc(digest_len, 2, 0, 0);
     539             : 
     540         189 :                 php_hash_bin2hex(hex_digest->val, (unsigned char *) digest->val, digest_len);
     541         189 :                 hex_digest->val[2 * digest_len] = 0;
     542             :                 zend_string_release(digest);
     543         189 :                 RETURN_STR(hex_digest);
     544             :         }
     545             : }
     546             : /* }}} */
     547             : 
     548             : /* {{{ proto resource hash_copy(resource context)
     549             : Copy hash resource */
     550          97 : PHP_FUNCTION(hash_copy)
     551             : {
     552             :         zval *zhash;
     553             :         php_hash_data *hash, *copy_hash;
     554             :         void *context;
     555             :         int res;
     556             : 
     557          97 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zhash) == FAILURE) {
     558           2 :                 return;
     559             :         }
     560             : 
     561          95 :         ZEND_FETCH_RESOURCE(hash, php_hash_data*, zhash, -1, PHP_HASH_RESNAME, php_hash_le_hash);
     562             : 
     563             : 
     564          95 :         context = emalloc(hash->ops->context_size);
     565          95 :         hash->ops->hash_init(context);
     566             : 
     567          95 :         res = hash->ops->hash_copy(hash->ops, hash->context, context);
     568          95 :         if (res != SUCCESS) {
     569           0 :                 efree(context);
     570           0 :                 RETURN_FALSE;
     571             :         }
     572             : 
     573          95 :         copy_hash = emalloc(sizeof(php_hash_data));
     574          95 :         copy_hash->ops = hash->ops;
     575          95 :         copy_hash->context = context;
     576          95 :         copy_hash->options = hash->options;
     577          95 :         copy_hash->key = ecalloc(1, hash->ops->block_size);
     578          95 :         if (hash->key) {
     579           2 :                 memcpy(copy_hash->key, hash->key, hash->ops->block_size);
     580             :         }
     581          95 :         ZEND_REGISTER_RESOURCE(return_value, copy_hash, php_hash_le_hash);
     582             : }
     583             : /* }}} */
     584             : 
     585             : /* {{{ proto array hash_algos(void)
     586             : Return a list of registered hashing algorithms */
     587           2 : PHP_FUNCTION(hash_algos)
     588             : {
     589             :         zend_string *str;
     590             : 
     591           2 :         array_init(return_value);
     592         186 :         ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
     593          92 :                 add_next_index_str(return_value, zend_string_copy(str));
     594             :         } ZEND_HASH_FOREACH_END();
     595           2 : }
     596             : /* }}} */
     597             : 
     598             : /* {{{ proto string hash_pbkdf2(string algo, string password, string salt, int iterations [, int length = 0, bool raw_output = false])
     599             : Generate a PBKDF2 hash of the given password and salt
     600             : Returns lowercase hexits by default */
     601          21 : PHP_FUNCTION(hash_pbkdf2)
     602             : {
     603             :         zend_string *returnval;
     604          21 :         char *algo, *salt, *pass = NULL;
     605          21 :         unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
     606          21 :         zend_long loops, i, j, iterations, digest_length = 0, length = 0;
     607          21 :         size_t algo_len, pass_len, salt_len = 0;
     608          21 :         zend_bool raw_output = 0;
     609             :         const php_hash_ops *ops;
     610             :         void *context;
     611             : 
     612          21 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sssl|lb", &algo, &algo_len, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output) == FAILURE) {
     613           5 :                 return;
     614             :         }
     615             : 
     616          16 :         ops = php_hash_fetch_ops(algo, algo_len);
     617          16 :         if (!ops) {
     618           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown hashing algorithm: %s", algo);
     619           1 :                 RETURN_FALSE;
     620             :         }
     621             : 
     622          15 :         if (iterations <= 0) {
     623           2 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
     624           2 :                 RETURN_FALSE;
     625             :         }
     626             : 
     627          13 :         if (length < 0) {
     628           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
     629           1 :                 RETURN_FALSE;
     630             :         }
     631             : 
     632          12 :         if (salt_len > INT_MAX - 4) {
     633           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
     634           0 :                 RETURN_FALSE;
     635             :         }
     636             : 
     637          12 :         context = emalloc(ops->context_size);
     638          12 :         ops->hash_init(context);
     639             : 
     640          12 :         K1 = emalloc(ops->block_size);
     641          12 :         K2 = emalloc(ops->block_size);
     642          12 :         digest = emalloc(ops->digest_size);
     643          12 :         temp = emalloc(ops->digest_size);
     644             : 
     645             :         /* Setup Keys that will be used for all hmac rounds */
     646          12 :         php_hash_hmac_prep_key(K1, ops, context, (unsigned char *) pass, pass_len);
     647             :         /* Convert K1 to opad -- 0x6A = 0x36 ^ 0x5C */
     648          12 :         php_hash_string_xor_char(K2, K1, 0x6A, ops->block_size);
     649             : 
     650             :         /* Setup Main Loop to build a long enough result */
     651          12 :         if (length == 0) {
     652           4 :                 length = ops->digest_size;
     653           4 :                 if (!raw_output) {
     654           3 :                         length = length * 2;
     655             :                 }
     656             :         }
     657          12 :         digest_length = length;
     658          12 :         if (!raw_output) {
     659           7 :                 digest_length = (zend_long) ceil((float) length / 2.0);
     660             :         }
     661             : 
     662          12 :         loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
     663             : 
     664          12 :         result = safe_emalloc(loops, ops->digest_size, 0);
     665             : 
     666          12 :         computed_salt = safe_emalloc(salt_len, 1, 4);
     667          12 :         memcpy(computed_salt, (unsigned char *) salt, salt_len);
     668             : 
     669          26 :         for (i = 1; i <= loops; i++) {
     670             :                 /* digest = hash_hmac(salt + pack('N', i), password) { */
     671             : 
     672             :                 /* pack("N", i) */
     673          14 :                 computed_salt[salt_len] = (unsigned char) (i >> 24);
     674          14 :                 computed_salt[salt_len + 1] = (unsigned char) ((i & 0xFF0000) >> 16);
     675          14 :                 computed_salt[salt_len + 2] = (unsigned char) ((i & 0xFF00) >> 8);
     676          14 :                 computed_salt[salt_len + 3] = (unsigned char) (i & 0xFF);
     677             : 
     678          14 :                 php_hash_hmac_round(digest, ops, context, K1, computed_salt, (zend_long) salt_len + 4);
     679          14 :                 php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
     680             :                 /* } */
     681             : 
     682             :                 /* temp = digest */
     683          14 :                 memcpy(temp, digest, ops->digest_size);
     684             : 
     685             :                 /* 
     686             :                  * Note that the loop starting at 1 is intentional, since we've already done
     687             :                  * the first round of the algorithm.
     688             :                  */
     689       24584 :                 for (j = 1; j < iterations; j++) {
     690             :                         /* digest = hash_hmac(digest, password) { */
     691       24570 :                         php_hash_hmac_round(digest, ops, context, K1, digest, ops->digest_size);
     692       24570 :                         php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
     693             :                         /* } */
     694             :                         /* temp ^= digest */
     695       24570 :                         php_hash_string_xor(temp, temp, digest, ops->digest_size);
     696             :                 }
     697             :                 /* result += temp */
     698          14 :                 memcpy(result + ((i - 1) * ops->digest_size), temp, ops->digest_size);
     699             :         }
     700             :         /* Zero potentially sensitive variables */
     701          12 :         ZEND_SECURE_ZERO(K1, ops->block_size);
     702          12 :         ZEND_SECURE_ZERO(K2, ops->block_size);
     703          12 :         ZEND_SECURE_ZERO(computed_salt, salt_len + 4);
     704          12 :         efree(K1);
     705          12 :         efree(K2);
     706          12 :         efree(computed_salt);
     707          12 :         efree(context);
     708          12 :         efree(digest);
     709          12 :         efree(temp);
     710             : 
     711          24 :         returnval = zend_string_alloc(length, 0);
     712          12 :         if (raw_output) {
     713           5 :                 memcpy(returnval->val, result, length);
     714             :         } else {
     715           7 :                 php_hash_bin2hex(returnval->val, result, digest_length);
     716             :         }
     717          12 :         returnval->val[length] = 0;
     718          12 :         efree(result);
     719          12 :         RETURN_STR(returnval);
     720             : }
     721             : /* }}} */
     722             : 
     723             : /* {{{ proto bool hash_equals(string known_string, string user_string)
     724             :    Compares two strings using the same time whether they're equal or not.
     725             :    A difference in length will leak */
     726          13 : PHP_FUNCTION(hash_equals)
     727             : {
     728             :         zval *known_zval, *user_zval;
     729             :         char *known_str, *user_str;
     730          13 :         int result = 0, j;
     731             : 
     732          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz", &known_zval, &user_zval) == FAILURE) {
     733           0 :                 return;
     734             :         }
     735             : 
     736             :         /* We only allow comparing string to prevent unexpected results. */
     737          26 :         if (Z_TYPE_P(known_zval) != IS_STRING) {
     738           5 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
     739           5 :                 RETURN_FALSE;
     740             :         }
     741             : 
     742          16 :         if (Z_TYPE_P(user_zval) != IS_STRING) {
     743           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
     744           1 :                 RETURN_FALSE;
     745             :         }
     746             : 
     747           7 :         if (Z_STRLEN_P(known_zval) != Z_STRLEN_P(user_zval)) {
     748           4 :                 RETURN_FALSE;
     749             :         }
     750             : 
     751           3 :         known_str = Z_STRVAL_P(known_zval);
     752           3 :         user_str = Z_STRVAL_P(user_zval);
     753             : 
     754             :         /* This is security sensitive code. Do not optimize this for speed. */
     755          15 :         for (j = 0; j < Z_STRLEN_P(known_zval); j++) {
     756          12 :                 result |= known_str[j] ^ user_str[j];
     757             :         }
     758             : 
     759           3 :         RETURN_BOOL(0 == result);
     760             : }
     761             : /* }}} */
     762             : 
     763             : /* Module Housekeeping */
     764             : 
     765         191 : static void php_hash_dtor(zend_resource *rsrc TSRMLS_DC) /* {{{ */
     766             : {
     767         191 :         php_hash_data *hash = (php_hash_data*)rsrc->ptr;
     768             : 
     769             :         /* Just in case the algo has internally allocated resources */
     770         191 :         if (hash->context) {
     771           2 :                 unsigned char *dummy = emalloc(hash->ops->digest_size);
     772           2 :                 hash->ops->hash_final(dummy, hash->context);
     773           2 :                 efree(dummy);
     774           2 :                 efree(hash->context);
     775             :         }
     776             : 
     777         191 :         if (hash->key) {
     778          93 :                 memset(hash->key, 0, hash->ops->block_size);
     779          93 :                 efree(hash->key);
     780             :         }
     781         191 :         efree(hash);
     782         191 : }
     783             : /* }}} */
     784             : 
     785             : #define PHP_HASH_HAVAL_REGISTER(p,b)    php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
     786             : 
     787             : #ifdef PHP_MHASH_BC
     788             : 
     789         143 : PHP_MINFO_FUNCTION(mhash)
     790             : {
     791         143 :         php_info_print_table_start();
     792         143 :         php_info_print_table_row(2, "MHASH support", "Enabled");
     793         143 :         php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
     794         143 :         php_info_print_table_end();
     795         143 : }
     796             : 
     797             : zend_module_entry mhash_module_entry = {
     798             :         STANDARD_MODULE_HEADER,
     799             :         "mhash",
     800             :         NULL,
     801             :         NULL,
     802             :         NULL,
     803             :         NULL,
     804             :         NULL,
     805             :         PHP_MINFO(mhash),
     806             :         NO_VERSION_YET,
     807             :         STANDARD_MODULE_PROPERTIES,
     808             : };
     809             : 
     810       20423 : static void mhash_init(INIT_FUNC_ARGS)
     811             : {
     812             :         char buf[128];
     813             :         int len;
     814       20423 :         int algo_number = 0;
     815             : 
     816      714805 :         for (algo_number = 0; algo_number < MHASH_NUM_ALGOS; algo_number++) {
     817      694382 :                 struct mhash_bc_entry algorithm = mhash_to_hash[algo_number];
     818      694382 :                 if (algorithm.mhash_name == NULL) {
     819       61269 :                         continue;
     820             :                 }
     821             : 
     822      633113 :                 len = slprintf(buf, 127, "MHASH_%s", algorithm.mhash_name, strlen(algorithm.mhash_name));
     823      633113 :                 zend_register_long_constant(buf, len, algorithm.value, CONST_CS | CONST_PERSISTENT, module_number TSRMLS_CC);
     824             :         }
     825       20423 :         zend_register_internal_module(&mhash_module_entry TSRMLS_CC);
     826       20423 : }
     827             : 
     828             : /* {{{ proto string mhash(int hash, string data [, string key])
     829             :    Hash data with hash */
     830          12 : PHP_FUNCTION(mhash)
     831             : {
     832             :         zval *z_algorithm;
     833             :         zend_long algorithm;
     834             : 
     835          12 :         if (zend_parse_parameters(1 TSRMLS_CC, "z", &z_algorithm) == FAILURE) {
     836           0 :                 return;
     837             :         }
     838             : 
     839          12 :         SEPARATE_ZVAL(z_algorithm);
     840          24 :         convert_to_long_ex(z_algorithm);
     841          12 :         algorithm = Z_LVAL_P(z_algorithm);
     842             : 
     843             :         /* need to convert the first parameter from int constant to string algorithm name */
     844          12 :         if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
     845          12 :                 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
     846          12 :                 if (algorithm_lookup.hash_name) {
     847          24 :                         ZVAL_STRING(z_algorithm, algorithm_lookup.hash_name);
     848             :                 }
     849             :         }
     850             : 
     851          12 :         if (ZEND_NUM_ARGS() == 3) {
     852           0 :                 php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
     853          12 :         } else if (ZEND_NUM_ARGS() == 2) {
     854          12 :                 php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
     855             :         } else {
     856           0 :                 WRONG_PARAM_COUNT;
     857             :         }
     858             : }
     859             : /* }}} */
     860             : 
     861             : /* {{{ proto string mhash_get_hash_name(int hash)
     862             :    Gets the name of hash */
     863          34 : PHP_FUNCTION(mhash_get_hash_name)
     864             : {
     865             :         zend_long algorithm;
     866             : 
     867          34 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &algorithm) == FAILURE) {
     868           0 :                 return;
     869             :         }
     870             : 
     871          34 :         if (algorithm >= 0 && algorithm  < MHASH_NUM_ALGOS) {
     872          34 :                 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
     873          34 :                 if (algorithm_lookup.mhash_name) {
     874          62 :                         RETURN_STRING(algorithm_lookup.mhash_name);
     875             :                 }
     876             :         }
     877           3 :         RETURN_FALSE;
     878             : }
     879             : /* }}} */
     880             : 
     881             : /* {{{ proto int mhash_count(void)
     882             :    Gets the number of available hashes */
     883           1 : PHP_FUNCTION(mhash_count)
     884             : {
     885           1 :         if (zend_parse_parameters_none() == FAILURE) {
     886           0 :                 return;
     887             :         }
     888           1 :         RETURN_LONG(MHASH_NUM_ALGOS - 1);
     889             : }
     890             : /* }}} */
     891             : 
     892             : /* {{{ proto int mhash_get_block_size(int hash)
     893             :    Gets the block size of hash */
     894          17 : PHP_FUNCTION(mhash_get_block_size)
     895             : {
     896             :         zend_long algorithm;
     897             : 
     898          17 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &algorithm) == FAILURE) {
     899           0 :                 return;
     900             :         }
     901          17 :         RETVAL_FALSE;
     902             : 
     903          17 :         if (algorithm >= 0 && algorithm  < MHASH_NUM_ALGOS) {
     904          17 :                 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
     905          17 :                 if (algorithm_lookup.mhash_name) {
     906          17 :                         const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
     907          17 :                         if (ops) {
     908          17 :                                 RETVAL_LONG(ops->digest_size);
     909             :                         }
     910             :                 }
     911             :         }
     912             : }
     913             : /* }}} */
     914             : 
     915             : #define SALT_SIZE 8
     916             : 
     917             : /* {{{ proto string mhash_keygen_s2k(int hash, string input_password, string salt, int bytes)
     918             :    Generates a key using hash functions */
     919          11 : PHP_FUNCTION(mhash_keygen_s2k)
     920             : {
     921             :         zend_long algorithm, l_bytes;
     922             :         int bytes;
     923             :         char *password, *salt;
     924             :         size_t password_len, salt_len;
     925             :         char padded_salt[SALT_SIZE];
     926             : 
     927          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) {
     928           0 :                 return;
     929             :         }
     930             : 
     931          11 :         bytes = (int)l_bytes;
     932          11 :         if (bytes <= 0){
     933           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "the byte parameter must be greater than 0");
     934           0 :                 RETURN_FALSE;
     935             :         }
     936             : 
     937          11 :         salt_len = MIN(salt_len, SALT_SIZE);
     938             : 
     939          11 :         memcpy(padded_salt, salt, salt_len);
     940          11 :         if (salt_len < SALT_SIZE) {
     941           0 :                 memset(padded_salt + salt_len, 0, SALT_SIZE - salt_len);
     942             :         }
     943          11 :         salt_len = SALT_SIZE;
     944             : 
     945          11 :         RETVAL_FALSE;
     946          11 :         if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
     947          11 :                 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
     948          11 :                 if (algorithm_lookup.mhash_name) {
     949          11 :                         const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
     950          11 :                         if (ops) {
     951          11 :                                 unsigned char null = '\0';
     952             :                                 void *context;
     953             :                                 char *key, *digest;
     954          11 :                                 int i = 0, j = 0;
     955          11 :                                 int block_size = ops->digest_size;
     956          11 :                                 int times = bytes / block_size;
     957          11 :                                 if (bytes % block_size  != 0) times++;
     958             : 
     959          11 :                                 context = emalloc(ops->context_size);
     960          11 :                                 ops->hash_init(context);
     961             : 
     962          11 :                                 key = ecalloc(1, times * block_size);
     963          11 :                                 digest = emalloc(ops->digest_size + 1);
     964             : 
     965         105 :                                 for (i = 0; i < times; i++) {
     966          94 :                                         ops->hash_init(context);
     967             : 
     968         783 :                                         for (j=0;j<i;j++) {
     969         689 :                                                 ops->hash_update(context, &null, 1);
     970             :                                         }
     971          94 :                                         ops->hash_update(context, (unsigned char *)padded_salt, salt_len);
     972          94 :                                         ops->hash_update(context, (unsigned char *)password, password_len);
     973          94 :                                         ops->hash_final((unsigned char *)digest, context);
     974          94 :                                         memcpy( &key[i*block_size], digest, block_size);
     975             :                                 }
     976             : 
     977          22 :                                 RETVAL_STRINGL(key, bytes);
     978          11 :                                 ZEND_SECURE_ZERO(key, bytes);
     979          11 :                                 efree(digest);
     980          11 :                                 efree(context);
     981          11 :                                 efree(key);
     982             :                         }
     983             :                 }
     984             :         }
     985             : }
     986             : /* }}} */
     987             : 
     988             : #endif
     989             : 
     990             : /* {{{ PHP_MINIT_FUNCTION
     991             :  */
     992       20423 : PHP_MINIT_FUNCTION(hash)
     993             : {
     994       20423 :         php_hash_le_hash = zend_register_list_destructors_ex(php_hash_dtor, NULL, PHP_HASH_RESNAME, module_number);
     995             : 
     996       20423 :         zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
     997             : 
     998       20423 :         php_hash_register_algo("md2",                 &php_hash_md2_ops);
     999       20423 :         php_hash_register_algo("md4",                 &php_hash_md4_ops);
    1000       20423 :         php_hash_register_algo("md5",                 &php_hash_md5_ops);
    1001       20423 :         php_hash_register_algo("sha1",                        &php_hash_sha1_ops);
    1002       20423 :         php_hash_register_algo("sha224",              &php_hash_sha224_ops);
    1003       20423 :         php_hash_register_algo("sha256",              &php_hash_sha256_ops);
    1004       20423 :         php_hash_register_algo("sha384",              &php_hash_sha384_ops);
    1005       20423 :         php_hash_register_algo("sha512",              &php_hash_sha512_ops);
    1006       20423 :         php_hash_register_algo("ripemd128",           &php_hash_ripemd128_ops);
    1007       20423 :         php_hash_register_algo("ripemd160",           &php_hash_ripemd160_ops);
    1008       20423 :         php_hash_register_algo("ripemd256",           &php_hash_ripemd256_ops);
    1009       20423 :         php_hash_register_algo("ripemd320",           &php_hash_ripemd320_ops);
    1010       20423 :         php_hash_register_algo("whirlpool",           &php_hash_whirlpool_ops);
    1011       20423 :         php_hash_register_algo("tiger128,3",  &php_hash_3tiger128_ops);
    1012       20423 :         php_hash_register_algo("tiger160,3",  &php_hash_3tiger160_ops);
    1013       20423 :         php_hash_register_algo("tiger192,3",  &php_hash_3tiger192_ops);
    1014       20423 :         php_hash_register_algo("tiger128,4",  &php_hash_4tiger128_ops);
    1015       20423 :         php_hash_register_algo("tiger160,4",  &php_hash_4tiger160_ops);
    1016       20423 :         php_hash_register_algo("tiger192,4",  &php_hash_4tiger192_ops);
    1017       20423 :         php_hash_register_algo("snefru",              &php_hash_snefru_ops);
    1018       20423 :         php_hash_register_algo("snefru256",           &php_hash_snefru_ops);
    1019       20423 :         php_hash_register_algo("gost",                        &php_hash_gost_ops);
    1020       20423 :         php_hash_register_algo("gost-crypto",         &php_hash_gost_crypto_ops);
    1021       20423 :         php_hash_register_algo("adler32",             &php_hash_adler32_ops);
    1022       20423 :         php_hash_register_algo("crc32",                       &php_hash_crc32_ops);
    1023       20423 :         php_hash_register_algo("crc32b",              &php_hash_crc32b_ops);
    1024       20423 :         php_hash_register_algo("fnv132",              &php_hash_fnv132_ops);
    1025       20423 :         php_hash_register_algo("fnv1a32",             &php_hash_fnv1a32_ops);
    1026       20423 :         php_hash_register_algo("fnv164",              &php_hash_fnv164_ops);
    1027       20423 :         php_hash_register_algo("fnv1a64",             &php_hash_fnv1a64_ops);
    1028       20423 :         php_hash_register_algo("joaat",                       &php_hash_joaat_ops);
    1029             : 
    1030       20423 :         PHP_HASH_HAVAL_REGISTER(3,128);
    1031       20423 :         PHP_HASH_HAVAL_REGISTER(3,160);
    1032       20423 :         PHP_HASH_HAVAL_REGISTER(3,192);
    1033       20423 :         PHP_HASH_HAVAL_REGISTER(3,224);
    1034       20423 :         PHP_HASH_HAVAL_REGISTER(3,256);
    1035             : 
    1036       20423 :         PHP_HASH_HAVAL_REGISTER(4,128);
    1037       20423 :         PHP_HASH_HAVAL_REGISTER(4,160);
    1038       20423 :         PHP_HASH_HAVAL_REGISTER(4,192);
    1039       20423 :         PHP_HASH_HAVAL_REGISTER(4,224);
    1040       20423 :         PHP_HASH_HAVAL_REGISTER(4,256);
    1041             : 
    1042       20423 :         PHP_HASH_HAVAL_REGISTER(5,128);
    1043       20423 :         PHP_HASH_HAVAL_REGISTER(5,160);
    1044       20423 :         PHP_HASH_HAVAL_REGISTER(5,192);
    1045       20423 :         PHP_HASH_HAVAL_REGISTER(5,224);
    1046       20423 :         PHP_HASH_HAVAL_REGISTER(5,256);
    1047             : 
    1048       20423 :         REGISTER_LONG_CONSTANT("HASH_HMAC",           PHP_HASH_HMAC,  CONST_CS | CONST_PERSISTENT);
    1049             : 
    1050             : #ifdef PHP_MHASH_BC
    1051       20423 :         mhash_init(INIT_FUNC_ARGS_PASSTHRU);
    1052             : #endif
    1053             : 
    1054       20423 :         return SUCCESS;
    1055             : }
    1056             : /* }}} */
    1057             : 
    1058             : /* {{{ PHP_MSHUTDOWN_FUNCTION
    1059             :  */
    1060       20457 : PHP_MSHUTDOWN_FUNCTION(hash)
    1061             : {
    1062       20457 :         zend_hash_destroy(&php_hash_hashtable);
    1063             : 
    1064       20457 :         return SUCCESS;
    1065             : }
    1066             : /* }}} */
    1067             : 
    1068             : /* {{{ PHP_MINFO_FUNCTION
    1069             :  */
    1070         143 : PHP_MINFO_FUNCTION(hash)
    1071             : {
    1072             :         char buffer[2048];
    1073             :         zend_string *str;
    1074         143 :         char *s = buffer, *e = s + sizeof(buffer);
    1075             : 
    1076       13299 :         ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
    1077        6578 :                 s += slprintf(s, e - s, "%s ", str->val);
    1078             :         } ZEND_HASH_FOREACH_END();
    1079         143 :         *s = 0;
    1080             : 
    1081         143 :         php_info_print_table_start();
    1082         143 :         php_info_print_table_row(2, "hash support", "enabled");
    1083         143 :         php_info_print_table_row(2, "Hashing Engines", buffer);
    1084         143 :         php_info_print_table_end();
    1085         143 : }
    1086             : /* }}} */
    1087             : 
    1088             : /* {{{ arginfo */
    1089             : #ifdef PHP_HASH_MD5_NOT_IN_CORE
    1090             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5, 0, 0, 1)
    1091             :         ZEND_ARG_INFO(0, str)
    1092             :         ZEND_ARG_INFO(0, raw_output)
    1093             : ZEND_END_ARG_INFO()
    1094             : 
    1095             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5_file, 0, 0, 1)
    1096             :         ZEND_ARG_INFO(0, filename)
    1097             :         ZEND_ARG_INFO(0, raw_output)
    1098             : ZEND_END_ARG_INFO()
    1099             : #endif
    1100             : 
    1101             : #ifdef PHP_HASH_SHA1_NOT_IN_CORE
    1102             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1, 0, 0, 1)
    1103             :         ZEND_ARG_INFO(0, str)
    1104             :         ZEND_ARG_INFO(0, raw_output)
    1105             : ZEND_END_ARG_INFO()
    1106             : 
    1107             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1_file, 0, 0, 1)
    1108             :         ZEND_ARG_INFO(0, filename)
    1109             :         ZEND_ARG_INFO(0, raw_output)
    1110             : ZEND_END_ARG_INFO()
    1111             : #endif
    1112             : 
    1113             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash, 0, 0, 2)
    1114             :         ZEND_ARG_INFO(0, algo)
    1115             :         ZEND_ARG_INFO(0, data)
    1116             :         ZEND_ARG_INFO(0, raw_output)
    1117             : ZEND_END_ARG_INFO()
    1118             : 
    1119             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_file, 0, 0, 2)
    1120             :         ZEND_ARG_INFO(0, algo)
    1121             :         ZEND_ARG_INFO(0, filename)
    1122             :         ZEND_ARG_INFO(0, raw_output)
    1123             : ZEND_END_ARG_INFO()
    1124             : 
    1125             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac, 0, 0, 3)
    1126             :         ZEND_ARG_INFO(0, algo)
    1127             :         ZEND_ARG_INFO(0, data)
    1128             :         ZEND_ARG_INFO(0, key)
    1129             :         ZEND_ARG_INFO(0, raw_output)
    1130             : ZEND_END_ARG_INFO()
    1131             : 
    1132             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac_file, 0, 0, 3)
    1133             :         ZEND_ARG_INFO(0, algo)
    1134             :         ZEND_ARG_INFO(0, filename)
    1135             :         ZEND_ARG_INFO(0, key)
    1136             :         ZEND_ARG_INFO(0, raw_output)
    1137             : ZEND_END_ARG_INFO()
    1138             : 
    1139             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_init, 0, 0, 1)
    1140             :         ZEND_ARG_INFO(0, algo)
    1141             :         ZEND_ARG_INFO(0, options)
    1142             :         ZEND_ARG_INFO(0, key)
    1143             : ZEND_END_ARG_INFO()
    1144             : 
    1145             : ZEND_BEGIN_ARG_INFO(arginfo_hash_update, 0)
    1146             :         ZEND_ARG_INFO(0, context)
    1147             :         ZEND_ARG_INFO(0, data)
    1148             : ZEND_END_ARG_INFO()
    1149             : 
    1150             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_stream, 0, 0, 2)
    1151             :         ZEND_ARG_INFO(0, context)
    1152             :         ZEND_ARG_INFO(0, handle)
    1153             :         ZEND_ARG_INFO(0, length)
    1154             : ZEND_END_ARG_INFO()
    1155             : 
    1156             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_file, 0, 0, 2)
    1157             :         ZEND_ARG_INFO(0, context)
    1158             :         ZEND_ARG_INFO(0, filename)
    1159             :         ZEND_ARG_INFO(0, context)
    1160             : ZEND_END_ARG_INFO()
    1161             : 
    1162             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_final, 0, 0, 1)
    1163             :         ZEND_ARG_INFO(0, context)
    1164             :         ZEND_ARG_INFO(0, raw_output)
    1165             : ZEND_END_ARG_INFO()
    1166             : 
    1167             : ZEND_BEGIN_ARG_INFO(arginfo_hash_copy, 0)
    1168             :         ZEND_ARG_INFO(0, context)
    1169             : ZEND_END_ARG_INFO()
    1170             : 
    1171             : ZEND_BEGIN_ARG_INFO(arginfo_hash_algos, 0)
    1172             : ZEND_END_ARG_INFO()
    1173             : 
    1174             : ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_pbkdf2, 0, 0, 4)
    1175             :         ZEND_ARG_INFO(0, algo)
    1176             :         ZEND_ARG_INFO(0, password)
    1177             :         ZEND_ARG_INFO(0, salt)
    1178             :         ZEND_ARG_INFO(0, iterations)
    1179             :         ZEND_ARG_INFO(0, length)
    1180             :         ZEND_ARG_INFO(0, raw_output)
    1181             : ZEND_END_ARG_INFO()
    1182             : 
    1183             : ZEND_BEGIN_ARG_INFO(arginfo_hash_equals, 0)
    1184             :         ZEND_ARG_INFO(0, known_string)
    1185             :         ZEND_ARG_INFO(0, user_string)
    1186             : ZEND_END_ARG_INFO()
    1187             : 
    1188             : /* BC Land */
    1189             : #ifdef PHP_MHASH_BC
    1190             : ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_block_size, 0)
    1191             :         ZEND_ARG_INFO(0, hash)
    1192             : ZEND_END_ARG_INFO()
    1193             : 
    1194             : ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_hash_name, 0)
    1195             :         ZEND_ARG_INFO(0, hash)
    1196             : ZEND_END_ARG_INFO()
    1197             : 
    1198             : ZEND_BEGIN_ARG_INFO(arginfo_mhash_keygen_s2k, 0)
    1199             :         ZEND_ARG_INFO(0, hash)
    1200             :         ZEND_ARG_INFO(0, input_password)
    1201             :         ZEND_ARG_INFO(0, salt)
    1202             :         ZEND_ARG_INFO(0, bytes)
    1203             : ZEND_END_ARG_INFO()
    1204             : 
    1205             : ZEND_BEGIN_ARG_INFO(arginfo_mhash_count, 0)
    1206             : ZEND_END_ARG_INFO()
    1207             : 
    1208             : ZEND_BEGIN_ARG_INFO_EX(arginfo_mhash, 0, 0, 2)
    1209             :         ZEND_ARG_INFO(0, hash)
    1210             :         ZEND_ARG_INFO(0, data)
    1211             :         ZEND_ARG_INFO(0, key)
    1212             : ZEND_END_ARG_INFO()
    1213             : #endif
    1214             : 
    1215             : /* }}} */
    1216             : 
    1217             : /* {{{ hash_functions[]
    1218             :  */
    1219             : const zend_function_entry hash_functions[] = {
    1220             :         PHP_FE(hash,                                                                    arginfo_hash)
    1221             :         PHP_FE(hash_file,                                                               arginfo_hash_file)
    1222             : 
    1223             :         PHP_FE(hash_hmac,                                                               arginfo_hash_hmac)
    1224             :         PHP_FE(hash_hmac_file,                                                  arginfo_hash_hmac_file)
    1225             : 
    1226             :         PHP_FE(hash_init,                                                               arginfo_hash_init)
    1227             :         PHP_FE(hash_update,                                                             arginfo_hash_update)
    1228             :         PHP_FE(hash_update_stream,                                              arginfo_hash_update_stream)
    1229             :         PHP_FE(hash_update_file,                                                arginfo_hash_update_file)
    1230             :         PHP_FE(hash_final,                                                              arginfo_hash_final)
    1231             :         PHP_FE(hash_copy,                                                               arginfo_hash_copy)
    1232             : 
    1233             :         PHP_FE(hash_algos,                                                              arginfo_hash_algos)
    1234             :         PHP_FE(hash_pbkdf2,                                                             arginfo_hash_pbkdf2)
    1235             :         PHP_FE(hash_equals,                                                             arginfo_hash_equals)
    1236             : 
    1237             :         /* BC Land */
    1238             : #ifdef PHP_HASH_MD5_NOT_IN_CORE
    1239             :         PHP_NAMED_FE(md5, php_if_md5,                                   arginfo_hash_md5)
    1240             :         PHP_NAMED_FE(md5_file, php_if_md5_file,                 arginfo_hash_md5_file)
    1241             : #endif /* PHP_HASH_MD5_NOT_IN_CORE */
    1242             : 
    1243             : #ifdef PHP_HASH_SHA1_NOT_IN_CORE
    1244             :         PHP_NAMED_FE(sha1, php_if_sha1,                                 arginfo_hash_sha1)
    1245             :         PHP_NAMED_FE(sha1_file, php_if_sha1_file,               arginfo_hash_sha1_file)
    1246             : #endif /* PHP_HASH_SHA1_NOT_IN_CORE */
    1247             : 
    1248             : #ifdef PHP_MHASH_BC
    1249             :         PHP_FE(mhash_keygen_s2k, arginfo_mhash_keygen_s2k)
    1250             :         PHP_FE(mhash_get_block_size, arginfo_mhash_get_block_size)
    1251             :         PHP_FE(mhash_get_hash_name, arginfo_mhash_get_hash_name)
    1252             :         PHP_FE(mhash_count, arginfo_mhash_count)
    1253             :         PHP_FE(mhash, arginfo_mhash)
    1254             : #endif
    1255             : 
    1256             :         PHP_FE_END
    1257             : };
    1258             : /* }}} */
    1259             : 
    1260             : /* {{{ hash_module_entry
    1261             :  */
    1262             : zend_module_entry hash_module_entry = {
    1263             : #if ZEND_MODULE_API_NO >= 20010901
    1264             :         STANDARD_MODULE_HEADER,
    1265             : #endif
    1266             :         PHP_HASH_EXTNAME,
    1267             :         hash_functions,
    1268             :         PHP_MINIT(hash),
    1269             :         PHP_MSHUTDOWN(hash),
    1270             :         NULL, /* RINIT */
    1271             :         NULL, /* RSHUTDOWN */
    1272             :         PHP_MINFO(hash),
    1273             : #if ZEND_MODULE_API_NO >= 20010901
    1274             :         PHP_HASH_EXTVER, /* Replace with version number for your extension */
    1275             : #endif
    1276             :         STANDARD_MODULE_PROPERTIES
    1277             : };
    1278             : /* }}} */
    1279             : 
    1280             : #ifdef COMPILE_DL_HASH
    1281             : ZEND_GET_MODULE(hash)
    1282             : #endif
    1283             : 
    1284             : /*
    1285             :  * Local variables:
    1286             :  * tab-width: 4
    1287             :  * c-basic-offset: 4
    1288             :  * End:
    1289             :  * vim600: noet sw=4 ts=4 fdm=marker
    1290             :  * vim<600: noet sw=4 ts=4
    1291             :  */
    1292             : 

Generated by: LCOV version 1.10

Generated at Thu, 30 Oct 2014 07:41:30 +0000 (27 hours ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.