Base64.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 Base64
  28. * [A-Z][a-z][0-9]+/
  29. *
  30. * @package ParagonIE\ConstantTime
  31. */
  32. abstract class Base64 implements EncoderInterface
  33. {
  34. /**
  35. * Encode into Base64
  36. *
  37. * Base64 character set "[A-Z][a-z][0-9]+/"
  38. *
  39. * @param string $src
  40. * @return string
  41. * @throws \TypeError
  42. */
  43. public static function encode(string $src): string
  44. {
  45. return static::doEncode($src, true);
  46. }
  47. /**
  48. * Encode into Base64, no = padding
  49. *
  50. * Base64 character set "[A-Z][a-z][0-9]+/"
  51. *
  52. * @param string $src
  53. * @return string
  54. * @throws \TypeError
  55. */
  56. public static function encodeUnpadded(string $src): string
  57. {
  58. return static::doEncode($src, false);
  59. }
  60. /**
  61. * @param string $src
  62. * @param bool $pad Include = padding?
  63. * @return string
  64. * @throws \TypeError
  65. */
  66. protected static function doEncode(string $src, bool $pad = true): string
  67. {
  68. $dest = '';
  69. $srcLen = Binary::safeStrlen($src);
  70. // Main loop (no padding):
  71. for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
  72. /** @var array<int, int> $chunk */
  73. $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 3));
  74. $b0 = $chunk[1];
  75. $b1 = $chunk[2];
  76. $b2 = $chunk[3];
  77. $dest .=
  78. static::encode6Bits( $b0 >> 2 ) .
  79. static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
  80. static::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
  81. static::encode6Bits( $b2 & 63);
  82. }
  83. // The last chunk, which may have padding:
  84. if ($i < $srcLen) {
  85. /** @var array<int, int> $chunk */
  86. $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
  87. $b0 = $chunk[1];
  88. if ($i + 1 < $srcLen) {
  89. $b1 = $chunk[2];
  90. $dest .=
  91. static::encode6Bits($b0 >> 2) .
  92. static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
  93. static::encode6Bits(($b1 << 2) & 63);
  94. if ($pad) {
  95. $dest .= '=';
  96. }
  97. } else {
  98. $dest .=
  99. static::encode6Bits( $b0 >> 2) .
  100. static::encode6Bits(($b0 << 4) & 63);
  101. if ($pad) {
  102. $dest .= '==';
  103. }
  104. }
  105. }
  106. return $dest;
  107. }
  108. /**
  109. * decode from base64 into binary
  110. *
  111. * Base64 character set "./[A-Z][a-z][0-9]"
  112. *
  113. * @param string $src
  114. * @param bool $strictPadding
  115. * @return string
  116. * @throws \RangeException
  117. * @throws \TypeError
  118. */
  119. public static function decode(string $src, bool $strictPadding = false): string
  120. {
  121. // Remove padding
  122. $srcLen = Binary::safeStrlen($src);
  123. if ($srcLen === 0) {
  124. return '';
  125. }
  126. if ($strictPadding) {
  127. if (($srcLen & 3) === 0) {
  128. if ($src[$srcLen - 1] === '=') {
  129. $srcLen--;
  130. if ($src[$srcLen - 1] === '=') {
  131. $srcLen--;
  132. }
  133. }
  134. }
  135. if (($srcLen & 3) === 1) {
  136. throw new \RangeException(
  137. 'Incorrect padding'
  138. );
  139. }
  140. if ($src[$srcLen - 1] === '=') {
  141. throw new \RangeException(
  142. 'Incorrect padding'
  143. );
  144. }
  145. } else {
  146. $src = \rtrim($src, '=');
  147. $srcLen = Binary::safeStrlen($src);
  148. }
  149. $err = 0;
  150. $dest = '';
  151. // Main loop (no padding):
  152. for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
  153. /** @var array<int, int> $chunk */
  154. $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 4));
  155. $c0 = static::decode6Bits($chunk[1]);
  156. $c1 = static::decode6Bits($chunk[2]);
  157. $c2 = static::decode6Bits($chunk[3]);
  158. $c3 = static::decode6Bits($chunk[4]);
  159. $dest .= \pack(
  160. 'CCC',
  161. ((($c0 << 2) | ($c1 >> 4)) & 0xff),
  162. ((($c1 << 4) | ($c2 >> 2)) & 0xff),
  163. ((($c2 << 6) | $c3 ) & 0xff)
  164. );
  165. $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
  166. }
  167. // The last chunk, which may have padding:
  168. if ($i < $srcLen) {
  169. /** @var array<int, int> $chunk */
  170. $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
  171. $c0 = static::decode6Bits($chunk[1]);
  172. if ($i + 2 < $srcLen) {
  173. $c1 = static::decode6Bits($chunk[2]);
  174. $c2 = static::decode6Bits($chunk[3]);
  175. $dest .= \pack(
  176. 'CC',
  177. ((($c0 << 2) | ($c1 >> 4)) & 0xff),
  178. ((($c1 << 4) | ($c2 >> 2)) & 0xff)
  179. );
  180. $err |= ($c0 | $c1 | $c2) >> 8;
  181. } elseif ($i + 1 < $srcLen) {
  182. $c1 = static::decode6Bits($chunk[2]);
  183. $dest .= \pack(
  184. 'C',
  185. ((($c0 << 2) | ($c1 >> 4)) & 0xff)
  186. );
  187. $err |= ($c0 | $c1) >> 8;
  188. } elseif ($i < $srcLen && $strictPadding) {
  189. $err |= 1;
  190. }
  191. }
  192. if ($err !== 0) {
  193. throw new \RangeException(
  194. 'Base64::decode() only expects characters in the correct base64 alphabet'
  195. );
  196. }
  197. return $dest;
  198. }
  199. /**
  200. * Uses bitwise operators instead of table-lookups to turn 6-bit integers
  201. * into 8-bit integers.
  202. *
  203. * Base64 character set:
  204. * [A-Z] [a-z] [0-9] + /
  205. * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
  206. *
  207. * @param int $src
  208. * @return int
  209. */
  210. protected static function decode6Bits(int $src): int
  211. {
  212. $ret = -1;
  213. // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
  214. $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
  215. // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
  216. $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
  217. // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
  218. $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
  219. // if ($src == 0x2b) $ret += 62 + 1;
  220. $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63;
  221. // if ($src == 0x2f) ret += 63 + 1;
  222. $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64;
  223. return $ret;
  224. }
  225. /**
  226. * Uses bitwise operators instead of table-lookups to turn 8-bit integers
  227. * into 6-bit integers.
  228. *
  229. * @param int $src
  230. * @return string
  231. */
  232. protected static function encode6Bits(int $src): string
  233. {
  234. $diff = 0x41;
  235. // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
  236. $diff += ((25 - $src) >> 8) & 6;
  237. // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
  238. $diff -= ((51 - $src) >> 8) & 75;
  239. // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15
  240. $diff -= ((61 - $src) >> 8) & 15;
  241. // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3
  242. $diff += ((62 - $src) >> 8) & 3;
  243. return \pack('C', $src + $diff);
  244. }
  245. }