如何创建下载的文本文件的SHA256散列 [英] How to create SHA256 hash of downloaded text file

查看:129
本文介绍了如何创建下载的文本文件的SHA256散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,可以将URL传到一个文件(例如www.documents.com/docName.txt),我想为该文件创建一个散列。我怎么做到这一点。

I have a project where I get the url to a file (e.g. www.documents.com/docName.txt) and I want to create a hash for that file. How can I do this.

FileStream filestream;
SHA256 mySHA256 = SHA256Managed.Create();

filestream = new FileStream(docUrl, FileMode.Open);

filestream.Position = 0;

byte[] hashValue = mySHA256.ComputeHash(filestream);

Label2.Text = BitConverter.ToString(hashValue).Replace("-", String.Empty);

filestream.Close();

这是我必须创建散列的代码。但看到它如何使用文件流它使用存储在硬盘驱动器上的文件(例如c:/documents/docName.txt)但我需要它使用URL到文件而不是驱动器上文件的路径。 p>

This is the code i have to create a hash. But seeing how it uses filestream it uses files stored on the hard drive (e.g. c:/documents/docName.txt) But i need it to work with an url to a file and not the path to a file on the drive.

推荐答案

要下载文件,请使用:

To download the file use:

string url = "http://www.documents.com/docName.txt";
string localPath = @"C://Local//docName.txt"

using (WebClient client = new WebClient())
{
    client.DownloadFile(url, localPath);
}

然后阅读您喜欢的文件:

then read the file like you have:

FileStream filestream;
SHA256 mySHA256 = SHA256Managed.Create();

filestream = new FileStream(localPath, FileMode.Open);

filestream.Position = 0;

byte[] hashValue = mySHA256.ComputeHash(filestream);

Label2.Text = BitConverter.ToString(hashValue).Replace("-", String.Empty);

filestream.Close();

这篇关于如何创建下载的文本文件的SHA256散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆