mirror of
https://github.com/SabreTools/SabreTools.Hashing.git
synced 2026-09-21 22:35:04 +00:00
Add stream GetStandardHashes variant
This commit is contained in:
@@ -32,7 +32,7 @@ namespace SabreTools.Hashing.Test
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetStandardHashesTest()
|
||||
public void GetStandardHashesFileTest()
|
||||
{
|
||||
bool gotHashes = HashTool.GetStandardHashes(_hashFilePath, out long actualSize, out string? crc32, out string? md5, out string? sha1);
|
||||
|
||||
@@ -43,6 +43,19 @@ namespace SabreTools.Hashing.Test
|
||||
TestHelper.ValidateHash(HashType.SHA1, sha1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetStandardHashesStreamTest()
|
||||
{
|
||||
var fileStream = File.OpenRead(_hashFilePath);
|
||||
bool gotHashes = HashTool.GetStandardHashes(fileStream, out long actualSize, out string? crc32, out string? md5, out string? sha1);
|
||||
|
||||
Assert.True(gotHashes);
|
||||
TestHelper.ValidateSize(actualSize);
|
||||
TestHelper.ValidateHash(HashType.CRC32, crc32);
|
||||
TestHelper.ValidateHash(HashType.MD5, md5);
|
||||
TestHelper.ValidateHash(HashType.SHA1, sha1);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFileHashesParallelTest()
|
||||
{
|
||||
|
||||
@@ -35,6 +35,29 @@ namespace SabreTools.Hashing
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get CRC-32, MD5, and SHA-1 hashes from an input stream
|
||||
/// </summary>
|
||||
/// <param name="stream">Input stream</param>
|
||||
/// <returns>True if hashing was successful, false otherwise</returns>
|
||||
public static bool GetStandardHashes(Stream stream, out long size, out string? crc32, out string? md5, out string? sha1)
|
||||
{
|
||||
// Set all initial values
|
||||
crc32 = null; md5 = null; sha1 = null;
|
||||
|
||||
// Get all file hashes
|
||||
HashType[] standardHashTypes = [HashType.CRC32, HashType.MD5, HashType.SHA1];
|
||||
var fileHashes = GetStreamHashesAndSize(stream, standardHashTypes, out size);
|
||||
if (fileHashes == null)
|
||||
return false;
|
||||
|
||||
// Assign the file hashes and return
|
||||
crc32 = fileHashes[HashType.CRC32];
|
||||
md5 = fileHashes[HashType.MD5];
|
||||
sha1 = fileHashes[HashType.SHA1];
|
||||
return true;
|
||||
}
|
||||
|
||||
#region File Hashes Without Size
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user