123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <?php
- use \ParagonIE\ConstantTime\Base32;
- use \ParagonIE\ConstantTime\Base32Hex;
- use \ParagonIE\ConstantTime\Base64;
- use \ParagonIE\ConstantTime\Base64DotSlash;
- use \ParagonIE\ConstantTime\Base64DotSlashOrdered;
- use \ParagonIE\ConstantTime\Base64UrlSafe;
- use \ParagonIE\ConstantTime\Encoding;
- use \ParagonIE\ConstantTime\Hex;
- class EncodingTest extends PHPUnit_Framework_TestCase
- {
- public function testBase32Encode()
- {
- $this->assertSame(
- Encoding::base32Encode("\x00"),
- 'aa======'
- );
- $this->assertSame(
- Encoding::base32Encode("\x00\x00"),
- 'aaaa===='
- );
- $this->assertSame(
- Encoding::base32Encode("\x00\x00\x00"),
- 'aaaaa==='
- );
- $this->assertSame(
- Encoding::base32Encode("\x00\x00\x00\x00"),
- 'aaaaaaa='
- );
- $this->assertSame(
- Encoding::base32Encode("\x00\x00\x00\x00\x00"),
- 'aaaaaaaa'
- );
- $this->assertSame(
- Encoding::base32Encode("\x00\x00\x0F\xFF\xFF"),
- 'aaaa7777'
- );
- $this->assertSame(
- Encoding::base32Encode("\xFF\xFF\xF0\x00\x00"),
- '7777aaaa'
- );
- $this->assertSame(
- Encoding::base32Encode("\xce\x73\x9c\xe7\x39"),
- 'zzzzzzzz'
- );
- $this->assertSame(
- Encoding::base32Encode("\xd6\xb5\xad\x6b\x5a"),
- '22222222'
- );
- $this->assertSame(
- Base32::encodeUpper("\x00"),
- 'AA======'
- );
- $this->assertSame(
- Base32::encodeUpper("\x00\x00"),
- 'AAAA===='
- );
- $this->assertSame(
- Base32::encodeUpper("\x00\x00\x00"),
- 'AAAAA==='
- );
- $this->assertSame(
- Base32::encodeUpper("\x00\x00\x00\x00"),
- 'AAAAAAA='
- );
- $this->assertSame(
- Base32::encodeUpper("\x00\x00\x00\x00\x00"),
- 'AAAAAAAA'
- );
- $this->assertSame(
- Base32::encodeUpper("\x00\x00\x0F\xFF\xFF"),
- 'AAAA7777'
- );
- $this->assertSame(
- Base32::encodeUpper("\xFF\xFF\xF0\x00\x00"),
- '7777AAAA'
- );
- $this->assertSame(
- Base32::encodeUpper("\xce\x73\x9c\xe7\x39"),
- 'ZZZZZZZZ'
- );
- $this->assertSame(
- Base32::encodeUpper("\xd6\xb5\xad\x6b\x5a"),
- '22222222'
- );
- }
- public function testBase32Hex()
- {
- $this->assertSame(
- Base32Hex::encode("\x00"),
- '00======'
- );
- $this->assertSame(
- Base32Hex::encode("\x00\x00"),
- '0000===='
- );
- $this->assertSame(
- Base32Hex::encode("\x00\x00\x00"),
- '00000==='
- );
- $this->assertSame(
- Base32Hex::encode("\x00\x00\x00\x00"),
- '0000000='
- );
- $this->assertSame(
- Base32Hex::encode("\x00\x00\x00\x00\x00"),
- '00000000'
- );
- $this->assertSame(
- Base32Hex::encode("\x00\x00\x0F\xFF\xFF"),
- '0000vvvv'
- );
- $this->assertSame(
- Base32Hex::encode("\xFF\xFF\xF0\x00\x00"),
- 'vvvv0000'
- );
- }
- /**
- * Based on test vectors from RFC 4648
- */
- public function testBase32Decode()
- {
- $this->assertSame(
- "\x00\x00\x00\x00\x00\x00",
- Encoding::base32Decode('aaaaaaaaaa======')
- );
- $this->assertSame(
- "\x00\x00\x00\x00\x00\x00\x00",
- Encoding::base32Decode('aaaaaaaaaaaa====')
- );
- $this->assertSame(
- "\x00\x00\x00\x00\x00\x00\x00\x00",
- Encoding::base32Decode('aaaaaaaaaaaaa===')
- );
- $this->assertSame(
- "\x00\x00\x00\x00\x00\x00\x00\x00\x00",
- Encoding::base32Decode('aaaaaaaaaaaaaaa=')
- );
- $this->assertSame(
- "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
- Encoding::base32Decode('aaaaaaaaaaaaaaaa')
- );
- $this->assertSame(
- "\x00",
- Encoding::base32Decode('aa======')
- );
- $this->assertSame(
- "\x00\x00",
- Encoding::base32Decode('aaaa====')
- );
- $this->assertSame(
- "\x00\x00\x00",
- Encoding::base32Decode('aaaaa===')
- );
- $this->assertSame(
- "\x00\x00\x00\x00",
- Encoding::base32Decode('aaaaaaa=')
- );
- $this->assertSame(
- "\x00\x00\x00\x00\x00",
- Encoding::base32Decode('aaaaaaaa')
- );
- $this->assertSame(
- "\x00\x00\x0F\xFF\xFF",
- Encoding::base32Decode('aaaa7777')
- );
- $this->assertSame(
- "\xFF\xFF\xF0\x00\x00",
- Encoding::base32Decode('7777aaaa')
- );
- $this->assertSame(
- "\xce\x73\x9c\xe7\x39",
- Encoding::base32Decode('zzzzzzzz')
- );
- $this->assertSame(
- "\xd6\xb5\xad\x6b\x5a",
- Encoding::base32Decode('22222222')
- );
- $this->assertSame(
- 'foobar',
- Encoding::base32Decode('mzxw6ytboi======')
- );
- $rand = random_bytes(9);
- $enc = Encoding::base32Encode($rand);
- $this->assertSame(
- Encoding::base32Encode($rand),
- Encoding::base32Encode(Encoding::base32Decode($enc))
- );
- $this->assertSame(
- $rand,
- Encoding::base32Decode($enc)
- );
- }
- /**
- * @covers Encoding::hexDecode()
- * @covers Encoding::hexEncode()
- * @covers Encoding::base32Decode()
- * @covers Encoding::base32Encode()
- * @covers Encoding::base64Decode()
- * @covers Encoding::base64Encode()
- * @covers Encoding::base64DotSlashDecode()
- * @covers Encoding::base64DotSlashEncode()
- * @covers Encoding::base64DotSlashOrderedDecode()
- * @covers Encoding::base64DotSlashOrderedEncode()
- */
- public function testBasicEncoding()
- {
- // Re-run the test at least 3 times for each length
- for ($j = 0; $j < 3; ++$j) {
- for ($i = 1; $i < 84; ++$i) {
- $rand = random_bytes($i);
- $enc = Encoding::hexEncode($rand);
- $this->assertSame(
- \bin2hex($rand),
- $enc,
- "Hex Encoding - Length: " . $i
- );
- $this->assertSame(
- $rand,
- Encoding::hexDecode($enc),
- "Hex Encoding - Length: " . $i
- );
- // Uppercase variant:
- $enc = Hex::encodeUpper($rand);
- $this->assertSame(
- \strtoupper(\bin2hex($rand)),
- $enc,
- "Hex Encoding - Length: " . $i
- );
- $this->assertSame(
- $rand,
- Hex::decode($enc),
- "HexUpper Encoding - Length: " . $i
- );
- $enc = Encoding::base32Encode($rand);
- $this->assertSame(
- $rand,
- Encoding::base32Decode($enc),
- "Base32 Encoding - Length: " . $i
- );
- $enc = Encoding::base32EncodeUpper($rand);
- $this->assertSame(
- $rand,
- Encoding::base32DecodeUpper($enc),
- "Base32Upper Encoding - Length: " . $i
- );
- $enc = Encoding::base32HexEncode($rand);
- $this->assertSame(
- bin2hex($rand),
- bin2hex(Encoding::base32HexDecode($enc)),
- "Base32Hex Encoding - Length: " . $i
- );
- $enc = Encoding::base32HexEncodeUpper($rand);
- $this->assertSame(
- bin2hex($rand),
- bin2hex(Encoding::base32HexDecodeUpper($enc)),
- "Base32HexUpper Encoding - Length: " . $i
- );
- $enc = Encoding::base64Encode($rand);
- $this->assertSame(
- $rand,
- Encoding::base64Decode($enc),
- "Base64 Encoding - Length: " . $i
- );
- $enc = Encoding::base64EncodeDotSlash($rand);
- $this->assertSame(
- $rand,
- Encoding::base64DecodeDotSlash($enc),
- "Base64 DotSlash Encoding - Length: " . $i
- );
- $enc = Encoding::base64EncodeDotSlashOrdered($rand);
- $this->assertSame(
- $rand,
- Encoding::base64DecodeDotSlashOrdered($enc),
- "Base64 Ordered DotSlash Encoding - Length: " . $i
- );
- $enc = Base64UrlSafe::encode($rand);
- $this->assertSame(
- \strtr(\base64_encode($rand), '+/', '-_'),
- $enc
- );
- $this->assertSame(
- $rand,
- Base64UrlSafe::decode($enc)
- );
- }
- }
- }
- }
|