[英]Azure stored file has different MD5 checksum than local file (being same file)
我正在使用罐頭服務將文件上傳到Azure存儲服務,因此我想使用MD5校驗和檢查文件的完整性,因此首先我從函數中獲取校驗和。
public static string GetMD5HashFromFile(Stream stream)
{
using (var md5 = MD5.Create())
{
return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", string.Empty);
}
}
為測試文件我用我越來越:1dffc245282f4e0a45a9584fe90f12f2,我得到了相同的結果時,我喜歡使用的在線工具本 。
然后,我將文件上傳到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();
}
我還嘗試使用不同的過程來獲取校驗和,如下所示:
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();
}
最后,對於azureFileCheckSum,我得到:d41d8cd98f00b204e9800998ecf8427e不確定將文件上傳到ftp時我做錯了什么還是改變了...
在調用md5.ComputeHash(stream)
,您需要將流的位置重置為開頭。
stream.Position = 0;
當然,如果流類型不支持查找,則將失敗並顯示NotSupportedException
,但是在您的情況下它應該可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.