2017-08-04 3 views
1

私はCanサービスを使用してAzureストレージサービスにファイルをアップロードしています。そのため、MD5チェックサムを使用してファイルの整合性をチェックします関数からのチェックサム。私はthisのようなオンラインツールを使用するとき1dffc245282f4e0a45a9584fe90f12f2と私は同じ結果を得た:私は取得しています使用していたテストファイルのAzureの保存ファイルのローカルファイル(同じファイル)と異なるMD5チェックサム

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

。 (避けるためにのファイルを仮定しようとディレクトリが存在しない検証が含まれています。)私も取得しよう

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のために私が取得しています:私は、FTPにファイルをアップロードする場合であればd41d8cd98f00b204e9800998ecf8427eわからない私が何か​​間違った場合、または何かの変更をしています...

答えて

2

md5.ComputeHash(stream)に電話する前に、ストリームの位置を最初にリセットする必要があります。もちろん

stream.Position = 0; 

、ストリームタイプが求めてサポートしていない場合、これはNotSupportedExceptionで失敗しますが、あなたの場合にはそれが動作するはずです。

+0

素晴らしい!それは働いて、ありがとう! –

+0

Proのヒント: 'd41d8cd98f00b204e9800998ecf8427e'はゼロバイトのMD5ハッシュですので、簡単に見つけることができます。 –

関連する問題