繁体   English   中英

Laravel 5.4通过会话ID加密

[英]Laravel 5.4 Encryption by session id

我正在寻找使用会话ID加密文件ID并将其用作下载的临时URL的方法。

我发现Laravel中只是encrypt功能,但这与我想要的不完全一样。 是否有一些类似的功能可以使用会话ID字符串进行编码和解码?

make function in 
**Path -  /app/Libraries/Scramble.php**

**<Scramble.php>**
<?php

namespace App\Libraries;

use Crypt;
use Session;
use Illuminate\Contracts\Encryption\EncryptException;

class Scramble
{

    public function __construct()
    {
    }

    /**
     * Encrypt the given value with session binding.
     *
     * @param string $value
     *
     * @return string
     *
     * @throws \Illuminate\Contracts\Encryption\EncryptException
     */
    public static function encrypt($value)
    {
        if ($value === false) {
            throw new EncryptException('Could not encrypt the data.');
        }
        $manupulate_val = Session::getId()."##".config('app.key')."##".$value;
        return Crypt::encrypt($manupulate_val);
    }

    /**
     * Decrypt the given value.
     *
     * @param  string  $decrypted
     * @return string
     *
     * @throws \Illuminate\Contracts\Encryption\DecryptException
     */
    public static function decrypt($decrypted)
    {
        if ($decrypted === false) {
            throw new DecryptException('Could not decrypt the data.');
        }

        $sess_id         = Session::getId();
        $decryptedStr    = Crypt::decrypt($decrypted);
        $decryptedStrArr = explode("##", $decryptedStr);

        if (is_array($decryptedStrArr) && $decryptedStrArr['0'] !== $sess_id) {
            abort(400);
        }

        if (is_array($decryptedStrArr) && $decryptedStrArr['1'] !== config('app.key')) {
            abort(400);
        }

        return $decryptedStrArr['2'];
    }
}
**</scramble.php>**

now you can call anywhere....

use App\Libraries\Scramble;

$Yourid = 12345;
$sessionIdWithencData = Scramble::encrypt($Yourid);
$sessionIdWithdecData = Scramble::decrypt($sessionIdWithencData);
dd($sessionIdWithdecData);
==========================================


i hope this is help full

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM