计算哈希在保存文件?

编程入门 行业动态 更新时间:2024-10-24 15:23:08
本文介绍了计算哈希在保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个的InputStream ,我想用它来计算哈希值,将文件保存到磁盘。我想知道如何有效地做到这一点。我应该用一些任务,这样做的同时,我要重复的流通到两个流,一个是在 saveFile的方法,一个是 computeHash 的方法,或者我应该做点别的?

I have an inputStream that I want to use to compute a hash and save the file to disk. I would like to know how to do that efficiently. Should I use some task to do that concurrently, should I duplicate the stream pass to two streams, one for the the saveFile method and one for thecomputeHash method, or should I do something else?

推荐答案

怎么样使用其上运行的散列算法块级?您可将块添加到哈希值(使用TransformBlock),随后写块流中的文件的foreach块

What about using a hash algorithms that operate on a block level? You can add the block to the hash (using the TransformBlock) and subsequently write the block to the file foreach block in the stream.

未经测试的粗糙的镜头:

Untested rough shot:

using System.IO; using System.Security.Cryptography; ... public byte[] HashedFileWrite(string filename, Stream input) { var hash_algorithm = MD5.Create(); using(var file = File.OpenWrite(filename)) { byte[] buffer = new byte[4096]; int read = 0; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { hash_algorithm.TransformBlock(buffer, 0, read, null, 0); file.Write(buffer, 0, read); } hash_algorithm.TransformFinalBlock(buffer, 0, read); } return hash_algorithm.Hash; }

更多推荐

计算哈希在保存文件?

本文发布于:2023-11-03 12:32:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555144.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:保存文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!