xref: /PHP-8.2/ext/hash/tests/xxhash_secret.phpt (revision 7463e70b)
1--TEST--
2Hash: xxHash secret
3--FILE--
4<?php
5
6class StringableThrowingClass {
7    public function __toString(): string {
8        throw new Exception('exception in __toString');
9        return '';
10    }
11}
12
13foreach (["xxh3", "xxh128"] as $a) {
14
15	//$secret = random_bytes(256);
16	$secret = str_repeat('a', 256);
17
18	try {
19		$ctx = hash_init($a, options: ["seed" => 24, "secret" => $secret]);
20	} catch (Throwable $e) {
21		var_dump($e->getMessage());
22	}
23
24	try {
25		$ctx = hash_init($a, options: ["secret" => new StringableThrowingClass()]);
26	} catch (Throwable $e) {
27		var_dump($e->getMessage());
28	}
29
30	try {
31		$ctx = hash_init($a, options: ["secret" => str_repeat('a', 17)]);
32	} catch (Throwable $e) {
33		var_dump($e->getMessage());
34	}
35
36	$ctx = hash_init($a, options: ["secret" => $secret]);
37	hash_update($ctx, "Lorem");
38	hash_update($ctx, " ipsum dolor");
39	hash_update($ctx, " sit amet,");
40	hash_update($ctx, " consectetur adipiscing elit.");
41	$h0 = hash_final($ctx);
42
43	$h1 = hash($a, "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", options: ["secret" => $secret]);
44	echo $h0 , " == ", $h1, " == ", (($h0 == $h1) ? "true" : "false"), "\n";
45
46}
47
48?>
49--EXPECT--
50string(67) "xxh3: Only one of seed or secret is to be passed for initialization"
51string(23) "exception in __toString"
52string(57) "xxh3: Secret length must be >= 136 bytes, 17 bytes passed"
538028aa834c03557a == 8028aa834c03557a == true
54string(69) "xxh128: Only one of seed or secret is to be passed for initialization"
55string(23) "exception in __toString"
56string(59) "xxh128: Secret length must be >= 136 bytes, 17 bytes passed"
5754279097795e7218093a05d4d781cbb9 == 54279097795e7218093a05d4d781cbb9 == true
58