xref: /PHP-8.2/ext/hash/tests/hash_init_error.phpt (revision b10416a6)
1--TEST--
2Hash: hash_init() function - errors test
3--FILE--
4<?php
5echo "*** Testing hash_init(): error conditions ***\n";
6
7echo "\n-- Testing hash_init() function with unknown algorithms --\n";
8try {
9    var_dump(hash_init('dummy'));
10}
11catch (\Error $e) {
12    echo $e->getMessage() . "\n";
13}
14
15echo "\n-- Testing hash_init() function with HASH_HMAC and non-cryptographic algorithms --\n";
16try {
17    var_dump(hash_init('crc32', HASH_HMAC));
18}
19catch (\Error $e) {
20    echo $e->getMessage() . "\n";
21}
22
23echo "\n-- Testing hash_init() function with HASH_HMAC and no key --\n";
24try {
25    var_dump(hash_init('md5', HASH_HMAC));
26}
27catch (\Error $e) {
28    echo  $e->getMessage() . "\n";
29}
30
31try {
32    var_dump(hash_init('md5', HASH_HMAC, null));
33}
34catch (\Error $e) {
35    echo $e->getMessage() . "\n";
36}
37
38
39?>
40--EXPECTF--
41*** Testing hash_init(): error conditions ***
42
43-- Testing hash_init() function with unknown algorithms --
44hash_init(): Argument #1 ($algo) must be a valid hashing algorithm
45
46-- Testing hash_init() function with HASH_HMAC and non-cryptographic algorithms --
47hash_init(): Argument #1 ($algo) must be a cryptographic hashing algorithm if HMAC is requested
48
49-- Testing hash_init() function with HASH_HMAC and no key --
50hash_init(): Argument #3 ($key) cannot be empty when HMAC is requested
51
52Deprecated: hash_init(): Passing null to parameter #3 ($key) of type string is deprecated in %s on line %d
53hash_init(): Argument #3 ($key) cannot be empty when HMAC is requested
54