Provides various official and user-released code examples. For code reference, you are welcome to learn about edauth's efficient reversible random encryption function. The performance is better than Discuz's authcode function and the response is faster.
'Auth _ key' => 'your KEY string' in the project configuration file'
// Edauth efficient reversible random encryption function parameter explanation
// Author: PGCao (pork ribs CAO)
#------------------------------------------------------------------------#
# $ String: plaintext or ciphertext
# $ Operation: true indicates encryption, and false indicates decryption.
# $ Key: key
# $ Outtime: ciphertext validity period, in seconds
# $ Entype: Two encryption methods are supported: md5 and sha1. You must use the same method to correctly restore the plaintext.
Function edauth ($ string, $ operation = true, $ key = '', $ outtime = 0, $ entype = 'md5 '){
$ Key_length = 4;
If ($ entype = 'md5') {// use the md5 Method
$ Long_len = 32; $ half_len = 16; $ entype = 'md5 ';
} Else {// use sha1
$ Long_len = 40; $ half_len = 20; $ entype = 'sha1 ';
}
$ Key = $ key! = ''? $ Key: substr (md5 ($ _ SERVER ['document _ root']. C ('auth _ key '). $ _ SERVER ['remote _ ADDR ']), 0, 30 );
$ FixedKey = hash ($ entype, $ key );
$ Egiskeys = md5 (substr ($ fixedKey, $ half_len, $ half_len ));
$ RuntoKey = $ key_length? ($ Operation? Substr (hash ($ entype, microtime (true),-$ key_length): substr ($ string, 0, $ key_length )):'';
$ Keys = hash ($ entype, substr ($ runtoKey, 0, $ half_len ). substr ($ fixedKey, 0, $ half_len ). substr ($ runtoKey, $ half_len ). substr ($ fixedKey, $ half_len ));
$ String = $ operation? Sprintf ('% 010d', $ outtime? $ Outtime + time (): 0 ). substr (md5 ($ string. $ egiskeys), 0, $ half_len ). $ string: base64_decode (substr ($ string, $ key_length ));
$ I = 0; $ result = '';
$ String_length = strlen ($ string );
For ($ I = 0; $ I <$ string_length; $ I ++ ){
$ Result. = chr (ord ($ string {$ I}) ^ ord ($ keys {$ I % $ long_len }));
}
If ($ operation ){
Return $ runtoKey. str_replace ('=', '', base64_encode ($ result ));
} Else {
If (substr ($ result, 0, 10) = 0 | substr ($ result, 0, 10)-time ()> 0) & substr ($ result, 10, $ half_len) = substr (md5 (substr ($ result, $ half_len + 10 ). $ egiskeys), 0, $ half_len )){
Return substr ($ result, $ half_len + 10 );
} Else {
Return '';
}
}
}The following is the speed code. If you are interested in PR, you can use the authcode function for comparison. The more you use on the same page, the better the performance difference.
// Test processing speed
Echo "edauth function ";
$ _ Runtime _ = microtime (true );
$ Str = "decrypt after producing different ciphertext! ^_^ ";
// For ($ I = 0; $ I <500; $ I ++) $ str. = "the same plaintext can be decrypted after different ciphertext values are produced! ^_^ ";
// $ Str. = "OK ";
$ Key = "12 *** 89 ";
Echo"Plaintext:". $ Str ." ";
Echo"Ciphertext:". $ Out = edauth ($ str, true, $ key );
Echo" Original article:". Edauth ($ out, false, $ key );
Echo" Plaintext:". $ Str ." ";
Echo"Ciphertext:". $ Out = edauth ($ str, true, $ key );
Echo" Original article:". Edauth ($ out, false, $ key );
$ Runtime = number_format (microtime (true)-$ _ runtime _), 6 );
Echo' Runtime '. $ runtime. 'second (s )';
?>
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB