Base32HexTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. use \ParagonIE\ConstantTime\Base32Hex;
  3. class Base32HexTest extends PHPUnit\Framework\TestCase
  4. {
  5. /**
  6. * @covers Base32Hex::encode()
  7. * @covers Base32Hex::decode()
  8. * @covers Base32Hex::encodeUpper()
  9. * @covers Base32Hex::decodeUpper()
  10. */
  11. public function testRandom()
  12. {
  13. for ($i = 1; $i < 32; ++$i) {
  14. for ($j = 0; $j < 50; ++$j) {
  15. $random = \random_bytes($i);
  16. $enc = Base32Hex::encode($random);
  17. $this->assertSame(
  18. $random,
  19. Base32Hex::decode($enc)
  20. );
  21. $unpadded = \rtrim($enc, '=');
  22. $this->assertSame(
  23. $unpadded,
  24. Base32Hex::encodeUnpadded($random)
  25. );
  26. $this->assertSame(
  27. $random,
  28. Base32Hex::decode($unpadded)
  29. );
  30. $enc = Base32Hex::encodeUpper($random);
  31. $this->assertSame(
  32. $random,
  33. Base32Hex::decodeUpper($enc)
  34. ); $unpadded = \rtrim($enc, '=');
  35. $this->assertSame(
  36. $unpadded,
  37. Base32Hex::encodeUpperUnpadded($random)
  38. );
  39. $this->assertSame(
  40. $random,
  41. Base32Hex::decodeUpper($unpadded)
  42. );
  43. }
  44. }
  45. }
  46. }