@@ -345,23 +345,14 @@ PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long ma
345
345
346
346
/* Userspace */
347
347
348
- static void php_hash_do_hash (INTERNAL_FUNCTION_PARAMETERS , int isfilename , zend_bool raw_output_default ) /* {{{ */
349
- {
350
- zend_string * digest , * algo ;
351
- char * data ;
352
- size_t data_len ;
353
- zend_bool raw_output = raw_output_default ;
348
+ static void php_hash_do_hash (
349
+ zval * return_value , zend_string * algo , char * data , size_t data_len , zend_bool raw_output , bool isfilename
350
+ ) /* {{{ */ {
351
+ zend_string * digest ;
354
352
const php_hash_ops * ops ;
355
353
void * context ;
356
354
php_stream * stream = NULL ;
357
355
358
- ZEND_PARSE_PARAMETERS_START (2 , 3 )
359
- Z_PARAM_STR (algo )
360
- Z_PARAM_STRING (data , data_len )
361
- Z_PARAM_OPTIONAL
362
- Z_PARAM_BOOL (raw_output )
363
- ZEND_PARSE_PARAMETERS_END ();
364
-
365
356
ops = php_hash_fetch_ops (algo );
366
357
if (!ops ) {
367
358
zend_argument_value_error (1 , "must be a valid hashing algorithm" );
@@ -420,15 +411,39 @@ static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_
420
411
Returns lowercase hexits by default */
421
412
PHP_FUNCTION (hash )
422
413
{
423
- php_hash_do_hash (INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 , 0 );
414
+ zend_string * algo ;
415
+ char * data ;
416
+ size_t data_len ;
417
+ zend_bool raw_output = 0 ;
418
+
419
+ ZEND_PARSE_PARAMETERS_START (2 , 3 )
420
+ Z_PARAM_STR (algo )
421
+ Z_PARAM_STRING (data , data_len )
422
+ Z_PARAM_OPTIONAL
423
+ Z_PARAM_BOOL (raw_output )
424
+ ZEND_PARSE_PARAMETERS_END ();
425
+
426
+ php_hash_do_hash (return_value , algo , data , data_len , raw_output , 0 );
424
427
}
425
428
/* }}} */
426
429
427
430
/* {{{ Generate a hash of a given file
428
431
Returns lowercase hexits by default */
429
432
PHP_FUNCTION (hash_file )
430
433
{
431
- php_hash_do_hash (INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 , 0 );
434
+ zend_string * algo ;
435
+ char * data ;
436
+ size_t data_len ;
437
+ zend_bool raw_output = 0 ;
438
+
439
+ ZEND_PARSE_PARAMETERS_START (2 , 3 )
440
+ Z_PARAM_STR (algo )
441
+ Z_PARAM_STRING (data , data_len )
442
+ Z_PARAM_OPTIONAL
443
+ Z_PARAM_BOOL (raw_output )
444
+ ZEND_PARSE_PARAMETERS_END ();
445
+
446
+ php_hash_do_hash (return_value , algo , data , data_len , raw_output , 1 );
432
447
}
433
448
/* }}} */
434
449
@@ -467,22 +482,15 @@ static inline void php_hash_hmac_round(unsigned char *final, const php_hash_ops
467
482
ops -> hash_final (final , context );
468
483
}
469
484
470
- static void php_hash_do_hash_hmac (INTERNAL_FUNCTION_PARAMETERS , int isfilename , zend_bool raw_output_default ) /* {{{ */
471
- {
472
- zend_string * digest , * algo ;
473
- char * data , * key ;
485
+ static void php_hash_do_hash_hmac (
486
+ zval * return_value , zend_string * algo , char * data , size_t data_len , char * key , size_t key_len , zend_bool raw_output , bool isfilename
487
+ ) /* {{{ */ {
488
+ zend_string * digest ;
474
489
unsigned char * K ;
475
- size_t data_len , key_len ;
476
- zend_bool raw_output = raw_output_default ;
477
490
const php_hash_ops * ops ;
478
491
void * context ;
479
492
php_stream * stream = NULL ;
480
493
481
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "Sss|b" , & algo , & data , & data_len ,
482
- & key , & key_len , & raw_output ) == FAILURE ) {
483
- RETURN_THROWS ();
484
- }
485
-
486
494
ops = php_hash_fetch_ops (algo );
487
495
if (!ops || !ops -> is_crypto ) {
488
496
zend_argument_value_error (1 , "must be a valid cryptographic hashing algorithm" );
@@ -556,15 +564,33 @@ static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename,
556
564
Returns lowercase hexits by default */
557
565
PHP_FUNCTION (hash_hmac )
558
566
{
559
- php_hash_do_hash_hmac (INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 , 0 );
567
+ zend_string * algo ;
568
+ char * data , * key ;
569
+ size_t data_len , key_len ;
570
+ zend_bool raw_output = 0 ;
571
+
572
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "Sss|b" , & algo , & data , & data_len , & key , & key_len , & raw_output ) == FAILURE ) {
573
+ RETURN_THROWS ();
574
+ }
575
+
576
+ php_hash_do_hash_hmac (return_value , algo , data , data_len , key , key_len , raw_output , 0 );
560
577
}
561
578
/* }}} */
562
579
563
580
/* {{{ Generate a hash of a given file with a key using HMAC
564
581
Returns lowercase hexits by default */
565
582
PHP_FUNCTION (hash_hmac_file )
566
583
{
567
- php_hash_do_hash_hmac (INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 , 0 );
584
+ zend_string * algo ;
585
+ char * data , * key ;
586
+ size_t data_len , key_len ;
587
+ zend_bool raw_output = 0 ;
588
+
589
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "Sss|b" , & algo , & data , & data_len , & key , & key_len , & raw_output ) == FAILURE ) {
590
+ RETURN_THROWS ();
591
+ }
592
+
593
+ php_hash_do_hash_hmac (return_value , algo , data , data_len , key , key_len , raw_output , 1 );
568
594
}
569
595
/* }}} */
570
596
@@ -1163,29 +1189,27 @@ static void mhash_init(INIT_FUNC_ARGS)
1163
1189
/* {{{ Hash data with hash */
1164
1190
PHP_FUNCTION (mhash )
1165
1191
{
1166
- zval * z_algorithm ;
1167
1192
zend_long algorithm ;
1193
+ zend_string * algo = NULL ;
1194
+ char * data , * key = NULL ;
1195
+ size_t data_len , key_len = 0 ;
1168
1196
1169
- if (zend_parse_parameters (1 , "z " , & z_algorithm ) == FAILURE ) {
1197
+ if (zend_parse_parameters (ZEND_NUM_ARGS () , "ls|s! " , & algorithm , & data , & data_len , & key , & key_len ) == FAILURE ) {
1170
1198
RETURN_THROWS ();
1171
1199
}
1172
1200
1173
- algorithm = zval_get_long (z_algorithm );
1174
-
1175
1201
/* need to convert the first parameter from int constant to string algorithm name */
1176
1202
if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS ) {
1177
1203
struct mhash_bc_entry algorithm_lookup = mhash_to_hash [algorithm ];
1178
1204
if (algorithm_lookup .hash_name ) {
1179
- ZVAL_STRING ( z_algorithm , algorithm_lookup .hash_name );
1205
+ algo = zend_string_init ( algorithm_lookup . hash_name , strlen ( algorithm_lookup .hash_name ), 1 );
1180
1206
}
1181
1207
}
1182
1208
1183
- if (ZEND_NUM_ARGS () == 3 ) {
1184
- php_hash_do_hash_hmac (INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 , 1 );
1185
- } else if (ZEND_NUM_ARGS () == 2 ) {
1186
- php_hash_do_hash (INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 , 1 );
1209
+ if (key ) {
1210
+ php_hash_do_hash_hmac (return_value , algo , data , data_len , key , key_len , 1 , 0 );
1187
1211
} else {
1188
- WRONG_PARAM_COUNT ;
1212
+ php_hash_do_hash ( return_value , algo , data , data_len , 1 , 0 ) ;
1189
1213
}
1190
1214
}
1191
1215
/* }}} */
0 commit comments