public static void HashTagFile(string path, G2Protocol protocol, ref byte[] hash, ref long size) { using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { // record size of file long originalSize = file.Length; // sha hash 128k chunks of file SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider(); int read = 0; int chunkSize = 128; // 128kb chunks int chunkBytes = chunkSize * 1024; int buffSize = file.Length > chunkBytes ? chunkBytes : (int)file.Length; byte[] chunk = new byte[buffSize]; List<byte[]> hashes = new List<byte[]>(); read = 1; while (read > 0) { read = file.Read(chunk, 0, buffSize); if (read > 0) hashes.Add(sha.ComputeHash(chunk, 0, read)); } // write packets - 200 sub-hashes per packet int writePos = 0; int hashesLeft = hashes.Count; while (hashesLeft > 0) { int writeCount = (hashesLeft > 100) ? 100 : hashesLeft; hashesLeft -= writeCount; SubHashPacket packet = new SubHashPacket(); packet.ChunkSize = chunkSize; packet.TotalCount = hashes.Count; packet.SubHashes = new byte[20 * writeCount]; for (int i = 0; i < writeCount; i++) hashes[writePos++].CopyTo(packet.SubHashes, 20 * i); byte[] encoded = packet.Encode(protocol); file.Write(encoded, 0, encoded.Length); } // write null - end packets file.WriteByte(0); // attach original size to end of file byte[] sizeBytes = BitConverter.GetBytes(originalSize); file.Write(sizeBytes, 0, sizeBytes.Length); // sha1 hash tagged file file.Seek(0, SeekOrigin.Begin); hash = sha.ComputeHash(file); size = file.Length; } }
public static void HashTagFile(string path, G2Protocol protocol, ref byte[] hash, ref long size) { using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { // record size of file long originalSize = file.Length; // sha hash 128k chunks of file SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider(); int read = 0; int chunkSize = 128; // 128kb chunks int chunkBytes = chunkSize * 1024; int buffSize = file.Length > chunkBytes ? chunkBytes : (int)file.Length; byte[] chunk = new byte[buffSize]; List <byte[]> hashes = new List <byte[]>(); read = 1; while (read > 0) { read = file.Read(chunk, 0, buffSize); if (read > 0) { hashes.Add(sha.ComputeHash(chunk, 0, read)); } } // write packets - 200 sub-hashes per packet int writePos = 0; int hashesLeft = hashes.Count; while (hashesLeft > 0) { int writeCount = (hashesLeft > 100) ? 100 : hashesLeft; hashesLeft -= writeCount; SubHashPacket packet = new SubHashPacket(); packet.ChunkSize = chunkSize; packet.TotalCount = hashes.Count; packet.SubHashes = new byte[20 * writeCount]; for (int i = 0; i < writeCount; i++) { hashes[writePos++].CopyTo(packet.SubHashes, 20 * i); } byte[] encoded = packet.Encode(protocol); file.Write(encoded, 0, encoded.Length); } // write null - end packets file.WriteByte(0); // attach original size to end of file byte[] sizeBytes = BitConverter.GetBytes(originalSize); file.Write(sizeBytes, 0, sizeBytes.Length); // sha1 hash tagged file file.Seek(0, SeekOrigin.Begin); hash = sha.ComputeHash(file); size = file.Length; } }