You are here

protected static function ParagonIE_Sodium_Core_Base64_Original::doEncode in Automatic Updates 7

Same name and namespace in other branches
  1. 8 vendor/paragonie/sodium_compat/src/Core/Base64/Original.php \ParagonIE_Sodium_Core_Base64_Original::doEncode()

Parameters

string $src:

bool $pad Include = padding?:

Return value

string

Throws

TypeError

2 calls to ParagonIE_Sodium_Core_Base64_Original::doEncode()
ParagonIE_Sodium_Core_Base64_Original::encode in vendor/paragonie/sodium_compat/src/Core/Base64/Original.php
Encode into Base64
ParagonIE_Sodium_Core_Base64_Original::encodeUnpadded in vendor/paragonie/sodium_compat/src/Core/Base64/Original.php
Encode into Base64, no = padding

File

vendor/paragonie/sodium_compat/src/Core/Base64/Original.php, line 46

Class

ParagonIE_Sodium_Core_Base64_Original
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;
}