You are here

public static function ParagonIE_Sodium_Core_Util::bin2hexUpper 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::bin2hexUpper()

Convert a binary string into a hexadecimal string without cache-timing leaks, returning uppercase letters (as per RFC 4648)

@internal You should not use this directly from another application

Parameters

string $bin_string (raw binary):

Return value

string

Throws

TypeError

File

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

Class

ParagonIE_Sodium_Core_Util
Class ParagonIE_Sodium_Core_Util

Code

public static function bin2hexUpper($bin_string) {
  $hex = '';
  $len = self::strlen($bin_string);
  for ($i = 0; $i < $len; ++$i) {

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

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

    /**
     * Upper 16 bits
     * @var int $b
     */
    $b = $chunk[1] >> 4;

    /**
     * Use pack() and binary operators to turn the two integers
     * into hexadecimal characters. We don't use chr() here, because
     * it uses a lookup table internally and we want to avoid
     * cache-timing side-channels.
     */
    $hex .= pack('CC', 55 + $b + ($b - 10 >> 8 & ~6), 55 + $c + ($c - 10 >> 8 & ~6));
  }
  return $hex;
}