2 declare(strict_types=1);
3 namespace ParagonIE\ConstantTime;
6 * Copyright (c) 2016 Paragon Initiative Enterprises.
7 * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
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:
16 * The above copyright notice and this permission notice shall be included in all
17 * copies or substantial portions of the Software.
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
32 * @package ParagonIE\ConstantTime
34 abstract class Base64 implements EncoderInterface
39 * Base64 character set "[A-Z][a-z][0-9]+/"
44 public static function encode(string $src): string
47 $srcLen = Binary::safeStrlen($src);
48 // Main loop (no padding):
49 for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
50 $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 3));
56 static::encode6Bits( $b0 >> 2 ) .
57 static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
58 static::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
59 static::encode6Bits( $b2 & 63);
61 // The last chunk, which may have padding:
63 $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
65 if ($i + 1 < $srcLen) {
68 static::encode6Bits( $b0 >> 2 ) .
69 static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
70 static::encode6Bits( ($b1 << 2) & 63) . '=';
73 static::encode6Bits( $b0 >> 2) .
74 static::encode6Bits(($b0 << 4) & 63) . '==';
81 * decode from base64 into binary
83 * Base64 character set "./[A-Z][a-z][0-9]"
86 * @param bool $strictPadding
88 * @throws \RangeException
90 public static function decode(string $src, bool $strictPadding = false): string
93 $srcLen = Binary::safeStrlen($src);
99 if (($srcLen & 3) === 0) {
100 if ($src[$srcLen - 1] === '=') {
102 if ($src[$srcLen - 1] === '=') {
107 if (($srcLen & 3) === 1) {
108 throw new \RangeException(
112 if ($src[$srcLen - 1] === '=') {
113 throw new \RangeException(
118 $src = \rtrim($src, '=');
119 $srcLen = Binary::safeStrlen($src);
124 // Main loop (no padding):
125 for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
126 $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 4));
127 $c0 = static::decode6Bits($chunk[1]);
128 $c1 = static::decode6Bits($chunk[2]);
129 $c2 = static::decode6Bits($chunk[3]);
130 $c3 = static::decode6Bits($chunk[4]);
134 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
135 ((($c1 << 4) | ($c2 >> 2)) & 0xff),
136 ((($c2 << 6) | $c3 ) & 0xff)
138 $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
140 // The last chunk, which may have padding:
142 $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
143 $c0 = static::decode6Bits($chunk[1]);
145 if ($i + 2 < $srcLen) {
146 $c1 = static::decode6Bits($chunk[2]);
147 $c2 = static::decode6Bits($chunk[3]);
150 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
151 ((($c1 << 4) | ($c2 >> 2)) & 0xff)
153 $err |= ($c0 | $c1 | $c2) >> 8;
154 } elseif ($i + 1 < $srcLen) {
155 $c1 = static::decode6Bits($chunk[2]);
158 ((($c0 << 2) | ($c1 >> 4)) & 0xff)
160 $err |= ($c0 | $c1) >> 8;
161 } elseif ($i < $srcLen && $strictPadding) {
172 * Uses bitwise operators instead of table-lookups to turn 6-bit integers
173 * into 8-bit integers.
175 * Base64 character set:
176 * [A-Z] [a-z] [0-9] + /
177 * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
182 protected static function decode6Bits(int $src): int
186 // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
187 $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
189 // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
190 $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
192 // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
193 $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
195 // if ($src == 0x2b) $ret += 62 + 1;
196 $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63;
198 // if ($src == 0x2f) ret += 63 + 1;
199 $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64;
205 * Uses bitwise operators instead of table-lookups to turn 8-bit integers
206 * into 6-bit integers.
211 protected static function encode6Bits(int $src): string
215 // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
216 $diff += ((25 - $src) >> 8) & 6;
218 // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
219 $diff -= ((51 - $src) >> 8) & 75;
221 // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15
222 $diff -= ((61 - $src) >> 8) & 15;
224 // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3
225 $diff += ((62 - $src) >> 8) & 3;
227 return \pack('C', $src + $diff);