Encoding.php 5.6 KB

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