RFC4648.php 4.1 KB

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