PHP cryptr Cryptographic functions

Source: Internet
Author: User
Tags crypt diff ord

classCrypthelper {/** * Encryption * @param unknown $password * @param unknown $salt * @return String*/     public Static function Crypt($password,$salt){        //$saltPrefix. = ' $2y$ ';//Blowfish algorithm//$saltPrefix. = ' 13 ';//two-bit cost parameter//$saltPrefix. = ' $ '; A $//$saltSuffix. = ' lgsf2ctmkkhe1yr2py.vtu ';        return Crypt($password,$salt); }        /** * Character comparison * @param unknown $expected * @param unknown $actual * @return Boolean*/     public Static functionCompareString ($expected,$actual)    {        $expected. = "+"; $actual. = "+"; $expectedLength= Mb_strlen ($expected, ' 8bit '); $actualLength= Mb_strlen ($actual, ' 8bit '); $diff=$expectedLength-$actualLength;  for($i= 0;$i<$actualLength;$i++) {            $diff|= (Ord($actual[$i]) ^Ord($expected[$i%$expectedLength])); }        return $diff= = 0; }        /** * Check password * @param unknown $password * @param unknown $hash * @return Boolean*/     public Static functionValidatePassword ($password,$hash)    {        if(!is_string($password) ||$password= = = "") {            return false; }                if(!Preg_match('/^\$2[axy]\$ (\d\d) \$[\.\/0-9a-za-z]{22}/',$hash,$matches)            ||$matches[1] < 4 | |$matches[1] > 30        ) {            return false; }                $test= self::Crypt($password,$hash); $n=strlen($test); if($n!== 60) {            return false; }        returnSelf::comparestring ($test,$hash); }}

Test

//---------test--------//encryption$salt= ' $2y$13$lgsf2ctmkkhe1yr2py.vtu ';//7 + = =$password= ' Aa123456 ';Echocrypthelper::Crypt($password,$salt);Echo Php_eol;//Checksum$hash= ' $2y$13$lgsf2ctmkkhe1yr2py.vtuiur/a0c6tarkcxmo.lulsirisu7u53m ';Echocrypthelper::Crypt($password,$hash);Echo Php_eol;Echo strlen($hash);Echo Php_eol;EchoCrypthelper::validatepassword ($password,$hash);

PHP cryptr Cryptographic functions

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.