我正在构建一个通过比较哈希来扫描文件的应用程序。我需要搜索超过 1GB 的哈希值来获取文件的哈希值。我为此找到了其他解决方案,例如 Aho-Corasick,但它比File.ReadLines(file).Contains(str).
这是迄今为止最快的代码,使用File.ReadLines. 扫描一个文件大约需要 8 秒,而使用 Aho-Corasick 扫描一个文件大约需要 2 分钟。由于显而易见的原因,我无法将整个哈希文件读入内存。
IEnumerable<DirectoryInfo> directories = new DirectoryInfo(scanPath).EnumerateDirectories();
IEnumerable<FileInfo> files = new DirectoryInfo(scanPath).EnumerateFiles();
FileInfo hashes = new FileInfo(hashPath);
await Task.Run(() =>
{
IEnumerable<string> lines = File.ReadLines(hashes.FullName);
foreach (FileInfo file in files) {
if (!AuthenticodeTools.IsTrusted(file.FullName))
{
string hash = getHash(file.FullName);
if (lines.Contains(hash)) flaggedFiles.Add(file.FullName);
}
filesScanned += 1;
}
});
foreach (DirectoryInfo directory in directories)
{
await scan(directory.FullName, hashPath);
directoriesScanned += 1;
}
Run Code Online (Sandbox Code Playgroud)
编辑:根据请求,以下是文件内容的示例:
5c269c9ec0255bbd9f4e20420233b1a7
63510b1eea36a23b3520e2b39c35ef4e
0955924ebc1876f0b849b3b9e45ed49d
Run Code Online (Sandbox Code Playgroud)
它们是 …