【问题标题】:Download file and check integrity下载文件并检查完整性
【发布时间】:2013-06-03 17:46:45
【问题描述】:

我有一台运行 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);

现在,完整性检查来了。 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吗?
  • 你的意思是来自MySQL?好吧,我没有将文件上传到 MySQL,我只是将其上传到目录中......

标签: c# php zip download checksum


【解决方案1】:

我遇到了类似的问题,问题不在于校验和算法。
问题是下载的文件与上传的文件不同。

首先,我会检查文件上传是否与您存储在 ddbb 中的文件相同。

在 MySQL 中: SELECT fileName, md5(fileBlob) FROM yourTable;

其次,我会检查您下载的文件是否与您上传的文件相同。

你可以在这里检查完整性http://onlinemd5.com/

我确实意识到我正在开发的 REST 进行了错误的转换。

我知道现在已经晚了,但希望它会有所帮助。

【讨论】:

    猜你喜欢
    • 2014-03-24
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    相关资源
    最近更新 更多