Encoding.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 Encoding
  28. * @package ParagonIE\ConstantTime
  29. */
  30. abstract class Encoding
  31. {
  32. /**
  33. * RFC 4648 Base32 encoding
  34. *
  35. * @param string $str
  36. * @return string
  37. * @throws \TypeError
  38. */
  39. public static function base32Encode(string $str): string
  40. {
  41. return Base32::encode($str);
  42. }
  43. /**
  44. * RFC 4648 Base32 encoding
  45. *
  46. * @param string $str
  47. * @return string
  48. * @throws \TypeError
  49. */
  50. public static function base32EncodeUpper(string $str): string
  51. {
  52. return Base32::encodeUpper($str);
  53. }
  54. /**
  55. * RFC 4648 Base32 decoding
  56. *
  57. * @param string $str
  58. * @return string
  59. * @throws \TypeError
  60. */
  61. public static function base32Decode(string $str): string
  62. {
  63. return Base32::decode($str);
  64. }
  65. /**
  66. * RFC 4648 Base32 decoding
  67. *
  68. * @param string $str
  69. * @return string
  70. * @throws \TypeError
  71. */
  72. public static function base32DecodeUpper(string $str): string
  73. {
  74. return Base32::decodeUpper($str);
  75. }
  76. /**
  77. * RFC 4648 Base32 encoding
  78. *
  79. * @param string $str
  80. * @return string
  81. * @throws \TypeError
  82. */
  83. public static function base32HexEncode(string $str): string
  84. {
  85. return Base32Hex::encode($str);
  86. }
  87. /**
  88. * RFC 4648 Base32Hex encoding
  89. *
  90. * @param string $str
  91. * @return string
  92. * @throws \TypeError
  93. */
  94. public static function base32HexEncodeUpper(string $str): string
  95. {
  96. return Base32Hex::encodeUpper($str);
  97. }
  98. /**
  99. * RFC 4648 Base32Hex decoding
  100. *
  101. * @param string $str
  102. * @return string
  103. * @throws \TypeError
  104. */
  105. public static function base32HexDecode(string $str): string
  106. {
  107. return Base32Hex::decode($str);
  108. }
  109. /**
  110. * RFC 4648 Base32Hex decoding
  111. *
  112. * @param string $str
  113. * @return string
  114. * @throws \TypeError
  115. */
  116. public static function base32HexDecodeUpper(string $str): string
  117. {
  118. return Base32Hex::decodeUpper($str);
  119. }
  120. /**
  121. * RFC 4648 Base64 encoding
  122. *
  123. * @param string $str
  124. * @return string
  125. * @throws \TypeError
  126. */
  127. public static function base64Encode(string $str): string
  128. {
  129. return Base64::encode($str);
  130. }
  131. /**
  132. * RFC 4648 Base64 decoding
  133. *
  134. * @param string $str
  135. * @return string
  136. * @throws \TypeError
  137. */
  138. public static function base64Decode(string $str): string
  139. {
  140. return Base64::decode($str);
  141. }
  142. /**
  143. * Encode into Base64
  144. *
  145. * Base64 character set "./[A-Z][a-z][0-9]"
  146. * @param string $str
  147. * @return string
  148. * @throws \TypeError
  149. */
  150. public static function base64EncodeDotSlash(string $str): string
  151. {
  152. return Base64DotSlash::encode($str);
  153. }
  154. /**
  155. * Decode from base64 to raw binary
  156. *
  157. * Base64 character set "./[A-Z][a-z][0-9]"
  158. *
  159. * @param string $str
  160. * @return string
  161. * @throws \RangeException
  162. * @throws \TypeError
  163. */
  164. public static function base64DecodeDotSlash(string $str): string
  165. {
  166. return Base64DotSlash::decode($str);
  167. }
  168. /**
  169. * Encode into Base64
  170. *
  171. * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
  172. * @param string $str
  173. * @return string
  174. * @throws \TypeError
  175. */
  176. public static function base64EncodeDotSlashOrdered(string $str): string
  177. {
  178. return Base64DotSlashOrdered::encode($str);
  179. }
  180. /**
  181. * Decode from base64 to raw binary
  182. *
  183. * Base64 character set "[.-9][A-Z][a-z]" or "./[0-9][A-Z][a-z]"
  184. *
  185. * @param string $str
  186. * @return string
  187. * @throws \RangeException
  188. * @throws \TypeError
  189. */
  190. public static function base64DecodeDotSlashOrdered(string $str): string
  191. {
  192. return Base64DotSlashOrdered::decode($str);
  193. }
  194. /**
  195. * Convert a binary string into a hexadecimal string without cache-timing
  196. * leaks
  197. *
  198. * @param string $bin_string (raw binary)
  199. * @return string
  200. * @throws \TypeError
  201. */
  202. public static function hexEncode(string $bin_string): string
  203. {
  204. return Hex::encode($bin_string);
  205. }
  206. /**
  207. * Convert a hexadecimal string into a binary string without cache-timing
  208. * leaks
  209. *
  210. * @param string $hex_string
  211. * @return string (raw binary)
  212. * @throws \RangeException
  213. */
  214. public static function hexDecode(string $hex_string): string
  215. {
  216. return Hex::decode($hex_string);
  217. }
  218. /**
  219. * Convert a binary string into a hexadecimal string without cache-timing
  220. * leaks
  221. *
  222. * @param string $bin_string (raw binary)
  223. * @return string
  224. * @throws \TypeError
  225. */
  226. public static function hexEncodeUpper(string $bin_string): string
  227. {
  228. return Hex::encodeUpper($bin_string);
  229. }
  230. /**
  231. * Convert a binary string into a hexadecimal string without cache-timing
  232. * leaks
  233. *
  234. * @param string $bin_string (raw binary)
  235. * @return string
  236. */
  237. public static function hexDecodeUpper(string $bin_string): string
  238. {
  239. return Hex::decode($bin_string);
  240. }
  241. }