🐛 Fix file hash overflow
This commit is contained in:
@@ -437,14 +437,13 @@ public class FileService(
|
|||||||
|
|
||||||
private static async Task<string> HashFileAsync(string filePath, int chunkSize = 1024 * 1024)
|
private static async Task<string> HashFileAsync(string filePath, int chunkSize = 1024 * 1024)
|
||||||
{
|
{
|
||||||
using var stream = File.OpenRead(filePath);
|
var fileInfo = new FileInfo(filePath);
|
||||||
var fileSize = stream.Length;
|
if (fileInfo.Length > chunkSize * 1024 * 5)
|
||||||
if (fileSize > chunkSize * 1024 * 5)
|
|
||||||
return await HashFastApproximateAsync(filePath, chunkSize);
|
return await HashFastApproximateAsync(filePath, chunkSize);
|
||||||
|
|
||||||
|
await using var stream = File.OpenRead(filePath);
|
||||||
using var md5 = MD5.Create();
|
using var md5 = MD5.Create();
|
||||||
var hashBytes = await md5.ComputeHashAsync(stream);
|
var hashBytes = await md5.ComputeHashAsync(stream);
|
||||||
stream.Position = 0; // Reset stream position after reading
|
|
||||||
return Convert.ToHexString(hashBytes).ToLowerInvariant();
|
return Convert.ToHexString(hashBytes).ToLowerInvariant();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -452,11 +451,6 @@ public class FileService(
|
|||||||
{
|
{
|
||||||
await using var stream = File.OpenRead(filePath);
|
await using var stream = File.OpenRead(filePath);
|
||||||
|
|
||||||
// Scale the chunk size to kB level
|
|
||||||
chunkSize *= 1024;
|
|
||||||
|
|
||||||
using var md5 = MD5.Create();
|
|
||||||
|
|
||||||
var buffer = new byte[chunkSize * 2];
|
var buffer = new byte[chunkSize * 2];
|
||||||
var fileLength = stream.Length;
|
var fileLength = stream.Length;
|
||||||
|
|
||||||
@@ -468,7 +462,7 @@ public class FileService(
|
|||||||
bytesRead += await stream.ReadAsync(buffer.AsMemory(chunkSize, chunkSize));
|
bytesRead += await stream.ReadAsync(buffer.AsMemory(chunkSize, chunkSize));
|
||||||
}
|
}
|
||||||
|
|
||||||
var hash = md5.ComputeHash(buffer, 0, bytesRead);
|
var hash = MD5.HashData(buffer.AsSpan(0, bytesRead));
|
||||||
stream.Position = 0; // Reset stream position
|
stream.Position = 0; // Reset stream position
|
||||||
return Convert.ToHexString(hash).ToLowerInvariant();
|
return Convert.ToHexString(hash).ToLowerInvariant();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user