looginModule :: frofder()必须是字符串的实例,字符串给定

发布于 2025-01-31 17:54:57 字数 3426 浏览 2 评论 0原文

我有这样的代码,当我在不使用Laravel项目中使用任何依赖项的情况下运行它时,我可以获取JWT,但是当我将相同的代码移动到.php项目时,我会遇到此错误。为什么?

我在此.php文件中编写的代码并指定了从部分给出此错误:参数1传递给loginmodule :: fromder()必须是字符串的实例,字符串给定

    $privKey=openssl_pkey_get_private(file_get_contents(APP_ROOT_PATH.'system/applekey/AuthKey.pem';
            $dataa = $this->encode(json_encode($header)) . '.' . $this->encode(json_encode($payload));
            $signature = '';
            $success = openssl_sign($dataa, $signature, $privKey, OPENSSL_ALGO_SHA256);
            //The value of $signature returns this:0D >tºA55Ï»‹€ë݆}OãÍ%2KÉÛcÏñã ¾Ò#ä–"¼>»j§}ıË9
            äğeFT¤9w
            $raw_signature=$this->fromDER($signature, 64);
            $jwt = $dataa . '.' . $this->encode($raw_signature);

这是我写的代码在Laravel文件中,可以更轻松地查看输出,并且不包含任何依赖项。

 $privKey = openssl_pkey_get_private(file_get_contents(public_path('AuthKey_D8A88N3HCS.pem')));
        $dataa = $this->encode(json_encode($header)) . '.' . $this->encode(json_encode($payload));
        $signature = '';
        $success = openssl_sign($dataa, $signature, $privKey, OPENSSL_ALGO_SHA256);
        //The value of $signature returns this:b"0E\x02!\x00¶Œ7ûî>Zj+­/©R¤;[\x10ÌçÂ@BÅ=¸Q8¶9¼îÇ\x02 J,V(èhÈÅ\x00ýÕð8Z=\x04Sœˆæ«D(!Ä¢/z‚Hv\x13"
        $raw_signature = $this->fromDER($signature, 64);
        $jwt = $dataa . '.' . $this->encode($raw_signature);

这是传递功能

public static function fromDER(string $der, int $partLength)
    {
        $hex = unpack('H*', $der)[1];
        if ('30' !== mb_substr($hex, 0, 2, '8bit')) { // SEQUENCE
            throw new \RuntimeException();
        }
        if ('81' === mb_substr($hex, 2, 2, '8bit')) { // LENGTH > 128
            $hex = mb_substr($hex, 6, null, '8bit');
        } else {
            $hex = mb_substr($hex, 4, null, '8bit');
        }
        if ('02' !== mb_substr($hex, 0, 2, '8bit')) { // INTEGER
            throw new \RuntimeException();
        }
        $Rl = hexdec(mb_substr($hex, 2, 2, '8bit'));
        $R = self::retrievePositiveInteger(mb_substr($hex, 4, $Rl * 2, '8bit'));
        $R = str_pad($R, $partLength, '0', STR_PAD_LEFT);
        $hex = mb_substr($hex, 4 + $Rl * 2, null, '8bit');
        if ('02' !== mb_substr($hex, 0, 2, '8bit')) { // INTEGER
            throw new \RuntimeException();
        }
        $Sl = hexdec(mb_substr($hex, 2, 2, '8bit'));
        $S = self::retrievePositiveInteger(mb_substr($hex, 4, $Sl * 2, '8bit'));
        $S = str_pad($S, $partLength, '0', STR_PAD_LEFT);
        return pack('H*', $R . $S);
    }
    /**
     * @param string $data
     *
     * @return string
     */
    private static function preparePositiveInteger(string $data)
    {
        if (mb_substr($data, 0, 2, '8bit') > '7f') {
            return '00' . $data;
        }
        while ('00' === mb_substr($data, 0, 2, '8bit') && mb_substr($data, 2, 2, '8bit') <= '7f') {
            $data = mb_substr($data, 2, null, '8bit');
        }
        return $data;
    }
    /**
     * @param string $data
     *
     * @return string
     */
    private static function retrievePositiveInteger(string $data)
    {
        while ('00' === mb_substr($data, 0, 2, '8bit') && mb_substr($data, 2, 2, '8bit') > '7f') {
            $data = mb_substr($data, 2, null, '8bit');
        }
        return $data;
    }

I have a code like this and when I run it by writing plain php without using any dependencies in a Laravel project, I can get jwt, but when I move the same code to a .php project, I get this error. Why?

The code I wrote in this .php file and specifying the fromDer part gives this error: Argument 1 passed to loginModule::fromDER() must be an instance of string, string given

    $privKey=openssl_pkey_get_private(file_get_contents(APP_ROOT_PATH.'system/applekey/AuthKey.pem';
            $dataa = $this->encode(json_encode($header)) . '.' . $this->encode(json_encode($payload));
            $signature = '';
            $success = openssl_sign($dataa, $signature, $privKey, OPENSSL_ALGO_SHA256);
            //The value of $signature returns this:0D >tºA55Ï»‹€ë݆}OãÍ%2KÉÛcÏñã ¾Ò#ä–"¼>»j§}ıË9
            äğeFT¤9w
            $raw_signature=$this->fromDER($signature, 64);
            $jwt = $dataa . '.' . $this->encode($raw_signature);

This is the code I wrote in the laravel file to see the output more easily and it does not contain any dependencies.

 $privKey = openssl_pkey_get_private(file_get_contents(public_path('AuthKey_D8A88N3HCS.pem')));
        $dataa = $this->encode(json_encode($header)) . '.' . $this->encode(json_encode($payload));
        $signature = '';
        $success = openssl_sign($dataa, $signature, $privKey, OPENSSL_ALGO_SHA256);
        //The value of $signature returns this:b"0E\x02!\x00¶Œ7ûî>Zj+­/©R¤;[\x10ÌçÂ@BÅ=¸Q8¶9¼îÇ\x02 J,V(èhÈÅ\x00ýÕð8Z=\x04Sœˆæ«D(!Ä¢/z‚Hv\x13"
        $raw_signature = $this->fromDER($signature, 64);
        $jwt = $dataa . '.' . $this->encode($raw_signature);

This is the fromDer function

public static function fromDER(string $der, int $partLength)
    {
        $hex = unpack('H*', $der)[1];
        if ('30' !== mb_substr($hex, 0, 2, '8bit')) { // SEQUENCE
            throw new \RuntimeException();
        }
        if ('81' === mb_substr($hex, 2, 2, '8bit')) { // LENGTH > 128
            $hex = mb_substr($hex, 6, null, '8bit');
        } else {
            $hex = mb_substr($hex, 4, null, '8bit');
        }
        if ('02' !== mb_substr($hex, 0, 2, '8bit')) { // INTEGER
            throw new \RuntimeException();
        }
        $Rl = hexdec(mb_substr($hex, 2, 2, '8bit'));
        $R = self::retrievePositiveInteger(mb_substr($hex, 4, $Rl * 2, '8bit'));
        $R = str_pad($R, $partLength, '0', STR_PAD_LEFT);
        $hex = mb_substr($hex, 4 + $Rl * 2, null, '8bit');
        if ('02' !== mb_substr($hex, 0, 2, '8bit')) { // INTEGER
            throw new \RuntimeException();
        }
        $Sl = hexdec(mb_substr($hex, 2, 2, '8bit'));
        $S = self::retrievePositiveInteger(mb_substr($hex, 4, $Sl * 2, '8bit'));
        $S = str_pad($S, $partLength, '0', STR_PAD_LEFT);
        return pack('H*', $R . $S);
    }
    /**
     * @param string $data
     *
     * @return string
     */
    private static function preparePositiveInteger(string $data)
    {
        if (mb_substr($data, 0, 2, '8bit') > '7f') {
            return '00' . $data;
        }
        while ('00' === mb_substr($data, 0, 2, '8bit') && mb_substr($data, 2, 2, '8bit') <= '7f') {
            $data = mb_substr($data, 2, null, '8bit');
        }
        return $data;
    }
    /**
     * @param string $data
     *
     * @return string
     */
    private static function retrievePositiveInteger(string $data)
    {
        while ('00' === mb_substr($data, 0, 2, '8bit') && mb_substr($data, 2, 2, '8bit') > '7f') {
            $data = mb_substr($data, 2, null, '8bit');
        }
        return $data;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文