Encoding.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. declare(strict_types=1);
  3. namespace ParagonIE\ConstantTime;
  4. use TypeError;
  5. /**
  6. * Copyright (c) 2016 - 2022 Paragon Initiative Enterprises.
  7. * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. /**
  28. * Class Encoding
  29. * @package ParagonIE\ConstantTime
  30. */
  31. abstract class Encoding
  32. {
  33. /**
  34. * RFC 4648 Base32 encoding
  35. *
  36. * @param string $str
  37. * @return string
  38. * @throws TypeError
  39. */
  40. public static function base32Encode(string $str): string
  41. {
  42. return Base32::encode($str);
  43. }
  44. /**
  45. * RFC 4648 Base32 encoding
  46. *
  47. * @param string $str
  48. * @return string
  49. * @throws TypeError
  50. */
  51. public static function base32EncodeUpper(string $str): string
  52. {
  53. return Base32::encodeUpper($str);
  54. }
  55. /**
  56. * RFC 4648 Base32 decoding
  57. *
  58. * @param string $str
  59. * @return string
  60. * @throws TypeError
  61. */
  62. public static function base32Decode(string $str): string
  63. {
  64. return Base32::decode($str);
  65. }
  66. /**
  67. * RFC 4648 Base32 decoding
  68. *
  69. * @param string $str
  70. * @return string
  71. * @throws TypeError
  72. */
  73. public static function base32DecodeUpper(string $str): string
  74. {
  75. return Base32::decodeUpper($str);
  76. }
  77. /**
  78. * RFC 4648 Base32 encoding
  79. *
  80. * @param string $str
  81. * @return string
  82. * @throws TypeError
  83. */
  84. public static function base32HexEncode(string $str): string
  85. {
  86. return Base32Hex::encode($str);
  87. }
  88. /**
  89. * RFC 4648 Base32Hex encoding
  90. *
  91. * @param string $str
  92. * @return string
  93. * @throws TypeError
  94. */
  95. public static function base32HexEncodeUpper(string $str): string
  96. {
  97. return Base32Hex::encodeUpper($str);
  98. }
  99. /**
  100. * RFC 4648 Base32Hex decoding
  101. *
  102. * @param string $str
  103. * @return string
  104. * @throws TypeError
  105. */
  106. public static function base32HexDecode(string $str): string
  107. {
  108. return Base32Hex::decode($str);
  109. }
  110. /**
  111. * RFC 4648 Base32Hex decoding
  112. *
  113. * @param string $str
  114. * @return string
  115. * @throws TypeError
  116. */
  117. public static function base32HexDecodeUpper(string $str): string
  118. {
  119. return Base32Hex::decodeUpper($str);
  120. }
  121. /**
  122. * RFC 4648 Base64 encoding
  123. *
  124. * @param string $str
  125. * @return string
  126. * @throws TypeError
  127. */
  128. public static function base64Encode(string $str): string
  129. {
  130. return Base64::encode($str);
  131. }
  132. /**
  133. * RFC 4648 Base64 decoding
  134. *
  135. * @param string $str
  136. * @return string
  137. * @throws TypeError
  138. */
  139. public static function base64Decode(string $str): string
  140. {
  141. return Base64::decode($str);
  142. }
  143. /**
  144. * Encode into Base64
  145. *
  146. * Base64 character set "./[A-Z][a-z][0-9]"
  147. * @param string $str
  148. * @return string
  149. * @throws TypeError
  150. */
  151. public static function base64EncodeDotSlash(string $str): string
  152. {
  153. return Base64DotSlash::encode($str);
  154. }
  155. /**
  156. * Decode from base64 to raw binary
  157. *
  158. * Base64 character set "./[A-Z][a-z][0-9]"
  159. *
  160. * @param string $str
  161. * @return string
  162. * @throws \RangeException
  163. * @throws TypeError
  164. */
  165. public static function base64DecodeDotSlash(string $str): string
  166. {
  167. return Base64DotSlash::decode($str);
  168. }
  169. /**
  170. * Encode into Base64
  171. *
  172. * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
  173. * @param string $str
  174. * @return string
  175. * @throws TypeError
  176. */
  177. public static function base64EncodeDotSlashOrdered(string $str): string
  178. {
  179. return Base64DotSlashOrdered::encode($str);
  180. }
  181. /**
  182. * Decode from base64 to raw binary
  183. *
  184. * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
  185. *
  186. * @param string $str
  187. * @return string
  188. * @throws \RangeException
  189. * @throws TypeError
  190. */
  191. public static function base64DecodeDotSlashOrdered(string $str): string
  192. {
  193. return Base64DotSlashOrdered::decode($str);
  194. }
  195. /**
  196. * Convert a binary string into a hexadecimal string without cache-timing
  197. * leaks
  198. *
  199. * @param string $bin_string (raw binary)
  200. * @return string
  201. * @throws TypeError
  202. */
  203. public static function hexEncode(string $bin_string): string
  204. {
  205. return Hex::encode($bin_string);
  206. }
  207. /**
  208. * Convert a hexadecimal string into a binary string without cache-timing
  209. * leaks
  210. *
  211. * @param string $hex_string
  212. * @return string (raw binary)
  213. * @throws \RangeException
  214. */
  215. public static function hexDecode(string $hex_string): string
  216. {
  217. return Hex::decode($hex_string);
  218. }
  219. /**
  220. * Convert a binary string into a hexadecimal string without cache-timing
  221. * leaks
  222. *
  223. * @param string $bin_string (raw binary)
  224. * @return string
  225. * @throws TypeError
  226. */
  227. public static function hexEncodeUpper(string $bin_string): string
  228. {
  229. return Hex::encodeUpper($bin_string);
  230. }
  231. /**
  232. * Convert a binary string into a hexadecimal string without cache-timing
  233. * leaks
  234. *
  235. * @param string $bin_string (raw binary)
  236. * @return string
  237. */
  238. public static function hexDecodeUpper(string $bin_string): string
  239. {
  240. return Hex::decode($bin_string);
  241. }
  242. }