You are here

public static function ParagonIE_Sodium_Core_Util::bin2hex in Automatic Updates 8

Same name and namespace in other branches
  1. 7 vendor/paragonie/sodium_compat/src/Core/Util.php \ParagonIE_Sodium_Core_Util::bin2hex()

Convert a binary string into a hexadecimal string without cache-timing leaks

@internal You should not use this directly from another application

Parameters

string $binaryString (raw binary):

Return value

string

Throws

TypeError

1 call to ParagonIE_Sodium_Core_Util::bin2hex()
ParagonIE_Sodium_Compat::bin2hex in vendor/paragonie/sodium_compat/src/Compat.php
Cache-timing-safe implementation of bin2hex().

File

vendor/paragonie/sodium_compat/src/Core/Util.php, line 46

Class

ParagonIE_Sodium_Core_Util
Class ParagonIE_Sodium_Core_Util

Code

public static function bin2hex($binaryString) {

  /* Type checks: */
  if (!is_string($binaryString)) {
    throw new TypeError('Argument 1 must be a string, ' . gettype($binaryString) . ' given.');
  }
  $hex = '';
  $len = self::strlen($binaryString);
  for ($i = 0; $i < $len; ++$i) {

    /** @var array<int, int> $chunk */
    $chunk = unpack('C', $binaryString[$i]);

    /** @var int $c */
    $c = $chunk[1] & 0xf;

    /** @var int $b */
    $b = $chunk[1] >> 4;
    $hex .= pack('CC', 87 + $b + ($b - 10 >> 8 & ~38), 87 + $c + ($c - 10 >> 8 & ~38));
  }
  return $hex;
}