Reputation: 111
I want to append byte array to stream i opened but just to pass it to hash algorithm and compute the hash, something like:
byte[] data;
using (BufferedStream bs = new BufferedStream(File.OpenRead(path), 1200000))
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(data + bs);
}
I don't know how to do "data + bs" or "bs + data" part... Any idea about this?
Upvotes: 0
Views: 596
Reputation: 1284
// here is a quick approach to your original question:
List<byte> raw = new List<byte>(data);
raw.AddRange(File.ReadAllBytes("filePath.ext"));
To do this without using List (in other words arrays), use Array.Copy(...) to copy the first and second arrays into a new array of a larger size. (Personally, I prefer Lists in C#, Vectors in C++) If you have 1200000 bytes max and don't want to pull everything from the file, then you could use this code to your advantage: Best way to read a large file into a byte array in C#? where numBytes
is the number of bytes you want to read in.
Upvotes: 0
Reputation: 74375
Why wouldn't you simply do something like this:
public static byte[] MD5Hash( byte[] salt, Stream s , int blockSize )
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider() ;
md5.Initialize();
md5.TransformBlock(salt,0,salt.Length,salt,0) ;
byte[] buf = new byte[blockSize];
int bufl ;
while ( (bufl=s.Read( buf , 0 , buf.Length )) > 0 )
{
md5.TransformBlock(buf,0,bufl,buf,0) ;
}
md5.TransformFinalBlock(buf,0,0) ;
return md5.Hash ;
}
Upvotes: 2
Reputation: 9165
Do this:
byte[] hash = md5.ComputeHash(data + ReadToEnd(bs));
And, your ReadToEnd function:
public static byte[] ReadToEnd(Stream input)
{
byte[] buffer = new byte[16*1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
Upvotes: 0