public static async Task <byte[]> ReadToFileAndHashAsync(Stream networkStream, string filePath, long fileLength)
        {
            var folder = Path.GetDirectoryName(filePath);

            if (folder == null)
            {
                throw new InvalidOperationException($"Failed to get file '{filePath}' folder");
            }

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            if (fileLength == 0)
            {
                return(XxHash64Callback.EmptyHash);
            }

            using (var fileStream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None, ReadBufferSize, FileOptions.WriteThrough | FileOptions.Asynchronous))
            {
                async Task WriteToFile(byte[] bytes, int length)
                {
                    await fileStream.WriteAsync(bytes, 0, length);
                }

                return(await XxHash64Callback.ComputeHash(networkStream, ChunkSize, fileLength, WriteToFile));
            }
        }
 private static async Task <byte[]> HashFileAsync(string filePath, long fileLength)
 {
     using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, ReadBufferSize, FileOptions))
         using (var bufferedStream = new BufferedStream(fileStream, ReadBufferSize))
         {
             return(await XxHash64Callback.ComputeHash(bufferedStream, ChunkSize, fileLength, (bytes, i) => Task.CompletedTask));
         }
 }
        public static async Task <byte[]> WriteFromFileAndHashAsync(NetworkStream networkStream, string filePath, int fileLength)
        {
            async Task WriteToNetwork(byte[] bytes, int length)
            {
                await networkStream.WriteAsync(bytes, 0, length);
            }

            using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, ReadBufferSize, FileOptions))
                using (var bufferedStream = new BufferedStream(fileStream, ReadBufferSize))
                {
                    return(await XxHash64Callback.ComputeHash(bufferedStream, ChunkSize, fileLength, WriteToNetwork));
                }
        }
        public static async Task <byte[]> HashFileAsync(FileInfo inf)
        {
            if (inf.Length == 0)
            {
                return(XxHash64Callback.EmptyHash);
            }

            using (var fileStream = new FileStream(inf.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, ReadBufferSize, FileOptions))
                using (var bufferedStream = new BufferedStream(fileStream, ReadBufferSize))
                {
                    return(await XxHash64Callback.ComputeHash(bufferedStream, ChunkSize, inf.Length, (bytes, i) => Task.CompletedTask));
                }
        }