国产宅男网站在线|亚洲A级性爱免费视频|亚洲中精品级在线|午夜福利AA毛

  • <dd id="gf5jf"><th id="gf5jf"></th></dd>

    <cite id="gf5jf"><label id="gf5jf"></label></cite>
  • <div id="gf5jf"><listing id="gf5jf"></listing></div>
    學(xué)習(xí)啦>學(xué)習(xí)電腦>網(wǎng)絡(luò)知識(shí)>網(wǎng)絡(luò)技術(shù)>

    如何正確地使用加密與認(rèn)證技術(shù)(5)

    時(shí)間: 恒輝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);

      }

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

    如何正確地使用加密與認(rèn)證技術(shù)(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 * *
    推薦度:
    點(diǎn)擊下載文檔文檔為doc格式
    168319