You are here

public static function ParagonIE_Sodium_Core_Salsa20::salsa20 in Automatic Updates 8

Same name and namespace in other branches
  1. 7 vendor/paragonie/sodium_compat/src/Core/Salsa20.php \ParagonIE_Sodium_Core_Salsa20::salsa20()

@internal You should not use this directly from another application

Parameters

int $len:

string $nonce:

string $key:

Return value

string

Throws

SodiumException

TypeError

4 calls to ParagonIE_Sodium_Core_Salsa20::salsa20()
ParagonIE_Sodium_Core_Salsa20::salsa20_xor in vendor/paragonie/sodium_compat/src/Core/Salsa20.php
@internal You should not use this directly from another application
ParagonIE_Sodium_Core_XSalsa20::xsalsa20 in vendor/paragonie/sodium_compat/src/Core/XSalsa20.php
Expand a key and nonce into an xsalsa20 keystream.
ParagonIE_Sodium_Crypto::secretbox_open in vendor/paragonie/sodium_compat/src/Crypto.php
Decrypt a ciphertext generated via secretbox().
ParagonIE_Sodium_File::secretbox_decrypt in vendor/paragonie/sodium_compat/src/File.php
Decrypt a file

File

vendor/paragonie/sodium_compat/src/Core/Salsa20.php, line 141

Class

ParagonIE_Sodium_Core_Salsa20
Class ParagonIE_Sodium_Core_Salsa20

Code

public static function salsa20($len, $nonce, $key) {
  if (self::strlen($key) !== 32) {
    throw new RangeException('Key must be 32 bytes long');
  }
  $kcopy = '' . $key;
  $in = self::substr($nonce, 0, 8) . str_repeat("\0", 8);
  $c = '';
  while ($len >= 64) {
    $c .= self::core_salsa20($in, $kcopy, null);
    $u = 1;

    // Internal counter.
    for ($i = 8; $i < 16; ++$i) {
      $u += self::chrToInt($in[$i]);
      $in[$i] = self::intToChr($u & 0xff);
      $u >>= 8;
    }
    $len -= 64;
  }
  if ($len > 0) {
    $c .= self::substr(self::core_salsa20($in, $kcopy, null), 0, $len);
  }
  try {
    ParagonIE_Sodium_Compat::memzero($kcopy);
  } catch (SodiumException $ex) {
    $kcopy = null;
  }
  return $c;
}