简体   繁体   English

Azure存储的文件具有与本地文件不同的MD5校验和(属于同一文件)

[英]Azure stored file has different MD5 checksum than local file (being same file)

I'm using a could service to upload files to an Azure Storage service, so I want to check the file's integrity using MD5 checksum, so first I get the checksum from a function. 我正在使用罐头服务将文件上传到Azure存储服务,因此我想使用MD5校验和检查文件的完整性,因此首先我从函数中获取校验和。

public static string GetMD5HashFromFile(Stream stream)
{
    using (var md5 = MD5.Create())
    {
        return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty);
    }
}

for the test file I'm using I'm getting: 1dffc245282f4e0a45a9584fe90f12f2 and I got the same result when I use an online tool like this . 为测试文件我用我越来越:1dffc245282f4e0a45a9584fe90f12f2,我得到了相同的结果时,我喜欢使用的在线工具

Then I upload the file to Azure and get it from my code like this: (In order to avoid include the validations let's assume the file and directories do exist.) 然后,我将文件上传到Azure并从我的代码中获取它,如下所示:(为了避免包含验证,我们假设文件和目录确实存在。)

public bool CompareCheckSum(string fileName, string checksum)
{
    this.storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("MyConnectionString"));
    this.fileClient = this.storageAccount.CreateCloudFileClient();
    this.shareReference = this.fileClient.GetShareReference(CloudStorageFileShareSettings.StorageFileShareName);
    this.rootDir = this.shareReference.GetRootDirectoryReference();
    this.directoryReference = this.rootDir.GetDirectoryReference("MyDirectory");
    this.fileReference = this.directoryReference.GetFileReference(fileName);

    Stream stream = new MemoryStream();
    this.fileReference.DownloadToStream(stream);
    string azureFileCheckSum = GetMD5HashFromFile(stream);

    return azureFileCheckSum.ToLower() == checksum.ToLower();
}

I also tried to get the checksum using a different process like this: 我还尝试使用不同的过程来获取校验和,如下所示:

public bool CompareCheckSum(string fileName, string checksum)
{
    this.storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("MyConnectionString"));
    this.fileClient = this.storageAccount.CreateCloudFileClient();
    this.shareReference = this.fileClient.GetShareReference(CloudStorageFileShareSettings.StorageFileShareName);
    this.rootDir = this.shareReference.GetRootDirectoryReference();
    this.directoryReference = 
    this.rootDir.GetDirectoryReference("MyDirectory");
    this.fileReference = this.directoryReference.GetFileReference(fileName);

    this.fileReference.FetchAttributes();
    string azureFileCheckSum = this.fileReference.Metadata["md5B64"];

    return azureFileCheckSum.ToLower() == checksum.ToLower();  
}

Finally, for the azureFileCheckSum I'm getting: d41d8cd98f00b204e9800998ecf8427e not sure if am I doing something wrong or if something change when I upload the file to the ftp... 最后,对于azureFileCheckSum,我得到:d41d8cd98f00b204e9800998ecf8427e不确定将文件上传到ftp时我做错了什么还是改变了...

Before you call md5.ComputeHash(stream) , you need to reset the stream's position to the beginning. 在调用md5.ComputeHash(stream) ,您需要将流的位置重置为开头。

stream.Position = 0;

Of course, this will fail with a NotSupportedException if the stream type doesn't support seeking, but in your case it should work. 当然,如果流类型不支持查找,则将失败并显示NotSupportedException ,但是在您的情况下它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM