xref: /php-src/ext/hash/tests/xxhash_secret.phpt (revision 7ae7b4e3)
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--EXPECTF--
50string(67) "xxh3: Only one of seed or secret is to be passed for initialization"
51
52Deprecated: hash_init(): Passing a secret of a type other than string is deprecated because it implicitly converts to a string, potentially hiding bugs in %s on line %d
53string(23) "exception in __toString"
54string(57) "xxh3: Secret length must be >= 136 bytes, 17 bytes passed"
558028aa834c03557a == 8028aa834c03557a == true
56string(69) "xxh128: Only one of seed or secret is to be passed for initialization"
57
58Deprecated: hash_init(): Passing a secret of a type other than string is deprecated because it implicitly converts to a string, potentially hiding bugs in %s on line %d
59string(23) "exception in __toString"
60string(59) "xxh128: Secret length must be >= 136 bytes, 17 bytes passed"
6154279097795e7218093a05d4d781cbb9 == 54279097795e7218093a05d4d781cbb9 == true
62