如何通过文本文件读取代码 [英] How do I make my code read through a textfile

查看:53
本文介绍了如何通过文本文件读取代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!正如你们中的一些人已经知道了,我正在研究一个比较MD5哈希的软件,我的问题是我如何通过我指导的文件读取我的代码?



  public   string  MD5HashFile( string  fn)
{
byte [] hash = MD5.Create()。ComputeHash(File。 ReadAllBytes(FN));
return BitConverter.ToString(hash).Replace( - );

}


private Stream TestStream()
{
Stream fs = File.OpenRead( @ C:\ PathToDictionary);
return fs;
}

public string GetMD5( string file)
{
using var md5 = MD5.Create())
使用 var stream = File.OpenRead (TestStream))
return Encoding.Default.GetString(md5.ComputeHash(stream));
}







这是我目前的代码,正如您可能已经想到的那样,给我一个错误。



更具体地说它告诉我



我的TestStream()方法已经返回一个Stream而File.OpenRead用于创建一个新的Stream,这里不需要,因为TestStream()已经是Stream了。我可以替换使用(var stream = ..... line by var stream = TestStream()



但是...当我这样做时它还在扔我同样的错误,所以我在这里错过了什么?不要轻易对我说,如果有一些我错过的明显的东西,请随时告诉我!



我尝试了什么:



我试过替换使用(var stream = ..... line with var stream = TestStream()

解决方案

为什么不直接说:

  string  s = File.ReadAllText(pathToFile); 





  byte  [] rawData = File.ReadAllBytes(pathToFile); 

对于MD5,我会使用后者并生成原始字节数据的MD5,而不是使用字符串。



这听起来像是更简单的解决方案,我喜欢简洁的东西,但是如何让代码比较我用我的文本文件中的那个计算的文件?我有一个超过26k哈希的文本文件将被比较,所以我的目标是比较我计算的哈希值和文本文件中的哈希值,你明白吗?





有很多不同的方法可以做到这一点,但我可能会做的是使用字典作为初学者。

因为任何一个文件只能有一个哈希值,我会成对地将数据存储在文件中,用文件名中非法的字符分隔:'?'例如。



 path / to / file?hashValue 
path / to / other / file?otherHashValue





然后我用File.ReadAllLines读取文件 - 它给你一个数组,每一对都在一个单独的字符串中。

使用Split来破坏对,并将它们添加到Dictionary< string,string>以便Key是文件的路径(因为它必须是唯一的),而MD5是Value。

然后当你生成一个MD5用于比较它非常简单:

  if (myDictionaryOfFilesAndHashes [pathToFile] == calculatedMD5HashValueAsAString)
{
...
}



有意义吗?


用于阅读文件,更好使用 System.IO.StreamReader

StreamReader类(System.IO) [ ^ ]。



不要忘记使用语句在下创建它的实例:使用Statement(C#Reference) [ ^ ]。



你真正的问题是阅读MD5。你没有说出存储哈希的格式,所以我不能说它是如何真正被读取的。您只需要存储哈希的字节。如果你真的想要使用文本文件,它应该不是字节,而是某种字节的字符串表示,比如十六进制形式。在这种方法中,您应该读取整个字符串,然后尝试解析它到字节序列。问题很简单,只有你不应该以盲目的方式调用 ComputeHash 。如果你有关于如何存储哈希值的更多细节但不确定究竟要做什么,我可能会帮助你完成下一步。



或者,使用二进制文件。



另外,不要忘记MD5不适用于加密和安全敏感的应用程序;它被发现破了:

MD5 - 维基百科,免费的百科全书 [ ^ ],

另见:加密哈希函数 - 维基百科,免费的百科全书 [ ^ ],

HashAlgorithm类(System.Security.Cryptography) [ ^ ]。



-SA

Hello! As some of you already migth know, I am currently working on a software taht compares MD5 Hashes, my question is how do I make my code read through the file I am directing it to?

        public string MD5HashFile(string fn)
        {
            byte[] hash = MD5.Create().ComputeHash(File.ReadAllBytes(fn));
            return BitConverter.ToString(hash).Replace("-", "");

        }


private Stream TestStream()
{
    Stream fs = File.OpenRead(@"C:\PathToDictionary");
    return fs;
}

public string GetMD5(string file)
{
    using (var md5 = MD5.Create())
    using (var stream = File.OpenRead(TestStream))
    return Encoding.Default.GetString(md5.ComputeHash(stream));
}




This is my current code and as you probably already figured out, its throwing me an error.

To be more specific its telling me that

My TestStream() method already returns a Stream and File.OpenRead is for creating a new Stream which is not needed here because TestStream() is already a Stream. I can replace the using(var stream=..... line by var stream = TestStream()

But... When I do that its still throwing me the same error so what did I miss here? And dont go easy on me, if there is something obvious that I missed please feel free to tell me!

What I have tried:

Ive tried replace the using(var stream=..... line with var stream = TestStream()

解决方案

Why not just say either:

string s = File.ReadAllText(pathToFile);


or

byte[] rawData = File.ReadAllBytes(pathToFile);

For MD5, I'd use the later and generate an MD5 of the raw byte data instead of playing with strings.

"This sounds like the easier solution, and I like things to be cleana nd simple, but how would I make the code compare the hash file that I compute with the one from my text file? I have a text file with over 26k hashes that is going to be compares, so my goal is to compare the hash that I compute with the hashes from the text file, do you understand?"


There are a load of different ways to do that, but what I'd probably do is use a Dictionary for starters.
Because there can only be one hash value for any one file, I'd store the data in the file in pairs, separated by a character illegal in file names: '?' for example.

path/to/file?hashValue
path/to/other/file?otherHashValue



Then I'd read the file using File.ReadAllLines - that gives you an array, with each pair in a separate string.
Use Split to "break" the pairs, and add them to a Dictionary<string, string> so that the Key was the path to the file (since this has to be unique) and the MD5 is the Value.
Then when you generate a MD5 for comparision it's pretty trivial:

if (myDictionaryOfFilesAndHashes[pathToFile] == calculatedMD5HashValueAsAString)
    {
    ...
    }


Make sense?


For reading a file, better use System.IO.StreamReader:
StreamReader Class (System.IO)[^].

Don't forget to create it's instance under the using statement: using Statement (C# Reference)[^].

Your real problem is reading MD5. You did not say anything about the format the hash is stored in, so I cannot say how it can really be read. All you need is to store the bytes of the hash. If you really want to use the text file, it should be not bytes, but some kind of string representation of bytes, say, in hexadecimal form. In this approach, you should read, say, the whole string and then try to parse it to the sequence of bytes. The problem is very simple, only you should not call ComputeHash in a blind-folded manner. If you have more detail on how the hash value is stored but are not sure what exactly to do, I'll probably will be able to help you with your next step.

Alternatively, use a "binary" file.

Also, don't forget that MD5 is not suitable for cryptography and security-sensitive application; it was found broken:
MD5 — Wikipedia, the free encyclopedia[^],
see also: Cryptographic hash function — Wikipedia, the free encyclopedia[^],
HashAlgorithm Class (System.Security.Cryptography)[^].

—SA


这篇关于如何通过文本文件读取代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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