1--TEST--
2Check for libsodium stream
3--EXTENSIONS--
4sodium
5--SKIPIF--
6<?php if (!defined('SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES')) print "skip"; ?>
7--FILE--
8<?php
9$nonce = random_bytes(SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES);
10$key = sodium_crypto_stream_xchacha20_keygen();
11
12$len = 100;
13$stream = sodium_crypto_stream_xchacha20($len, $nonce, $key);
14var_dump(strlen($stream));
15
16$stream2 = sodium_crypto_stream_xchacha20($len, $nonce, $key);
17
18$nonce = random_bytes(SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES);
19$stream3 = sodium_crypto_stream_xchacha20($len, $nonce, $key);
20
21$key = sodium_crypto_stream_keygen();
22$stream4 = sodium_crypto_stream_xchacha20($len, $nonce, $key);
23
24var_dump($stream === $stream2);
25var_dump($stream !== $stream3);
26var_dump($stream !== $stream4);
27var_dump($stream2 !== $stream3);
28var_dump($stream2 !== $stream4);
29var_dump($stream3 !== $stream4);
30
31$stream5 = sodium_crypto_stream_xchacha20_xor($stream, $nonce, $key);
32var_dump($stream5 !== $stream);
33$stream6 = sodium_crypto_stream_xchacha20_xor($stream5, $nonce, $key);
34
35var_dump($stream6 === $stream);
36
37try {
38    sodium_crypto_stream_xchacha20(-1, $nonce, $key);
39} catch (SodiumException $ex) {
40    echo $ex->getMessage(), "\n";
41}
42try {
43    sodium_crypto_stream_xchacha20($len, substr($nonce, 1), $key);
44} catch (SodiumException $ex) {
45    echo $ex->getMessage(), "\n";
46}
47try {
48    sodium_crypto_stream_xchacha20($len, $nonce, substr($key, 1));
49} catch (SodiumException $ex) {
50    echo $ex->getMessage(), "\n";
51}
52try {
53    sodium_crypto_stream_xchacha20_xor($stream, substr($nonce, 1), $key);
54} catch (SodiumException $ex) {
55    echo $ex->getMessage(), "\n";
56}
57try {
58    sodium_crypto_stream_xchacha20_xor($stream, $nonce, substr($key, 1));
59} catch (SodiumException $ex) {
60    echo $ex->getMessage(), "\n";
61}
62
63?>
64--EXPECT--
65int(100)
66bool(true)
67bool(true)
68bool(true)
69bool(true)
70bool(true)
71bool(true)
72bool(true)
73bool(true)
74sodium_crypto_stream_xchacha20(): Argument #1 ($length) must be greater than 0
75sodium_crypto_stream_xchacha20(): Argument #2 ($nonce) must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long
76sodium_crypto_stream_xchacha20(): Argument #3 ($key) must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long
77sodium_crypto_stream_xchacha20_xor(): Argument #2 ($nonce) must be SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES bytes long
78sodium_crypto_stream_xchacha20_xor(): Argument #3 ($key) must be SODIUM_CRYPTO_STREAM_XCHACHA20_KEYBYTES bytes long
79