2 namespace ParagonIE\ConstantTime;
5 * Copyright (c) 2016 Paragon Initiative Enterprises.
6 * Copyright (c) 2014 Steve "Sc00bz" Thomas (steve at tobtu dot com)
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:
15 * The above copyright notice and this permission notice shall be included in all
16 * copies or substantial portions of the Software.
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
31 * @package ParagonIE\ConstantTime
33 abstract class Base64 implements EncoderInterface
38 * Base64 character set "[A-Z][a-z][0-9]+/"
43 public static function encode($src)
46 $srcLen = Binary::safeStrlen($src);
47 // Main loop (no padding):
48 for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
49 $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 3));
55 static::encode6Bits( $b0 >> 2 ) .
56 static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
57 static::encode6Bits((($b1 << 2) | ($b2 >> 6)) & 63) .
58 static::encode6Bits( $b2 & 63);
60 // The last chunk, which may have padding:
62 $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
64 if ($i + 1 < $srcLen) {
67 static::encode6Bits( $b0 >> 2 ) .
68 static::encode6Bits((($b0 << 4) | ($b1 >> 4)) & 63) .
69 static::encode6Bits( ($b1 << 2) & 63) . '=';
72 static::encode6Bits( $b0 >> 2) .
73 static::encode6Bits(($b0 << 4) & 63) . '==';
80 * decode from base64 into binary
82 * Base64 character set "./[A-Z][a-z][0-9]"
86 * @throws \RangeException
88 public static function decode($src, $strictPadding = false)
91 $srcLen = Binary::safeStrlen($src);
96 if (($srcLen & 3) === 0) {
97 if ($src[$srcLen - 1] === '=') {
99 if ($src[$srcLen - 1] === '=') {
104 if (($srcLen & 3) === 1) {
105 throw new \RangeException(
110 $src = \rtrim($src, '=');
111 $srcLen = Binary::safeStrlen($src);
116 // Main loop (no padding):
117 for ($i = 0; $i + 4 <= $srcLen; $i += 4) {
118 $chunk = \unpack('C*', Binary::safeSubstr($src, $i, 4));
119 $c0 = static::decode6Bits($chunk[1]);
120 $c1 = static::decode6Bits($chunk[2]);
121 $c2 = static::decode6Bits($chunk[3]);
122 $c3 = static::decode6Bits($chunk[4]);
126 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
127 ((($c1 << 4) | ($c2 >> 2)) & 0xff),
128 ((($c2 << 6) | $c3 ) & 0xff)
130 $err |= ($c0 | $c1 | $c2 | $c3) >> 8;
132 // The last chunk, which may have padding:
134 $chunk = \unpack('C*', Binary::safeSubstr($src, $i, $srcLen - $i));
135 $c0 = static::decode6Bits($chunk[1]);
136 if ($i + 2 < $srcLen) {
137 $c1 = static::decode6Bits($chunk[2]);
138 $c2 = static::decode6Bits($chunk[3]);
141 ((($c0 << 2) | ($c1 >> 4)) & 0xff),
142 ((($c1 << 4) | ($c2 >> 2)) & 0xff)
144 $err |= ($c0 | $c1 | $c2) >> 8;
145 } elseif($i + 1 < $srcLen) {
146 $c1 = static::decode6Bits($chunk[2]);
149 ((($c0 << 2) | ($c1 >> 4)) & 0xff)
151 $err |= ($c0 | $c1) >> 8;
152 } elseif ($i < $srcLen && $strictPadding) {
163 * Uses bitwise operators instead of table-lookups to turn 6-bit integers
164 * into 8-bit integers.
166 * Base64 character set:
167 * [A-Z] [a-z] [0-9] + /
168 * 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
173 protected static function decode6Bits($src)
177 // if ($src > 0x40 && $src < 0x5b) $ret += $src - 0x41 + 1; // -64
178 $ret += (((0x40 - $src) & ($src - 0x5b)) >> 8) & ($src - 64);
180 // if ($src > 0x60 && $src < 0x7b) $ret += $src - 0x61 + 26 + 1; // -70
181 $ret += (((0x60 - $src) & ($src - 0x7b)) >> 8) & ($src - 70);
183 // if ($src > 0x2f && $src < 0x3a) $ret += $src - 0x30 + 52 + 1; // 5
184 $ret += (((0x2f - $src) & ($src - 0x3a)) >> 8) & ($src + 5);
186 // if ($src == 0x2b) $ret += 62 + 1;
187 $ret += (((0x2a - $src) & ($src - 0x2c)) >> 8) & 63;
189 // if ($src == 0x2f) ret += 63 + 1;
190 $ret += (((0x2e - $src) & ($src - 0x30)) >> 8) & 64;
196 * Uses bitwise operators instead of table-lookups to turn 8-bit integers
197 * into 6-bit integers.
202 protected static function encode6Bits($src)
206 // if ($src > 25) $diff += 0x61 - 0x41 - 26; // 6
207 $diff += ((25 - $src) >> 8) & 6;
209 // if ($src > 51) $diff += 0x30 - 0x61 - 26; // -75
210 $diff -= ((51 - $src) >> 8) & 75;
212 // if ($src > 61) $diff += 0x2b - 0x30 - 10; // -15
213 $diff -= ((61 - $src) >> 8) & 15;
215 // if ($src > 62) $diff += 0x2f - 0x2b - 1; // 3
216 $diff += ((62 - $src) >> 8) & 3;
218 return \pack('C', $src + $diff);