private static function customPBKDF2($algorithm, $password, $salt, $count, $key_length)
    {
        $hash_length = strlen(hash($algorithm, '', true));
        if (0 === $key_length) {
            $key_length = $hash_length;
        }
        $block_count = ceil($key_length / $hash_length);

        $output = '';
        for ($i = 1; $i <= $block_count; ++$i) {
            $last = $salt.pack('N', $i);
            $last = $xorsum = hash_hmac($algorithm, $last, $password, true);
            for ($j = 1; $j < $count; ++$j) {
                $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
            }
            $output .= $xorsum;
        }

        return substr($output, 0, $key_length);
    }