ParagonIE_Sodium_Core_Util::bin2hex

Advertisement

Summery Summery

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

Syntax Syntax

ParagonIE_Sodium_Core_Util::bin2hex( string $binaryString )

Parameters Parameters

$binaryString

(string) (Required) (raw binary)

Return Return

(string)

Source Source

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

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

Advertisement

Advertisement

Leave a Reply

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