Defuse\Crypto\Encoding::binToHex PHP Method

binToHex() public static method

Converts a byte string to a hexadecimal string without leaking information through side channels.
public static binToHex ( string $byte_string ) : string
$byte_string string
return string
    public static function binToHex($byte_string)
    {
        $hex = '';
        $len = Core::ourStrlen($byte_string);
        for ($i = 0; $i < $len; ++$i) {
            $c = \ord($byte_string[$i]) & 0xf;
            $b = \ord($byte_string[$i]) >> 4;
            $hex .= \pack('CC', 87 + $b + ($b - 10 >> 8 & ~38), 87 + $c + ($c - 10 >> 8 & ~38));
        }
        return $hex;
    }

Usage Example

 public function testEncodeDecodeEquivalencyTwoBytes()
 {
     for ($b1 = 0; $b1 < 256; $b1++) {
         for ($b2 = 0; $b2 < 256; $b2++) {
             $str = \pack('C', $b1) . \pack('C', $b2);
             $encode_a = Encoding::binToHex($str);
             $encode_b = \bin2hex($str);
             $this->assertSame($encode_b, $encode_a);
             $decode_a = Encoding::hexToBin($encode_a);
             $decode_b = \hex2bin($encode_b);
             $this->assertSame($decode_b, $decode_a);
             $this->assertSame($str, $decode_b);
         }
     }
 }
All Usage Examples Of Defuse\Crypto\Encoding::binToHex