简体   繁体   中英

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.

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 .

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.)

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...

Before you call md5.ComputeHash(stream) , you need to reset the stream's position to the beginning.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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