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: 450 505 89.1 %
Date: 2014-08-04 Functions: 31 33 93.9 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:04 +0000 (27 days ago)

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