EsignUtilHelper.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Servers\ESign\comm;
  3. /**
  4. * 常用工具类
  5. * @author 澄泓
  6. * @date 2022/08/18 15:40
  7. */
  8. class EsignUtilHelper
  9. {
  10. /**
  11. * 获取MD5
  12. * @param $bodyData
  13. * @return string
  14. */
  15. public static function getContentMd5($bodyData){
  16. return base64_encode(md5($bodyData,true));
  17. }
  18. /**
  19. * 生成签名
  20. * @param $projectScert
  21. * @param $httpMethod
  22. * @param $accept
  23. * @param $contentType
  24. * @param $contentMd5
  25. * @param $date
  26. * @param $headers
  27. * @param $url
  28. * @return string
  29. */
  30. public static function getSignature($projectScert,$httpMethod,$accept,$contentType,$contentMd5,$date,$headers,$url){
  31. $stringToSign = $httpMethod . "\n" . $accept . "\n" . $contentMd5 . "\n" . $contentType . "\n" . $date . "\n" .$headers ;
  32. if($headers != ''){
  33. $stringToSign .= "\n".$url;
  34. }else{
  35. $stringToSign .= $url;
  36. }
  37. // EsignLogHelper::printMsg($stringToSign);
  38. $signature = hash_hmac("sha256",$stringToSign, $projectScert, true);
  39. $signature = base64_encode($signature);
  40. return $signature;
  41. }
  42. // 判断是网络路径还是文件路径
  43. public static function isUrl($url){
  44. $parse = parse_url($url);
  45. $scheme = $parse['scheme'];
  46. $scheme = strtolower($scheme);
  47. if("http" == $scheme){
  48. return true;
  49. }
  50. if("https" == $scheme){
  51. return true;
  52. }
  53. return false;
  54. }
  55. /**
  56. * 生成13位时间戳
  57. * @return float
  58. */
  59. public static function getMillisecond() {
  60. list($t1, $t2) = explode(' ', microtime());
  61. return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
  62. }
  63. public static function jsonSer($obj){
  64. $json = array();
  65. foreach ($obj as $key => $value) {
  66. if($value==null) {
  67. continue;
  68. }
  69. $json[$key] = $value;
  70. }
  71. return $json;
  72. }
  73. /**
  74. 获取文件的Content-MD5
  75. 原理:1.先计算MD5加密的二进制数组(128位)
  76. 2.再对这个二进制进行base64编码(而不是对32位字符串编码)
  77. */
  78. public static function getContentBase64Md5($filePath){
  79. //获取文件MD5的128位二进制数组
  80. $md5file = md5_file($filePath,true);
  81. //计算文件的Content-MD5
  82. $contentBase64Md5 = base64_encode($md5file);
  83. return $contentBase64Md5;
  84. }
  85. }