]> granicus.if.org Git - php/commitdiff
Add hash_hmac_algos() for filtered is_crypto methods
authorSara Golemon <pollita@php.net>
Sun, 23 Jul 2017 19:14:31 +0000 (15:14 -0400)
committerSara Golemon <pollita@php.net>
Sun, 23 Jul 2017 19:17:09 +0000 (15:17 -0400)
ext/hash/hash.c
ext/hash/tests/hash_hmac_algos.phpt [new file with mode: 0644]

index e2e478fcda856cabadc832ef6fba6389ae12f90d..5932ad9c6a2f29a56189eadb2d6199a2cd01f8fe 100644 (file)
@@ -597,6 +597,22 @@ PHP_FUNCTION(hash_algos)
 }
 /* }}} */
 
+/* {{{ proto array hash_hmac_algos(void)
+Return a list of registered hashing algorithms suitable for hash_hmac() */
+PHP_FUNCTION(hash_hmac_algos)
+{
+       zend_string *str;
+       const php_hash_ops *ops;
+
+       array_init(return_value);
+       ZEND_HASH_FOREACH_STR_KEY_PTR(&php_hash_hashtable, str, ops) {
+               if (ops->is_crypto) {
+                       add_next_index_str(return_value, zend_string_copy(str));
+               }
+       } ZEND_HASH_FOREACH_END();
+}
+/* }}} */
+
 /* {{{ proto string hash_hkdf(string algo, string ikm [, int length = 0, string info = '', string salt = ''])
 RFC5869 HMAC-based key derivation function */
 PHP_FUNCTION(hash_hkdf)
@@ -1429,6 +1445,7 @@ const zend_function_entry hash_functions[] = {
        PHP_FE(hash_copy,                                                               arginfo_hash_copy)
 
        PHP_FE(hash_algos,                                                              arginfo_hash_algos)
+       PHP_FE(hash_hmac_algos,                                                 arginfo_hash_algos)
        PHP_FE(hash_pbkdf2,                                                             arginfo_hash_pbkdf2)
        PHP_FE(hash_equals,                                                             arginfo_hash_equals)
        PHP_FE(hash_hkdf,                                                               arginfo_hash_hkdf)
diff --git a/ext/hash/tests/hash_hmac_algos.phpt b/ext/hash/tests/hash_hmac_algos.phpt
new file mode 100644 (file)
index 0000000..89877b7
--- /dev/null
@@ -0,0 +1,57 @@
+--TEST--
+Test hash_hmac_algos() function : basic functionality 
+--SKIPIF--
+<?php extension_loaded('hash') or die('skip: hash extension not loaded.'); ?>
+--FILE--
+<?php
+
+print_r(hash_hmac_algos());
+
+--EXPECTF--
+Array
+(
+    [%d] => md2
+    [%d] => md4
+    [%d] => md5
+    [%d] => sha1
+    [%d] => sha224
+    [%d] => sha256
+    [%d] => sha384
+    [%d] => sha512/224
+    [%d] => sha512/256
+    [%d] => sha512
+    [%d] => sha3-224
+    [%d] => sha3-256
+    [%d] => sha3-384
+    [%d] => sha3-512
+    [%d] => ripemd128
+    [%d] => ripemd160
+    [%d] => ripemd256
+    [%d] => ripemd320
+    [%d] => whirlpool
+    [%d] => tiger128,3
+    [%d] => tiger160,3
+    [%d] => tiger192,3
+    [%d] => tiger128,4
+    [%d] => tiger160,4
+    [%d] => tiger192,4
+    [%d] => snefru
+    [%d] => snefru256
+    [%d] => gost
+    [%d] => gost-crypto
+    [%d] => haval128,3
+    [%d] => haval160,3
+    [%d] => haval192,3
+    [%d] => haval224,3
+    [%d] => haval256,3
+    [%d] => haval128,4
+    [%d] => haval160,4
+    [%d] => haval192,4
+    [%d] => haval224,4
+    [%d] => haval256,4
+    [%d] => haval128,5
+    [%d] => haval160,5
+    [%d] => haval192,5
+    [%d] => haval224,5
+    [%d] => haval256,5
+)