Defuse\Crypto\Encoding::hexToBin PHP Method

hexToBin() public static method

Converts a hexadecimal string into a byte string without leaking information through side channels.
public static hexToBin ( string $hex_string ) : string
$hex_string string
return string
    public static function hexToBin($hex_string)
    {
        $hex_pos = 0;
        $bin = '';
        $hex_len = Core::ourStrlen($hex_string);
        $state = 0;
        $c_acc = 0;
        while ($hex_pos < $hex_len) {
            $c = \ord($hex_string[$hex_pos]);
            $c_num = $c ^ 48;
            $c_num0 = $c_num - 10 >> 8;
            $c_alpha = ($c & ~32) - 55;
            $c_alpha0 = ($c_alpha - 10 ^ $c_alpha - 16) >> 8;
            if (($c_num0 | $c_alpha0) === 0) {
                throw new Ex\BadFormatException('Encoding::hexToBin() input is not a hex string.');
            }
            $c_val = $c_num0 & $c_num | $c_alpha & $c_alpha0;
            if ($state === 0) {
                $c_acc = $c_val * 16;
            } else {
                $bin .= \pack('C', $c_acc | $c_val);
            }
            $state ^= 1;
            ++$hex_pos;
        }
        return $bin;
    }

Usage Example

示例#1
0
文件: Key.php 项目: robstoll/PuMa
 public static function LoadFromAsciiSafeString($savedKeyString)
 {
     try {
         $bytes = Encoding::hexToBin($savedKeyString);
     } catch (\RangeException $ex) {
         throw new Ex\CannotPerformOperationException("Key has invalid hex encoding.");
     }
     /* Make sure we have enough bytes to get the version header. */
     if (Core::ourStrlen($bytes) < self::KEY_HEADER_SIZE) {
         throw new Ex\CannotPerformOperationException("Saved Key is shorter than the version header.");
     }
     /* Grab the version header. */
     $version_header = Core::ourSubstr($bytes, 0, self::KEY_HEADER_SIZE);
     /* Grab the config for that version. */
     $config = self::GetKeyVersionConfigFromKeyHeader($version_header);
     /* Now that we know the version, check the length is correct. */
     if (Core::ourStrlen($bytes) !== self::KEY_HEADER_SIZE + $config->keyByteSize() + $config->checksumByteSize()) {
         throw new Ex\CannotPerformOperationException("Saved Key is not the correct size.");
     }
     /* Grab the bytes that are part of the checksum. */
     $checked_bytes = Core::ourSubstr($bytes, 0, self::KEY_HEADER_SIZE + $config->keyByteSize());
     /* Grab the included checksum. */
     $checksum_a = Core::ourSubstr($bytes, self::KEY_HEADER_SIZE + $config->keyByteSize(), $config->checksumByteSize());
     /* Re-compute the checksum. */
     $checksum_b = hash($config->checksumHashFunction(), $checked_bytes, true);
     /* Validate it. It *is* important for this to be constant time. */
     if (!Core::hashEquals($checksum_a, $checksum_b)) {
         throw new Ex\CannotPerformOperationException("Saved key is corrupted -- checksums don't match.");
     }
     /* Everything checks out. Grab the key and create a Key object. */
     $key_bytes = Core::ourSubstr($bytes, self::KEY_HEADER_SIZE, $config->keyByteSize());
     return new Key($version_header, $key_bytes);
 }
All Usage Examples Of Defuse\Crypto\Encoding::hexToBin