hash.c 36 KB

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