protected static function ParagonIE_Sodium_Core_Base64_Common::doEncode in Automatic Updates 8
Same name and namespace in other branches
- 7 vendor/paragonie/sodium_compat/src/Core/Base64/Common.php \ParagonIE_Sodium_Core_Base64_Common::doEncode()
Parameters
string $src:
bool $pad Include = padding?:
Return value
string
Throws
TypeError
2 calls to ParagonIE_Sodium_Core_Base64_Common::doEncode()
- ParagonIE_Sodium_Core_Base64_Common::encode in vendor/
paragonie/ sodium_compat/ src/ Core/ Base64/ Common.php - Encode into Base64
- ParagonIE_Sodium_Core_Base64_Common::encodeUnpadded in vendor/
paragonie/ sodium_compat/ src/ Core/ Base64/ Common.php - Encode into Base64, no = padding
File
- vendor/
paragonie/ sodium_compat/ src/ Core/ Base64/ Common.php, line 49
Class
- ParagonIE_Sodium_Core_Base64_Common
- Class ParagonIE_Sodium_Core_Base64
Code
protected static function doEncode($src, $pad = true) {
$dest = '';
$srcLen = ParagonIE_Sodium_Core_Util::strlen($src);
// Main loop (no padding):
for ($i = 0; $i + 3 <= $srcLen; $i += 3) {
/** @var array<int, int> $chunk */
$chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, 3));
$b0 = $chunk[1];
$b1 = $chunk[2];
$b2 = $chunk[3];
$dest .= self::encode6Bits($b0 >> 2) . self::encode6Bits(($b0 << 4 | $b1 >> 4) & 63) . self::encode6Bits(($b1 << 2 | $b2 >> 6) & 63) . self::encode6Bits($b2 & 63);
}
// The last chunk, which may have padding:
if ($i < $srcLen) {
/** @var array<int, int> $chunk */
$chunk = unpack('C*', ParagonIE_Sodium_Core_Util::substr($src, $i, $srcLen - $i));
$b0 = $chunk[1];
if ($i + 1 < $srcLen) {
$b1 = $chunk[2];
$dest .= self::encode6Bits($b0 >> 2) . self::encode6Bits(($b0 << 4 | $b1 >> 4) & 63) . self::encode6Bits($b1 << 2 & 63);
if ($pad) {
$dest .= '=';
}
}
else {
$dest .= self::encode6Bits($b0 >> 2) . self::encode6Bits($b0 << 4 & 63);
if ($pad) {
$dest .= '==';
}
}
}
return $dest;
}