= 20); return substr($digestinfo, $digestinfo_length-20); } function remove_PKCS1_padding_md5($data, $blocksize) { $digestinfo = remove_PKCS1_padding($data, $blocksize); $digestinfo_length = strlen($digestinfo); //md5 digestinfo length not less than 16 //assert($digestinfo_length >= 16); return substr($digestinfo, $digestinfo_length-16); } //-- // Convert binary data to a decimal number //-- function binary_to_number($data) { $base = "256"; $radix = "1"; $result = "0"; for($i = strlen($data) - 1; $i >= 0; $i--) { $digit = ord($data{$i}); $part_res = bcmul($digit, $radix); $result = bcadd($result, $part_res); $radix = bcmul($radix, $base); } return $result; } //-- // Convert a number back into binary form //-- function number_to_binary($number, $blocksize) { $base = "256"; $result = ""; $div = $number; while($div > 0) { $mod = bcmod($div, $base); $div = bcdiv($div, $base); $result = chr($mod) . $result; } return str_pad($result, $blocksize, "\x00", STR_PAD_LEFT); } // //Convert hexadecimal format data into binary // function hex2bins($data) { $len = strlen($data); $newdata=''; for($i=0;$i<$len;$i+=2) { $newdata .= pack("C",hexdec(substr($data,$i,2))); } return $newdata; } //function getPrivate($file, $passphrase){ // $p = openssl_pkey_get_private(file_get_contents($file), $passphrase); // $res = openssl_pkey_get_details($p); // var_dump($res); // openssl_free_key($p); // return array( // 'n' => bin2hex($res['rsa']['n']),//#模数 // 'e' => bin2hex($res['rsa']['e']),//#公钥指数 // 'd' => bin2hex($res['rsa']['d']),//#私钥指数 // ); //} // // Function to load privateKey and publicKey objects from pkcs12 file // @param $file // $file is expected to be pkcs12 file realpath // @param $passphrase // $passphrase is expected to be password of privatekey // @param $keyType // $keyType is expected to be a option of key style (pub/pri) // @author simonyi peng // function getKey($file, $passphrase, $keyType){ $p12_File_Name = ($file); $certs = array(); $pkcs12 = file_get_contents($p12_File_Name); openssl_pkcs12_read($pkcs12, $certs, $passphrase); #var_dump($certs);//可通过var_dump函数查看输出数组的key $pubKey = $certs['cert'];//公钥数据 $priKey = $certs['pkey'];//私钥数据 if($keyType == 'pubKey'){ return $pubKey; } if($keyType == 'priKey'){ return $priKey; } } // // Function to sign resource String to BASE64 // @author simonyi peng // function signByPriKey($resource, $file, $passphrase){ $priKey = getKey($file, $passphrase, 'priKey'); $res = openssl_pkey_get_private($priKey); if(openssl_sign($resource, $out, $res)){ //var_dump(base64_encode($out)); return base64_encode($out); }else{ return ""; } } // // Function to verify if signmsg is signed by appointed certificate and resource String // @param $sign -- sign parameter must be a BASE64 encoding string // @return 1 if verify victory 0 if verify missed // @author simonyi peng // function verifyByPubKey($resource, $sign, $file, $passphrase){ $pubKey = getKey($file, $passphrase, 'pubKey'); $out = base64_decode($sign); $res = openssl_pkey_get_public($pubKey); if(openssl_verify($resource, $out, $res) == 1) return 1; //echo "verify_success"; else return 0; //echo "verify_failed"; } ?>