1--TEST--
2encrypt()
3--FILE--
4<?php
5require_once 'Crypt/XXTEA.php';
6
7$XXTEA = new Crypt_XXTEA();
8
9// key-plaintext tuples for string encryptiont test
10$tuples_string = array(
11    array('abcdefghijklmnop', '[encryption test]'),
12    array('gd2fk$2ds', 'PHP is a widely-used general-purpose scripting language'),
13    array('p', 'It is especially suited for Web development and can be embedded into HTML'),
14    array("\xb2\x93\x13\xe5\x96", 'PEAR is a framework and distribution system for reusable PHP components'),
15);
16
17// key-plaintext tuples for long integer array encryptiont test
18$tuples_array = array(
19    array(array(0, 0, 0, 0), array(0, 0)),
20    array(array(0), array(0, 0, 0, 0, 0)),
21);
22
23// the following key-plaintext tuples are from http://www.crypt.co.za/post/27
24$tuples_hex = array(
25    array('6a6f686e636b656e64616c6c6a6f686e', '4100000000000000'),
26    array('6a6f686e636b656e64616c6c6a6f686e', '4142000000000000'),
27    array('6a6f686e636b656e64616c6c6a6f686e', '4142430000000000'),
28    array('6a6f686e636b656e64616c6c6a6f686e', '4142434400000000'),
29    array('6a6f686e636b656e64616c6c6a6f686e', '4142434445000000'),
30    array('6a6f686e636b656e64616c6c6a6f686e', '4142434445460000'),
31    array('6a6f686e636b656e64616c6c6a6f686e', '4142434445464700'),
32    array('6a6f686e636b656e64616c6c6a6f686e', '4142434445464748'),
33    array('00000000000000000000000000000000', '0000000000000000'),
34    array('0102040810204080fffefcf8f0e0c080', '0000000000000000'),
35    array('9e3779b99b9773e9b979379e6b695156', 'ffffffffffffffff'),
36    array('0102040810204080fffefcf8f0e0c080', 'fffefcf8f0e0c080'),
37    array('ffffffffffffffffffffffffffffffff', '157c13a850ba5e57306d7791'),
38    array('9e3779b99b9773e9b979379e6b695156', '157c13a850ba5e57306d7791'),
39    array('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', '0102040810204080fffefcf8f0e0c080'),
40    array('9e3779b99b9773e9b979379e6b695156', '0102040810204080fffefcf8f0e0c080'),
41    array('0102040810204080fffefcf8f0e0c080', '157c13a850ba5e57306d77916fa2c37be1949616'),
42    array('9e3779b99b9773e9b979379e6b695156', '690342f45054a708c475c91db77761bc01b815fd2e4894d1'),
43    array('9e3779b99b9773e9b979379e6b695156', '3555da434c9d868a1431e73e73372fc0688e09ce11d00b6fd936a764'),
44    array('0102040810204080fffefcf8f0e0c080', 'db9af3c96e36a30c643c6e97f4d75b7a4b51a40e9d8759e581e3c40b341b4436')
45);
46
47$result = $XXTEA->encrypt(null);
48if (PEAR::isError($result)) {
49    echo $result->getMessage();
50} else {
51    echo base64_encode($result);
52}
53echo "\n";
54
55$XXTEA->setKey('abc');
56$result = $XXTEA->encrypt(null);
57if (PEAR::isError($result)) {
58    echo $result->getMessage();
59} else {
60    echo base64_encode($result);
61}
62echo "\n";
63
64foreach ($tuples_string as $tuple) {
65    $XXTEA->setKey($tuple[0]);
66    $result = $XXTEA->encrypt($tuple[1]);
67    echo base64_encode($result);
68    echo "\n";
69}
70
71foreach ($tuples_array as $tuple) {
72    $XXTEA->setKey($tuple[0]);
73    $result = $XXTEA->encrypt($tuple[1]);
74    echo 'array(' . implode(', ', $result) . ')';
75    echo "\n";
76}
77
78foreach ($tuples_hex as $tuple) {
79    $XXTEA->setKey(hex2long($tuple[0]));
80    $result = $XXTEA->encrypt(hex2long($tuple[1]));
81    echo long2hex($result);
82    echo "\n";
83}
84
85function hex2long($hex) {
86    return array_values(unpack('V*', pack('H*', $hex)));
87}
88
89function long2hex($arr) {
90    $len = count($arr);
91    $hex = '';
92    for ($i = 0; $i < $len; $i++) {
93        $hex .= pack('V', $arr[$i]);
94    }
95    return array_shift(unpack('H*', $hex));
96}
97
98?>
99--EXPECT--
100Secret key is undefined.
101The plain text must be a string or long integer array.
102UsFmrB/Q8zLl+5TIYicLk+mMgrj41jRB
1030ec5tIhpKCIiwdN05MynE+qbtjtD45pEUsaVGaKkHKlD2AewGrjAryZOjQIYLY20HZTbq6Y/YcCcNoAJ
104iRj+IHHKrz5yXL6w7PnvflYzHoDItfi7Eg8+dyaL79vPbCUyRw2eVCKnU7LO2zbxYzj1T+o2kTA+MMhX85ktjddmC6FuVafophvx4Qxc9LU=
105bmeLisYRXKwB6xSK1v10c/QUYJh/iAtEX9q/qX74k8A3l/jVd++Gb41N327ARdZVrUxOG24Rw46WeS8LcEpML585kbOglxY6SbmUaw==
106array(87491755, 1465748608)
107array(-1698224617, 1492735309, 566640726, 178473705, 1574735419)
108014e7a34874eeb29
109e9d39f636e9ed090
110d20ec51c06feaf0e
111b1551d6ffcd4b61b
1120ff91e518b9837e3
1137003fc98b6788a77
11493951ad360650022
115cdeb72b9c903ce52
116ab043705808c5d57
117d1e78be2c746728a
11867ed0ea8e8973fc5
1198c3707c01c7fccc4
120b2601cefb078b772abccba6a
121579016d143ed6247ac6710dd
122c0a19f06ebb0d63925aa27f74cc6b2d0
12301b815fd2e4894d13555da434c9d868a
12451f0ffeb46012a245e0c6c4fa097db27caec698d
125759e5b212ee58be734d610248e1daa1c9d0647d428b4f95a
1268e63ae7d8a119566990eb756f16abf94ff87359803ca12fbaa03fdfb
1275ef1b6e010a2227ba337374b59beffc5263503054745fb513000641e2c7dd107
128