Edauth efficient reversible random encryption function

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.