我有一台运行PHP的服务器,我可以在管理面板上使用自定义表单上载zip压缩文件。当我上传一个zip压缩包,我计算出它的CRC32校验和,我将其插入到我的数据库以供将来下载完整性检查:下载文件并检查完整性
$zipchecksum = strtoupper(hash_file('crc32', $zippath));
$mysqli->query("INSERT INTO files VALUES (NULL, '$zipname', '$zippath')");
当我查询服务器的可用文件中使用我的C#客户端下载,我也收到校验的响应,如果新文件是可用的,我下载:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Properties.Settings.Default.URLDownload);
request.Accept = "application/octet-stream";
request.ContentType = "application/x-www-form-urlencoded";
request.Method = WebRequestMethods.Http.Post;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (MemoryStream memoryStream = new MemoryStream())
{
responseStream.CopyTo(memoryStream);
Crc32 crc = new Crc32();
crc.Update(memoryStream.GetBuffer());
if (crc.Value.ToString("X") != m_ServerData.CRC)
throw new Exception("Malformed zip file received!");
ZipFile file = new ZipFile(memoryStream);
// ...
}
这里是我的回应:
header('Cache-Control: must-revalidate, pre-check=0, post-check=0');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$zipname.'"');
header('Content-Length: '.filesize($filepath));
header('Content-Transfer-Encoding: binary');
header('Content-Type: application/octet-stream');
header('Expires: 0');
header('Pragma: public');
readfile($zippath);
现在,来这里的INTE grity检查。由PHP计算的CRC32校验和与我在我的客户端使用ICSharpCode.SharpZipLib中的Crc32类计算出的完全不同。例如:
PHP服务器 - > CF83A609
C#的客户端 - > 3FB78619
我还试图用另一个PHP函数:
$zipcontents = file_get_contents($zippath);
$crc = crc32($zipcontents);
$zipchecksum = strtoupper(dechex(crc32($zipcontents)));
$mysqli->query("INSERT INTO files VALUES (NULL, '$zipname', '$zippath')");
但结果是一次再次不同。
PHP服务器 - > A6A642D0
C#的客户端 - > 3FB78619
所以看起来像PHP使用计算校验从客户端从服务器接收到一个不同的缓冲区。那么......任何人都有解决方案?
您是否尝试过计算数据库中的CRC32? – Romoku
从MySQL你的意思?那么我没有上传到MySQL的文件,我只上传到一个目录中... –