六月丁香五月婷婷,丁香五月婷婷网,欧美激情网站,日本护士xxxx,禁止18岁天天操夜夜操,18岁禁止1000免费,国产福利无码一区色费

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

如何正確地使用加密與認(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