人人商城

secureUtil.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */$log = new PhpLog ( SDK_LOG_FILE_PATH, "PRC", SDK_LOG_LEVEL );
  6. function sign(&$params) {
  7. global $log;
  8. $log->LogInfo ( '=====签名报文开始======' );
  9. if(isset($params['transTempUrl'])){
  10. unset($params['transTempUrl']);
  11. }
  12. $params_str = coverParamsToString ( $params );
  13. $log->LogInfo ( "签名key=val&...串 >" . $params_str );
  14. $params_sha1x16 = sha1 ( $params_str, FALSE );
  15. $log->LogInfo ( "摘要sha1x16 >" . $params_sha1x16 );
  16. $cert_path = SDK_SIGN_CERT_PATH;
  17. $private_key = getPrivateKey ( $cert_path );
  18. $sign_falg = openssl_sign ( $params_sha1x16, $signature, $private_key, OPENSSL_ALGO_SHA1 );
  19. if ($sign_falg) {
  20. $signature_base64 = base64_encode ( $signature );
  21. $log->LogInfo ( "签名串为 >" . $signature_base64 );
  22. $params ['signature'] = $signature_base64;
  23. } else {
  24. $log->LogInfo ( ">>>>>签名失败<<<<<<<" );
  25. }
  26. $log->LogInfo ( '=====签名报文结束======' );
  27. }
  28. function verify($params) {
  29. global $log;
  30. $public_key = getPulbicKeyByCertId ( $params ['certId'] );
  31. $signature_str = $params ['signature'];
  32. unset ( $params ['signature'] );
  33. $params_str = coverParamsToString ( $params );
  34. $log->LogInfo ( '报文去[signature] key=val&串>' . $params_str );
  35. $signature = base64_decode ( $signature_str );
  36. $params_sha1x16 = sha1 ( $params_str, FALSE );
  37. $log->LogInfo ( '摘要shax16>' . $params_sha1x16 );
  38. $isSuccess = openssl_verify ( $params_sha1x16, $signature,$public_key, OPENSSL_ALGO_SHA1 );
  39. $log->LogInfo ( $isSuccess ? '验签成功' : '验签失败' );
  40. return $isSuccess;
  41. }
  42. function getPulbicKeyByCertId($certId) {
  43. global $log;
  44. $log->LogInfo ( '报文返回的证书ID>' . $certId );
  45. $cert_dir = SDK_VERIFY_CERT_DIR;
  46. $log->LogInfo ( '验证签名证书目录 :>' . $cert_dir );
  47. $handle = opendir ( $cert_dir );
  48. if ($handle) {
  49. while ( $file = readdir ( $handle ) ) {
  50. clearstatcache ();
  51. $filePath = $cert_dir . '/' . $file;
  52. if (is_file ( $filePath )) {
  53. if (pathinfo ( $file, PATHINFO_EXTENSION ) == 'cer') {
  54. if (getCertIdByCerPath ( $filePath ) == $certId) {
  55. closedir ( $handle );
  56. $log->LogInfo ( '加载验签证书成功' );
  57. return getPublicKey ( $filePath );
  58. }
  59. }
  60. }
  61. }
  62. $log->LogInfo ( '没有找到证书ID为[' . $certId . ']的证书' );
  63. } else {
  64. $log->LogInfo ( '证书目录 ' . $cert_dir . '不正确' );
  65. }
  66. closedir ( $handle );
  67. return null;
  68. }
  69. function getCertId($cert_path) {
  70. $pkcs12certdata = file_get_contents ( $cert_path );
  71. openssl_pkcs12_read ( $pkcs12certdata, $certs, SDK_SIGN_CERT_PWD );
  72. $x509data = $certs ['cert'];
  73. openssl_x509_read ( $x509data );
  74. $certdata = openssl_x509_parse ( $x509data );
  75. $cert_id = $certdata ['serialNumber'];
  76. return $cert_id;
  77. }
  78. function getCertIdByCerPath($cert_path) {
  79. $x509data = file_get_contents ( $cert_path );
  80. openssl_x509_read ( $x509data );
  81. $certdata = openssl_x509_parse ( $x509data );
  82. $cert_id = $certdata ['serialNumber'];
  83. return $cert_id;
  84. }
  85. function getSignCertId() {
  86. return getCertId ( SDK_SIGN_CERT_PATH );
  87. }
  88. function getEncryptCertId() {
  89. return getCertIdByCerPath ( SDK_ENCRYPT_CERT_PATH );
  90. }
  91. function getPublicKey($cert_path) {
  92. return file_get_contents ( $cert_path );
  93. }
  94. function getPrivateKey($cert_path) {
  95. $pkcs12 = file_get_contents ( $cert_path );
  96. openssl_pkcs12_read ( $pkcs12, $certs, SDK_SIGN_CERT_PWD );
  97. return $certs ['pkey'];
  98. }
  99. function encryptPan($pan) {
  100. $cert_path = MPI_ENCRYPT_CERT_PATH;
  101. $public_key = getPublicKey ( $cert_path );
  102. openssl_public_encrypt ( $pan, $cryptPan, $public_key );
  103. return base64_encode ( $cryptPan );
  104. }
  105. function encryptPin($pan, $pwd) {
  106. $cert_path = SDK_ENCRYPT_CERT_PATH;
  107. $public_key = getPublicKey ( $cert_path );
  108. return EncryptedPin ( $pwd, $pan, $public_key );
  109. }
  110. function encryptCvn2($cvn2) {
  111. $cert_path = SDK_ENCRYPT_CERT_PATH;
  112. $public_key = getPublicKey ( $cert_path );
  113. openssl_public_encrypt ( $cvn2, $crypted, $public_key );
  114. return base64_encode ( $crypted );
  115. }
  116. function encryptDate($certDate) {
  117. $cert_path = SDK_ENCRYPT_CERT_PATH;
  118. $public_key = getPublicKey ( $cert_path );
  119. openssl_public_encrypt ( $certDate, $crypted, $public_key );
  120. return base64_encode ( $crypted );
  121. }
  122. function encryptDateType($certDataType) {
  123. $cert_path = SDK_ENCRYPT_CERT_PATH;
  124. $public_key = getPublicKey ( $cert_path );
  125. openssl_public_encrypt ( $certDataType, $crypted, $public_key );
  126. return base64_encode ( $crypted );
  127. }
  128. ?>