You are here

public static function ParagonIE_Sodium_Core32_BLAKE2b::init in Automatic Updates 7

Same name and namespace in other branches
  1. 8 vendor/paragonie/sodium_compat/src/Core32/BLAKE2b.php \ParagonIE_Sodium_Core32_BLAKE2b::init()

@internal You should not use this directly from another application

@psalm-suppress MixedArgument @psalm-suppress MixedAssignment @psalm-suppress MixedArrayAccess @psalm-suppress MixedArrayAssignment @psalm-suppress MixedMethodCall

Parameters

SplFixedArray|null $key:

int $outlen:

SplFixedArray|null $salt:

SplFixedArray|null $personal:

Return value

SplFixedArray

Throws

SodiumException

TypeError

3 calls to ParagonIE_Sodium_Core32_BLAKE2b::init()
ParagonIE_Sodium_Crypto32::generichash in vendor/paragonie/sodium_compat/src/Crypto32.php
Calculate a BLAKE2b hash.
ParagonIE_Sodium_Crypto32::generichash_init in vendor/paragonie/sodium_compat/src/Crypto32.php
Initialize a hashing context for BLAKE2b.
ParagonIE_Sodium_Crypto32::generichash_init_salt_personal in vendor/paragonie/sodium_compat/src/Crypto32.php
Initialize a hashing context for BLAKE2b.

File

vendor/paragonie/sodium_compat/src/Core32/BLAKE2b.php, line 497

Class

ParagonIE_Sodium_Core32_BLAKE2b
Class ParagonIE_Sodium_Core_BLAKE2b

Code

public static function init($key = null, $outlen = 64, $salt = null, $personal = null) {
  self::pseudoConstructor();
  $klen = 0;
  if ($key !== null) {
    if (count($key) > 64) {
      throw new SodiumException('Invalid key size');
    }
    $klen = count($key);
  }
  if ($outlen > 64) {
    throw new SodiumException('Invalid output size');
  }
  $ctx = self::context();
  $p = new SplFixedArray(64);

  // Zero our param buffer...
  for ($i = 64; --$i;) {
    $p[$i] = 0;
  }
  $p[0] = $outlen;

  // digest_length
  $p[1] = $klen;

  // key_length
  $p[2] = 1;

  // fanout
  $p[3] = 1;

  // depth
  if ($salt instanceof SplFixedArray) {

    // salt: [32] through [47]
    for ($i = 0; $i < 16; ++$i) {
      $p[32 + $i] = (int) $salt[$i];
    }
  }
  if ($personal instanceof SplFixedArray) {

    // personal: [48] through [63]
    for ($i = 0; $i < 16; ++$i) {
      $p[48 + $i] = (int) $personal[$i];
    }
  }
  $ctx[0][0] = self::xor64($ctx[0][0], self::load64($p, 0));
  if ($salt instanceof SplFixedArray || $personal instanceof SplFixedArray) {

    // We need to do what blake2b_init_param() does:
    for ($i = 1; $i < 8; ++$i) {
      $ctx[0][$i] = self::xor64($ctx[0][$i], self::load64($p, $i << 3));
    }
  }
  if ($klen > 0 && $key instanceof SplFixedArray) {
    $block = new SplFixedArray(128);
    for ($i = 128; $i--;) {
      $block[$i] = 0;
    }
    for ($i = $klen; $i--;) {
      $block[$i] = $key[$i];
    }
    self::update($ctx, $block, 128);
    $ctx[4] = 128;
  }
  return $ctx;
}