PHPDaemon\Utils\Crypt::stringIdx PHP Method

stringIdx() public static method

Returns the character at index $idx in $str in constant time
public static stringIdx ( string $str, integer $idx ) : string
$str string String
$idx integer Index
return string
    public static function stringIdx($str, $idx)
    {
        // FIXME: Make the const-time hack below work for all integer sizes, or
        // check it properly
        $l = mb_orig_strlen($str);
        if ($l > 65535 || $idx > $l) {
            return false;
        }
        $r = 0;
        for ($i = 0; $i < $l; ++$i) {
            $x = $i ^ $idx;
            $mask = ((($x | $x >> 16) & 0xffff) + 0xffff >> 16) - 1;
            $r |= ord($str[$i]) & $mask;
        }
        return chr($r);
    }