ParagonIE_Sodium_Core_Util::bin2hexUpper

Advertisement

Summery Summery

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

Syntax Syntax

ParagonIE_Sodium_Core_Util::bin2hexUpper( string $bin_string )

Parameters Parameters

$bin_string

(string) (Required) (raw binary)

Return Return

(string)

Source Source

File: wp-includes/sodium_compat/src/Core/Util.php

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

Advertisement

Advertisement

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.