using System; using System.Buffers; using System.IO; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; using System.IO.Hashing; using Blake3; using CheckHash.Models; namespace CheckHash.Services; public enum HashType { MD5, SHA1, SHA256, SHA384, SHA512, BLAKE3, XxHash32, XxHash64, XxHash3, XxHash128, CRC32 } public class HashService { private const int DefaultBufferSize = 80 * AppConstants.OneKB; private const int LargeBufferSize = AppConstants.OneMB; public async Task ComputeHashAsync(string filePath, HashType type, CancellationToken token, int? bufferSize = null, Action? progressCallback = null) { int actualBufferSize = bufferSize ?? (type == HashType.BLAKE3 ? DefaultBufferSize : LargeBufferSize); try { Stream stream = new FileStream( filePath, FileMode.Open, FileAccess.Read, FileShare.Read, actualBufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan); if (progressCallback != null) { stream = new ProgressStream(stream, progressCallback); } await using (stream) { var hashBytes = type switch { HashType.MD5 => await MD5.HashDataAsync(stream, token), HashType.SHA1 => await SHA1.HashDataAsync(stream, token), HashType.SHA256 => await SHA256.HashDataAsync(stream, token), HashType.SHA384 => await SHA384.HashDataAsync(stream, token), HashType.SHA512 => await SHA512.HashDataAsync(stream, token), HashType.BLAKE3 => await ComputeBlake3Async(stream, token, actualBufferSize), HashType.XxHash32 => await ComputeXxHash32Async(stream, token), HashType.XxHash64 => await ComputeXxHash64Async(stream, token), HashType.XxHash3 => await ComputeXxHash3Async(stream, token), HashType.XxHash128 => await ComputeXxHash128Async(stream, token), HashType.CRC32 => await ComputeCrc32Async(stream, token), _ => throw new ArgumentOutOfRangeException(nameof(type), type, "Unknown HashType") }; return Convert.ToHexString(hashBytes); } } catch (FileNotFoundException) { throw new FileNotFoundException("File corrupted or deleted."); } catch (UnauthorizedAccessException) { throw new UnauthorizedAccessException("Access denied. Run as Administrator."); } catch (IOException ex) { // Check if it's a sharing violation (HRESULT 0x80070020) var hr = Marshal.GetHRForException(ex); if ((hr & 0xFFFF) == 32) throw new IOException("File is being used by another process."); throw new IOException($"IO Error: {ex.Message}", ex); } } private async Task ComputeBlake3Async(Stream stream, CancellationToken token, int bufferSize) { using var hasher = Hasher.New(); var buffer = ArrayPool.Shared.Rent(bufferSize); try { int bytesRead; while ((bytesRead = await stream.ReadAsync(buffer.AsMemory(), token)) > 0) hasher.Update(new ReadOnlySpan(buffer, 0, bytesRead)); return hasher.Finalize().AsSpan().ToArray(); } finally { ArrayPool.Shared.Return(buffer); } } private async Task ComputeXxHash32Async(Stream stream, CancellationToken token) { var alg = new XxHash32(); await alg.AppendAsync(stream, token); var buffer = new byte[4]; alg.GetCurrentHash(buffer); return buffer; } private async Task ComputeXxHash64Async(Stream stream, CancellationToken token) { var alg = new XxHash64(); await alg.AppendAsync(stream, token); var buffer = new byte[8]; alg.GetCurrentHash(buffer); return buffer; } private async Task ComputeXxHash3Async(Stream stream, CancellationToken token) { var alg = new XxHash3(); await alg.AppendAsync(stream, token); var buffer = new byte[8]; alg.GetCurrentHash(buffer); return buffer; } private async Task ComputeXxHash128Async(Stream stream, CancellationToken token) { var alg = new XxHash128(); await alg.AppendAsync(stream, token); var buffer = new byte[16]; alg.GetCurrentHash(buffer); return buffer; } private async Task ComputeCrc32Async(Stream stream, CancellationToken token) { var alg = new Crc32(); await alg.AppendAsync(stream, token); var buffer = new byte[4]; alg.GetCurrentHash(buffer); return buffer; } }