xref: /PHP-8.0/ext/sodium/tests/crypto_stream.phpt (revision 9d236d63)
1--TEST--
2Check for libsodium stream
3--SKIPIF--
4<?php if (!extension_loaded("sodium")) print "skip"; ?>
5--FILE--
6<?php
7$nonce = random_bytes(SODIUM_CRYPTO_STREAM_NONCEBYTES);
8$key = sodium_crypto_stream_keygen();
9
10$len = 100;
11$stream = sodium_crypto_stream($len, $nonce, $key);
12var_dump(strlen($stream));
13
14$stream2 = sodium_crypto_stream($len, $nonce, $key);
15
16$nonce = random_bytes(SODIUM_CRYPTO_STREAM_NONCEBYTES);
17$stream3 = sodium_crypto_stream($len, $nonce, $key);
18
19$key = sodium_crypto_stream_keygen();
20$stream4 = sodium_crypto_stream($len, $nonce, $key);
21
22var_dump($stream === $stream2);
23var_dump($stream !== $stream3);
24var_dump($stream !== $stream4);
25var_dump($stream2 !== $stream3);
26var_dump($stream2 !== $stream4);
27var_dump($stream3 !== $stream4);
28
29$stream5 = sodium_crypto_stream_xor($stream, $nonce, $key);
30var_dump($stream5 !== $stream);
31$stream6 = sodium_crypto_stream_xor($stream5, $nonce, $key);
32
33var_dump($stream6 === $stream);
34
35try {
36    sodium_crypto_stream($len, substr($nonce, 1), $key);
37} catch (SodiumException $ex) {
38    var_dump(true);
39}
40
41?>
42--EXPECT--
43int(100)
44bool(true)
45bool(true)
46bool(true)
47bool(true)
48bool(true)
49bool(true)
50bool(true)
51bool(true)
52bool(true)
53