c# - 需要在c#中计算存储在Azure存储中的文件的SHA1哈希值

标签 c# azure azure-storage azure-blob-storage

我正在将大文件(1-10 GB)上传到azure存储,并且需要在上传时计算文件的SHA1哈希值。我是否能够在服务器上计算 SHA1,而无需下载文件?

最佳答案

Azure Blob存储支持在放入blob时自动对blob进行MD5哈希计算,请参见Get Blob Properties下面的内容。

Content-MD5

If the Content-MD5 header has been set for the blob, this response header is returned so that the client can check for message content integrity. In version 2012-02-12 and newer, Put Blob sets a block blob’s MD5 value even when the Put Blob request doesn’t include an MD5 header.

因此,如果没有特殊需要,则无需计算 blob 的 SHA1 哈希值。

作为引用,这里是一个计算 SHA1 哈希值的示例,无需下载存储在存储中的 blob。

同步

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("<StorageAccountConnectionString>");
CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
CloudBlobContainer  container      = blobClient.GetContainerReference("<container-name>");
CloudBlob           blob           = container.GetBlobReference("<blob-name>");

using(Stream blobStream = blob.OpenRead())
{
    using (SHA1 sha1 = SHA1.Create())
    {
        byte[] checksum = sha1.ComputeHash(blobStream);
    }
}

异步:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("<StorageAccountConnectionString>");
CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
CloudBlobContainer  container      = blobClient.GetContainerReference("<container-name>");
CloudBlob           blob           = container.GetBlobReference("<blob-name>");

using(Stream blobStream = await blob.OpenReadAsync().ConfigureAwait(false))
{
    using (SHA1 sha1 = SHA1.Create())
    {
        byte[] checksum = await sha1.ComputeHashAsync(blobStream);
    }
}

// ComputeHashAsync extension method from https://www.tabsoverspaces.com/233439-computehashasync-for-sha1
public static async Task<Byte[]> ComputeHashAsync(this HashAlgorithm algo, Stream stream, Int32 bufferSize = 4096)
{
    algo.Initialize();

    var buffer = new byte[bufferSize];
    var streamLength = inputStream.Length;
    while (true)
    {
        var read = await inputStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
        if (inputStream.Position == streamLength)
        {
            algo.TransformFinalBlock(buffer, 0, read);
            break;
        }
        algo.TransformBlock(buffer, 0, read, default(byte[]), default(int));
    }

    return algo.Hash;
} 

关于c# - 需要在c#中计算存储在Azure存储中的文件的SHA1哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38400377/

相关文章:

c# - 如何定义配置节

node.js - 需要帮助使用 NodeJS 从 Azure Blob 下载图像

azure - Azure 文件存储中的检测更改

c# - Windows azure REST API 列出容器问题

c# - 新的 MVC 4 项目。默认路由被忽略

c# - Windows 工作流基础中基于人工的任务

C# Outlook 加载项数组从 1 开始?

.net - 将 ASP.net MVC 网站部署到 Azure 网站的子文件夹?

java - 使用 RestTemplate 进行外部 REST 端点调用时出错

c# - 向 LUIS 发送示例消息?