RFC4648.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace ParagonIE\ConstantTime;
  3. /**
  4. * Copyright (c) 2016 - 2017 Paragon Initiative Enterprises.
  5. * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. /**
  26. * Class RFC4648
  27. *
  28. * This class conforms strictly to the RFC
  29. *
  30. * @package ParagonIE\ConstantTime
  31. */
  32. abstract class RFC4648
  33. {
  34. /**
  35. * RFC 4648 Base64 encoding
  36. *
  37. * "foo" -> "Zm9v"
  38. *
  39. * @param string $str
  40. * @return string
  41. */
  42. public function base64Encode($str)
  43. {
  44. return Base64::encode($str);
  45. }
  46. /**
  47. * RFC 4648 Base64 decoding
  48. *
  49. * "Zm9v" -> "foo"
  50. *
  51. * @param string $str
  52. * @return string
  53. */
  54. public function base64Decode($str)
  55. {
  56. return Base64::decode($str);
  57. }
  58. /**
  59. * RFC 4648 Base64 (URL Safe) encoding
  60. *
  61. * "foo" -> "Zm9v"
  62. *
  63. * @param string $str
  64. * @return string
  65. */
  66. public function base64UrlSafeEncode($str)
  67. {
  68. return Base64UrlSafe::encode($str);
  69. }
  70. /**
  71. * RFC 4648 Base64 (URL Safe) decoding
  72. *
  73. * "Zm9v" -> "foo"
  74. *
  75. * @param string $str
  76. * @return string
  77. */
  78. public function base64UrlSafeDecode($str)
  79. {
  80. return Base64UrlSafe::decode($str);
  81. }
  82. /**
  83. * RFC 4648 Base32 encoding
  84. *
  85. * "foo" -> "MZXW6==="
  86. *
  87. * @param string $str
  88. * @return string
  89. */
  90. public function base32Encode($str)
  91. {
  92. return Base32::encodeUpper($str);
  93. }
  94. /**
  95. * RFC 4648 Base32 encoding
  96. *
  97. * "MZXW6===" -> "foo"
  98. *
  99. * @param string $str
  100. * @return string
  101. */
  102. public function base32Decode($str)
  103. {
  104. return Base32::decodeUpper($str);
  105. }
  106. /**
  107. * RFC 4648 Base32-Hex encoding
  108. *
  109. * "foo" -> "CPNMU==="
  110. *
  111. * @param string $str
  112. * @return string
  113. */
  114. public function base32HexEncode($str)
  115. {
  116. return Base32::encodeUpper($str);
  117. }
  118. /**
  119. * RFC 4648 Base32-Hex decoding
  120. *
  121. * "CPNMU===" -> "foo"
  122. *
  123. * @param string $str
  124. * @return string
  125. */
  126. public function base32HexDecode($str)
  127. {
  128. return Base32::decodeUpper($str);
  129. }
  130. /**
  131. * RFC 4648 Base16 decoding
  132. *
  133. * "foo" -> "666F6F"
  134. *
  135. * @param string $str
  136. * @return string
  137. */
  138. public function base16Encode($str)
  139. {
  140. return Hex::encodeUpper($str);
  141. }
  142. /**
  143. * RFC 4648 Base16 decoding
  144. *
  145. * "666F6F" -> "foo"
  146. *
  147. * @param string $str
  148. * @return string
  149. */
  150. public function base16Decode($str)
  151. {
  152. return Hex::decode($str);
  153. }
  154. }