thinkphp¼ÓÃÜÒªÁì
×î½ü£¬Ô½À´Ô½¶àµÄÍøÕ¾×îÏÈʹÓÃthinkphp¿ò¼ÜÀ´¿ª·¢ºÍά»¤×Ô¼ºµÄÍøÕ¾¡£ÕâÊÇÓÉÓÚthinkphp¿ò¼Ü¾ßÓÐÐí¶àÓŵ㣬ºÃ±ÈÒ×ÓÚѧϰ¡¢Ç¿Ê¢µÄÄ£°åÒýÇæ¡¢Çå¾²ÐԸߵȵȡ£È»¶øÔÚ¿ª·¢Àú³ÌÖУ¬ÐèҪעÖØÊý¾ÝÇå¾²µÄÎÊÌ⣬ÆäÖÐÒ»ÏîÖ÷ÒªµÄʹÃü¾ÍÊǼÓÃÜÊý¾Ý¡£±¾ÎĽ«»á̽ÌÖthinkphpÖмÓÃÜÊý¾ÝµÄÒªÁì¡£
ʹÓÃϵͳ×Ô´øµÄ¼ÓÃܺ¯Êý
thinkphp¿ò¼ÜÖÐ×Ô´øµÄ¼ÓÃܺ¯ÊýΪthinkhelperHash::make($str)¡£Õâ¸öº¯Êý¿ÉÒÔÓÃÀ´¼ÓÃÜ×Ö·û´®£¨$str£©¡£
ʹÓÃÒªÁìÈçÏ£º
$str = 'Hello World'; $encrypt_str = thinkhelperHash::make($str);
µÇ¼ºó¸´ÖÆ
¼ÓÃܺóµÄ×Ö·û´®$encrypt_str£¬ËüÊÇÒ»¸ö40λµÄËæ»ú×Ö·û´®¡£ÕâÖÖÒªÁìÊÇÒ»¸ö»ùÓÚ¹þÏ£Ëã·¨µÄ¼òÆÓ¼ÓÃÜÒªÁ죬¹ØÓÚһЩ¼òÆӵij¡¾°ÊÇ×ã¹»µÄ¡£
ʹÓõÚÈý·½¿â
³ýÁËϵͳ×Ô´øµÄ¼ÓÃܺ¯ÊýÖ®Í⣬thinkphp»¹Ö§³ÖÒýÈëµÚÈý·½¿â¾ÙÐмÓÃÜ¡£ºÃ±È¿ÉÒÔʹÓÃPHP¼ÓÃÜÀ©Õ¹¿âsodiumÀ´¾ÙÐÐÊý¾Ý¼ÓÃÜ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ʹÓÃÒªÁìÈçÏ£º
Ê×ÏÈÐèҪװÖÃsodiumÀ©Õ¹¿â£¬Ê¹ÓÃÏÂÁ
pecl install libsodium
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÐèÒªÔÚphp.iniÎļþÖмÓÈëÒ»ÐУº
extension=sodium.so
µÇ¼ºó¸´ÖÆ
È»ºóÔÚ´úÂëÖÐʹÓüÓÃܺ¯Êý¾ÙÐмÓÃÜ£¬ÔÚsodium¿âÖУ¬¼ÓÃܺ¯ÊýΪsodium_crypto_secretbox($str, $nonce, $secretKey)¡£
ÆäÖУ¬$strΪÐèÒª¼ÓÃܵÄ×Ö·û´®£¬$nonceΪΨһµÄ×Ö·û´®£¬$secretKeyΪÃÜÔ¿¡£
ʹÓÃÒªÁìÈçÏ£º
$str = 'Hello World'; $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES); $secretKey = sodium_crypto_secretbox_keygen(); $encrypt_str = sodium_crypto_secretbox($str, $nonce, $secretKey);
µÇ¼ºó¸´ÖÆ
¼ÓÃܺóµÄ$encrypt_str£¬¿ÉÒÔʹÓÃÏàͬµÄÃÜÔ¿ºÍnonceÀ´¾ÙÐнâÃÜ¡£
×Ô½ç˵¼ÓÃÜÒªÁì
ÈôÊÇÏëÒªÔ½·¢Çå¾²µÄ¼ÓÃÜ·½·¨£¬¿ÉÒÔ×Ô½ç˵¼ÓÃܺ¯Êý»òÕßŲÓõÚÈý·½µÄ¿â¾ÙÐмÓÃÜ¡£
ʹÓÃ×Ô½ç˵¼ÓÃܺ¯ÊýµÄÒªÁìÈçÏ£º
function my_encrypt($data, $key){ $iv = mcrypt_create_iv( mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_DEV_URANDOM ); $encrypted = base64_encode( $iv . mcrypt_encrypt( MCRYPT_RIJNDAEL_128, hash('sha256', $key, true), $data, MCRYPT_MODE_CBC, $iv ) ); return $encrypted; }
µÇ¼ºó¸´ÖÆ
ÕâÊÇÒ»¸ö»ùÓÚaes-256-ctrËã·¨µÄ¼ÓÃܺ¯Êý£¬¿ÉÒÔ×Ô½ç˵ÃÜÔ¿ºÍ¼ÓÃÜÊý¾Ý¡£¿ÉÊÇÐèҪעÖصÄÊÇ£¬mcryptÒѾÔÚPHP 7.2°æ±¾Öб»·ÅÆú£¬Òò´ËÐèÒª×ÔÐÐÑ°ÕÒÌæ»»µÄÒªÁì¡£
×ܽá
Êý¾ÝÇå¾²ÊÇÒ»¸öÍøÕ¾¿ª·¢Àú³ÌÖбØÐèҪ˼Á¿µÄÎÊÌâ¡£ÔÚthinkphp¿ò¼ÜÖУ¬¿ÉÒÔͨ¹ýϵͳ×Ô´øµÄ¼ÓÃܺ¯Êý¡¢µÚÈý·½¿â»òÕß×Ô½ç˵¼ÓÃܺ¯Êý£¬À´°ü¹ÜÊý¾ÝµÄÇå¾²ÐÔ¡£¿ÉÊÇÐèҪƾ֤ÏêϸµÄÐèÇóÀ´Ñ¡ÔñºÏÊʵļÓÃÜ·½·¨£¬ÒÔµÖ´ï×î¼ÑµÄЧ¹û¡£
ÒÔÉϾÍÊÇthinkphp¼ÓÃÜÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡