我正在将大文件(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/