Skip to content

Commit

Permalink
Improved hash search
Browse files Browse the repository at this point in the history
  • Loading branch information
Neodymium146 committed Jun 17, 2017
1 parent 54ccb8a commit 7073b91
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions RageLib/Helpers/HashSearch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright(c) 2015 Neodymium
Copyright(c) 2017 Neodymium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,37 +31,49 @@ namespace RageLib.Helpers
public static class HashSearch
{
private const int BLOCK_LENGTH = 1048576;
private const int ALIGN_LENGTH = 16;

public static byte[] SearchHash(Stream stream, byte[] hash, int length = 32)
public static byte[] SearchHash(Stream stream, byte[] hash, int alignment = 1, int length = 32)
{
return SearchHashes(stream, new List<byte[]> { hash }, length)[0];
return SearchHashes(stream, new List<byte[]> { hash }, alignment, length)[0];
}

public static byte[][] SearchHashes(Stream stream, IList<byte[]> hashes, int length = 32)
public static byte[][] SearchHashes(Stream stream, IList<byte[]> hashes, int alignment = 1, int length = 32)
{
var buf = new byte[stream.Length];
stream.Position = 0;
stream.Read(buf, 0, buf.Length);

var result = new byte[hashes.Count][];

Parallel.For(0, (int)(stream.Length / BLOCK_LENGTH), (int k) => {

var tmp = new byte[length];

var hashProvider = new SHA1CryptoServiceProvider();
var buffer = new byte[length];
for (int i = 0; i < (BLOCK_LENGTH / ALIGN_LENGTH); i++)
//var buffer = new byte[length];
for (int i = 0; i < (BLOCK_LENGTH / alignment); i++)
{
var position = k * BLOCK_LENGTH + i * ALIGN_LENGTH;
var position = k * BLOCK_LENGTH + i * alignment;
if (position >= stream.Length)
continue;

lock (stream)


//lock (stream)
//{
// stream.Position = position;
// stream.Read(buffer, 0, length);
//}
for (int t = 0; t < length; t++)
{
stream.Position = position;
stream.Read(buffer, 0, length);
tmp[t] = buf[position + t];
}

var hash = hashProvider.ComputeHash(buffer);

var hash = hashProvider.ComputeHash(tmp);
for (int j = 0; j < hashes.Count; j++)
if (hash.SequenceEqual(hashes[j]))
result[j] = (byte[])buffer.Clone();
result[j] = (byte[])tmp.Clone();
}


Expand Down

0 comments on commit 7073b91

Please sign in to comment.