精品丰满熟女一区二区三区_五月天亚洲欧美综合网_亚洲青青青在线观看_国产一区二区精选

  • <menu id="29e66"></menu>

    <bdo id="29e66"><mark id="29e66"><legend id="29e66"></legend></mark></bdo>

  • <pre id="29e66"><tt id="29e66"><rt id="29e66"></rt></tt></pre>

      <label id="29e66"></label><address id="29e66"><mark id="29e66"><strike id="29e66"></strike></mark></address>
      學習啦>學習電腦>網絡知識>網絡技術>

      如何正確地使用加密與認證技術(5)

      時間: 恒輝636 分享

        0x05 用Libsodium安全加密Cookies

        /*

        // At some point, we run this command:

        $key = Sodium::randombytes_buf(Sodium::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES);

        */

        /**

        * Store ciphertext in a cookie

        *

        * @param string $name - cookie name

        * @param mixed $cookieData - cookie data

        * @param string $key - crypto key

        */

        function setSafeCookie($name, $cookieData, $key)

        {

        $nonce = Sodium::randombytes_buf(Sodium::CRYPTO_SECRETBOX_NONCEBYTES);

        return setcookie(

        $name,

        base64_encode(

        $nonce.

        Sodium::crypto_secretbox(

        json_encode($cookieData),

        $nonce,

        $key

        )

        )

        );

        }

        /**

        * Decrypt a cookie, expand to array

        *

        * @param string $name - cookie name

        * @param string $key - crypto key

        */

        function getSafeCookie($name, $key)

        {

        $hexSize = 2 * Sodium::Sodium::CRYPTO_SECRETBOX_NONCEBYTES;

        if (!isset($_COOKIE[$name])) {

        return array();

        }

        $decoded = base64_decode($_COOKIE[$name]);

        $nonce = mb_substr($decoded, 0, $hexSize, '8bit');

        $ciphertext = mb_substr($decoded, $hexSize, null, '8bit');

        $decrypted = Sodium::crypto_secretbox_open(

        $ciphertext,

        $nonce,

        $key

        );

        if (empty($decrypted)) {

        return array();

        }

        return json_decode($decrypted, true);

        }

        對于沒有l(wèi)ibsodium庫的開發(fā)人員,我們的一個博客讀者,提供了一個安全cookie實現的例子,其使用了defuse/php-encryption(我們推薦的PHP庫)。

      如何正確地使用加密與認證技術(5)

      0x05 用Libsodium安全加密Cookies /* // At some point, we run this command: $key = Sodium::randombytes_buf(Sodium::CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES); */ /** * Store ciphertext in a cookie * *
      推薦度:
      點擊下載文檔文檔為doc格式
      168319