RFC4648Test.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. use \ParagonIE\ConstantTime\Base32;
  3. use \ParagonIE\ConstantTime\Base32Hex;
  4. use \ParagonIE\ConstantTime\Base64;
  5. use \ParagonIE\ConstantTime\Base64DotSlash;
  6. use \ParagonIE\ConstantTime\Base64DotSlashOrdered;
  7. use \ParagonIE\ConstantTime\Encoding;
  8. use \ParagonIE\ConstantTime\Hex;
  9. /**
  10. * Class RFC4648Test
  11. *
  12. * @ref https://tools.ietf.org/html/rfc4648#section-10
  13. */
  14. class RFC4648Test extends PHPUnit_Framework_TestCase
  15. {
  16. public function testVectorBase64()
  17. {
  18. $this->assertSame(Base64::encode(''), '');
  19. $this->assertSame(Base64::encode('f'), 'Zg==');
  20. $this->assertSame(Base64::encode('fo'), 'Zm8=');
  21. $this->assertSame(Base64::encode('foo'), 'Zm9v');
  22. $this->assertSame(Base64::encode('foob'), 'Zm9vYg==');
  23. $this->assertSame(Base64::encode('fooba'), 'Zm9vYmE=');
  24. $this->assertSame(Base64::encode('foobar'), 'Zm9vYmFy');
  25. }
  26. public function testVectorBase32()
  27. {
  28. $this->assertSame(Base32::encode(''), '');
  29. $this->assertSame(Base32::encode('f'), 'my======');
  30. $this->assertSame(Base32::encode('fo'), 'mzxq====');
  31. $this->assertSame(Base32::encode('foo'), 'mzxw6===');
  32. $this->assertSame(Base32::encode('foob'), 'mzxw6yq=');
  33. $this->assertSame(Base32::encode('fooba'), 'mzxw6ytb');
  34. $this->assertSame(Base32::encode('foobar'), 'mzxw6ytboi======');
  35. $this->assertSame(Base32::encodeUpper(''), '');
  36. $this->assertSame(Base32::encodeUpper('f'), 'MY======');
  37. $this->assertSame(Base32::encodeUpper('fo'), 'MZXQ====');
  38. $this->assertSame(Base32::encodeUpper('foo'), 'MZXW6===');
  39. $this->assertSame(Base32::encodeUpper('foob'), 'MZXW6YQ=');
  40. $this->assertSame(Base32::encodeUpper('fooba'), 'MZXW6YTB');
  41. $this->assertSame(Base32::encodeUpper('foobar'), 'MZXW6YTBOI======');
  42. }
  43. public function testVectorBase32Hex()
  44. {
  45. $this->assertSame(Base32Hex::encode(''), '');
  46. $this->assertSame(Base32Hex::encode('f'), 'co======');
  47. $this->assertSame(Base32Hex::encode('fo'), 'cpng====');
  48. $this->assertSame(Base32Hex::encode('foo'), 'cpnmu===');
  49. $this->assertSame(Base32Hex::encode('foob'), 'cpnmuog=');
  50. $this->assertSame(Base32Hex::encode('fooba'), 'cpnmuoj1');
  51. $this->assertSame(Base32Hex::encode('foobar'), 'cpnmuoj1e8======');
  52. $this->assertSame(Base32Hex::encodeUpper(''), '');
  53. $this->assertSame(Base32Hex::encodeUpper('f'), 'CO======');
  54. $this->assertSame(Base32Hex::encodeUpper('fo'), 'CPNG====');
  55. $this->assertSame(Base32Hex::encodeUpper('foo'), 'CPNMU===');
  56. $this->assertSame(Base32Hex::encodeUpper('foob'), 'CPNMUOG=');
  57. $this->assertSame(Base32Hex::encodeUpper('fooba'), 'CPNMUOJ1');
  58. $this->assertSame(Base32Hex::encodeUpper('foobar'), 'CPNMUOJ1E8======');
  59. }
  60. public function testVectorBase16()
  61. {
  62. $this->assertSame(Hex::encode(''), '');
  63. $this->assertSame(Hex::encode('f'), '66');
  64. $this->assertSame(Hex::encode('fo'), '666f');
  65. $this->assertSame(Hex::encode('foo'), '666f6f');
  66. $this->assertSame(Hex::encode('foob'), '666f6f62');
  67. $this->assertSame(Hex::encode('fooba'), '666f6f6261');
  68. $this->assertSame(Hex::encode('foobar'), '666f6f626172');
  69. $this->assertSame(Hex::encodeUpper(''), '');
  70. $this->assertSame(Hex::encodeUpper('f'), '66');
  71. $this->assertSame(Hex::encodeUpper('fo'), '666F');
  72. $this->assertSame(Hex::encodeUpper('foo'), '666F6F');
  73. $this->assertSame(Hex::encodeUpper('foob'), '666F6F62');
  74. $this->assertSame(Hex::encodeUpper('fooba'), '666F6F6261');
  75. $this->assertSame(Hex::encodeUpper('foobar'), '666F6F626172');
  76. }
  77. }