mirror of
https://github.com/SabreTools/SabreTools.Hashing.git
synced 2026-02-10 05:44:31 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7eeb11261b | ||
|
|
a0fc83b617 | ||
|
|
fbc81459ee | ||
|
|
7d53e07e90 | ||
|
|
e83c31b89c | ||
|
|
ca08f97336 | ||
|
|
bd1e23dad4 | ||
|
|
b22f3d2743 | ||
|
|
23ed4e9849 |
@@ -7,94 +7,52 @@ namespace SabreTools.Hashing.Test
|
||||
{
|
||||
public class CompressedStreamTests
|
||||
{
|
||||
private static readonly string _hashFilePath = Path.Combine(Environment.CurrentDirectory, "TestData", "file-to-hash.zip");
|
||||
/// <summary>
|
||||
/// Path to PKZIP archive containing a single compressed file to hash
|
||||
/// </summary>
|
||||
private static readonly string _singleGzipFilePath
|
||||
= Path.Combine(Environment.CurrentDirectory, "TestData", "file-to-hash.bin.gz");
|
||||
|
||||
#region Known File Information
|
||||
/// <summary>
|
||||
/// Path to PKZIP archive containing a single compressed file to hash
|
||||
/// </summary>
|
||||
private static readonly string _singleZipFilePath
|
||||
= Path.Combine(Environment.CurrentDirectory, "TestData", "file-to-hash.zip");
|
||||
|
||||
private const long _hashFileSize = 125;
|
||||
private const string _adler32 = "08562d95";
|
||||
#if NET7_0_OR_GREATER
|
||||
private const string _blake3 = "d4bd7ca6f1ebea9580d9381106b248eb5b6069170d0bfd00b17d659fcd10dcdc";
|
||||
#endif
|
||||
private const string _crc16_ccitt = "482d";
|
||||
private const string _crc16_ibm = "7573";
|
||||
private const string _crc32 = "ba02a660";
|
||||
private const string _crc64 = "a0e0009c18b5338d";
|
||||
private const string _crc64_reversed = "fb49044e8331f6e5";
|
||||
private const string _fletcher16 = "46c1";
|
||||
private const string _fletcher32 = "073f2d94";
|
||||
private const string _md5 = "b722871eaa950016296184d026c5dec9";
|
||||
#if NETFRAMEWORK
|
||||
private const string _ripemd160 = "346361e1d7fdb836650cecdb842b0dbe660eed66";
|
||||
#endif
|
||||
private const string _sha1 = "eea1ee2d801d830c4bdad4df3c8da6f9f52d1a9f";
|
||||
private const string _sha256 = "fdb02dee8c319c52087382c45f099c90d0b6cc824850aff28c1bfb2884b7b855";
|
||||
private const string _sha384 = "e276c49618fff25bc1fe2e0659cd0ef0e7c1186563b063e07c52323b9899f3ce9b091be04d6208444b3ef1265e879074";
|
||||
private const string _sha512 = "15d69514eb628c2403e945a7cafd1d27e557f6e336c69b63ea17e7ed9d256cc374ee662f09305836d6de37fdae59d83883b982aa8446e4ff26346b6b6b50b240";
|
||||
#if NET8_0_OR_GREATER
|
||||
private const string _sha3_256 = "1d76459e68c865b5911ada5104067cc604c5c60b345c4e81b3905e916a43c868";
|
||||
private const string _sha3_384 = "1bcbed87b73f25c0adf486c3afbf0ea3105763c387af3f8b2bd79b0a1964d42832b1d7c6a2225f9153ead26f442e8b67";
|
||||
private const string _sha3_512 = "89852144df37c58d01f5912124f1942dd00bac0346eb3971943416699c3094cff087fb42c356019c3d91f8e8f55b9254c8caec48e9414af6817297d06725ffeb";
|
||||
private const string _shake128 = "e5f88d0db79a71c39490beb9ebac21eaf4a5d6368438fca20f5e4ce77cfee9aa";
|
||||
private const string _shake256 = "24d9e83198bbc7baf4dcd293bfc35ae3fff05399786c37318f1b1ef85f41970c66926f8a2a1f912d96e2d8e45535af88a301a1c200697437c1a65d7e980344bc";
|
||||
#endif
|
||||
private const string _spamsum = "3:hMCPQCE6AFQxWyENFACBE+rW6Tj7SMQmKozr9MVERkL:hZRdxZENFs+rPSromekL";
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
private const string _xxhash32 = "8e331daa";
|
||||
private const string _xxhash64 = "082bf6f0a49e1e18";
|
||||
private const string _xxhash3 = "040474eb0eda9ff2";
|
||||
private const string _xxhash128 = "d934b4b4a5e1e11baeef8012fbcd11e8";
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
/// <summary>
|
||||
/// Path to PKZIP archive containing a multiple compressed files to hash
|
||||
/// </summary>
|
||||
private static readonly string _multiZipFilePath
|
||||
= Path.Combine(Environment.CurrentDirectory, "TestData", "file-to-hash-multi.zip");
|
||||
|
||||
[Fact]
|
||||
public void GetStreamHashesTest()
|
||||
public void GetSingleGzipStreamHashesTest()
|
||||
{
|
||||
var zipFile = ZipFile.OpenRead(_hashFilePath);
|
||||
var gzipStream = new GZipStream(File.OpenRead(_singleGzipFilePath), CompressionMode.Decompress);
|
||||
var hashDict = HashTool.GetStreamHashes(gzipStream);
|
||||
TestHelper.ValidateHashes(hashDict);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSingleDeflateStreamHashesTest()
|
||||
{
|
||||
var zipFile = ZipFile.OpenRead(_singleZipFilePath);
|
||||
var fileStream = zipFile.Entries[0].Open();
|
||||
var hashDict = HashTool.GetStreamHashes(fileStream);
|
||||
TestHelper.ValidateHashes(hashDict);
|
||||
}
|
||||
|
||||
Assert.NotNull(hashDict);
|
||||
Assert.Equal(_adler32, hashDict![HashType.Adler32]);
|
||||
#if NET7_0_OR_GREATER
|
||||
Assert.Equal(_blake3, hashDict[HashType.BLAKE3]);
|
||||
#endif
|
||||
Assert.Equal(_crc16_ccitt, hashDict[HashType.CRC16_CCITT]);
|
||||
Assert.Equal(_crc16_ibm, hashDict[HashType.CRC16_IBM]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_ISO]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Naive]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Optimized]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Parallel]);
|
||||
Assert.Equal(_crc64, hashDict[HashType.CRC64]);
|
||||
Assert.Equal(_crc64_reversed, hashDict[HashType.CRC64_Reversed]);
|
||||
Assert.Equal(_fletcher16, hashDict[HashType.Fletcher16]);
|
||||
Assert.Equal(_fletcher32, hashDict[HashType.Fletcher32]);
|
||||
Assert.Equal(_md5, hashDict[HashType.MD5]);
|
||||
Assert.Equal(_sha1, hashDict[HashType.SHA1]);
|
||||
Assert.Equal(_sha256, hashDict[HashType.SHA256]);
|
||||
Assert.Equal(_sha384, hashDict[HashType.SHA384]);
|
||||
Assert.Equal(_sha512, hashDict[HashType.SHA512]);
|
||||
#if NET8_0_OR_GREATER
|
||||
if (System.Security.Cryptography.SHA3_256.IsSupported)
|
||||
Assert.Equal(_sha3_256, hashDict[HashType.SHA3_256]);
|
||||
if (System.Security.Cryptography.SHA3_384.IsSupported)
|
||||
Assert.Equal(_sha3_384, hashDict[HashType.SHA3_384]);
|
||||
if (System.Security.Cryptography.SHA3_512.IsSupported)
|
||||
Assert.Equal(_sha3_512, hashDict[HashType.SHA3_512]);
|
||||
if (System.Security.Cryptography.Shake128.IsSupported)
|
||||
Assert.Equal(_shake128, hashDict[HashType.SHAKE128]);
|
||||
if (System.Security.Cryptography.Shake256.IsSupported)
|
||||
Assert.Equal(_shake256, hashDict[HashType.SHAKE256]);
|
||||
#endif
|
||||
Assert.Equal(_spamsum, hashDict[HashType.SpamSum]);
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
Assert.Equal(_xxhash32, hashDict[HashType.XxHash32]);
|
||||
Assert.Equal(_xxhash64, hashDict[HashType.XxHash64]);
|
||||
Assert.Equal(_xxhash3, hashDict[HashType.XxHash3]);
|
||||
Assert.Equal(_xxhash128, hashDict[HashType.XxHash128]);
|
||||
#endif
|
||||
[Fact]
|
||||
public void GetMultiDeflateStreamHashesTest()
|
||||
{
|
||||
var zipFile = ZipFile.OpenRead(_multiZipFilePath);
|
||||
|
||||
for (int i = 0; i < zipFile.Entries.Count; i++)
|
||||
{
|
||||
var fileStream = zipFile.Entries[i].Open();
|
||||
var hashDict = HashTool.GetStreamHashes(fileStream);
|
||||
TestHelper.ValidateHashes(hashDict);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,44 +6,18 @@ namespace SabreTools.Hashing.Test
|
||||
{
|
||||
public class HashToolTests
|
||||
{
|
||||
private static readonly string _hashFilePath = Path.Combine(Environment.CurrentDirectory, "TestData", "file-to-hash.bin");
|
||||
/// <summary>
|
||||
/// Path to the uncompressed file to hash
|
||||
/// </summary>
|
||||
private static readonly string _hashFilePath
|
||||
= Path.Combine(Environment.CurrentDirectory, "TestData", "file-to-hash.bin");
|
||||
|
||||
#region Known File Information
|
||||
|
||||
private const long _hashFileSize = 125;
|
||||
private const string _adler32 = "08562d95";
|
||||
#if NET7_0_OR_GREATER
|
||||
private const string _blake3 = "d4bd7ca6f1ebea9580d9381106b248eb5b6069170d0bfd00b17d659fcd10dcdc";
|
||||
#endif
|
||||
private const string _crc16_ccitt = "482d";
|
||||
private const string _crc16_ibm = "7573";
|
||||
private const string _crc32 = "ba02a660";
|
||||
private const string _crc64 = "a0e0009c18b5338d";
|
||||
private const string _crc64_reversed = "fb49044e8331f6e5";
|
||||
private const string _fletcher16 = "46c1";
|
||||
private const string _fletcher32 = "073f2d94";
|
||||
private const string _md5 = "b722871eaa950016296184d026c5dec9";
|
||||
#if NETFRAMEWORK
|
||||
private const string _ripemd160 = "346361e1d7fdb836650cecdb842b0dbe660eed66";
|
||||
#endif
|
||||
private const string _sha1 = "eea1ee2d801d830c4bdad4df3c8da6f9f52d1a9f";
|
||||
private const string _sha256 = "fdb02dee8c319c52087382c45f099c90d0b6cc824850aff28c1bfb2884b7b855";
|
||||
private const string _sha384 = "e276c49618fff25bc1fe2e0659cd0ef0e7c1186563b063e07c52323b9899f3ce9b091be04d6208444b3ef1265e879074";
|
||||
private const string _sha512 = "15d69514eb628c2403e945a7cafd1d27e557f6e336c69b63ea17e7ed9d256cc374ee662f09305836d6de37fdae59d83883b982aa8446e4ff26346b6b6b50b240";
|
||||
#if NET8_0_OR_GREATER
|
||||
private const string _sha3_256 = "1d76459e68c865b5911ada5104067cc604c5c60b345c4e81b3905e916a43c868";
|
||||
private const string _sha3_384 = "1bcbed87b73f25c0adf486c3afbf0ea3105763c387af3f8b2bd79b0a1964d42832b1d7c6a2225f9153ead26f442e8b67";
|
||||
private const string _sha3_512 = "89852144df37c58d01f5912124f1942dd00bac0346eb3971943416699c3094cff087fb42c356019c3d91f8e8f55b9254c8caec48e9414af6817297d06725ffeb";
|
||||
private const string _shake128 = "e5f88d0db79a71c39490beb9ebac21eaf4a5d6368438fca20f5e4ce77cfee9aa";
|
||||
private const string _shake256 = "24d9e83198bbc7baf4dcd293bfc35ae3fff05399786c37318f1b1ef85f41970c66926f8a2a1f912d96e2d8e45535af88a301a1c200697437c1a65d7e980344bc";
|
||||
#endif
|
||||
private const string _spamsum = "3:hMCPQCE6AFQxWyENFACBE+rW6Tj7SMQmKozr9MVERkL:hZRdxZENFs+rPSromekL";
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
private const string _xxhash32 = "8e331daa";
|
||||
private const string _xxhash64 = "082bf6f0a49e1e18";
|
||||
private const string _xxhash3 = "040474eb0eda9ff2";
|
||||
private const string _xxhash128 = "d934b4b4a5e1e11baeef8012fbcd11e8";
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -63,98 +37,15 @@ namespace SabreTools.Hashing.Test
|
||||
public void GetFileHashesTest()
|
||||
{
|
||||
var hashDict = HashTool.GetFileHashes(_hashFilePath);
|
||||
|
||||
Assert.NotNull(hashDict);
|
||||
Assert.Equal(_adler32, hashDict![HashType.Adler32]);
|
||||
#if NET7_0_OR_GREATER
|
||||
Assert.Equal(_blake3, hashDict[HashType.BLAKE3]);
|
||||
#endif
|
||||
Assert.Equal(_crc16_ccitt, hashDict[HashType.CRC16_CCITT]);
|
||||
Assert.Equal(_crc16_ibm, hashDict[HashType.CRC16_IBM]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_ISO]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Naive]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Optimized]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Parallel]);
|
||||
Assert.Equal(_crc64, hashDict[HashType.CRC64]);
|
||||
Assert.Equal(_crc64_reversed, hashDict[HashType.CRC64_Reversed]);
|
||||
Assert.Equal(_fletcher16, hashDict[HashType.Fletcher16]);
|
||||
Assert.Equal(_fletcher32, hashDict[HashType.Fletcher32]);
|
||||
Assert.Equal(_md5, hashDict[HashType.MD5]);
|
||||
#if NETFRAMEWORK
|
||||
Assert.Equal(_ripemd160, hashDict[HashType.RIPEMD160]);
|
||||
#endif
|
||||
Assert.Equal(_sha1, hashDict[HashType.SHA1]);
|
||||
Assert.Equal(_sha256, hashDict[HashType.SHA256]);
|
||||
Assert.Equal(_sha384, hashDict[HashType.SHA384]);
|
||||
Assert.Equal(_sha512, hashDict[HashType.SHA512]);
|
||||
#if NET8_0_OR_GREATER
|
||||
if (System.Security.Cryptography.SHA3_256.IsSupported)
|
||||
Assert.Equal(_sha3_256, hashDict[HashType.SHA3_256]);
|
||||
if (System.Security.Cryptography.SHA3_384.IsSupported)
|
||||
Assert.Equal(_sha3_384, hashDict[HashType.SHA3_384]);
|
||||
if (System.Security.Cryptography.SHA3_512.IsSupported)
|
||||
Assert.Equal(_sha3_512, hashDict[HashType.SHA3_512]);
|
||||
if (System.Security.Cryptography.Shake128.IsSupported)
|
||||
Assert.Equal(_shake128, hashDict[HashType.SHAKE128]);
|
||||
if (System.Security.Cryptography.Shake256.IsSupported)
|
||||
Assert.Equal(_shake256, hashDict[HashType.SHAKE256]);
|
||||
#endif
|
||||
Assert.Equal(_spamsum, hashDict[HashType.SpamSum]);
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
Assert.Equal(_xxhash32, hashDict[HashType.XxHash32]);
|
||||
Assert.Equal(_xxhash64, hashDict[HashType.XxHash64]);
|
||||
Assert.Equal(_xxhash3, hashDict[HashType.XxHash3]);
|
||||
Assert.Equal(_xxhash128, hashDict[HashType.XxHash128]);
|
||||
#endif
|
||||
TestHelper.ValidateHashes(hashDict);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetFileHashesAndSizeTest()
|
||||
{
|
||||
var hashDict = HashTool.GetFileHashesAndSize(_hashFilePath, out long actualSize);
|
||||
|
||||
Assert.Equal(_hashFileSize, actualSize);
|
||||
Assert.NotNull(hashDict);
|
||||
Assert.Equal(_adler32, hashDict![HashType.Adler32]);
|
||||
#if NET7_0_OR_GREATER
|
||||
Assert.Equal(_blake3, hashDict[HashType.BLAKE3]);
|
||||
#endif
|
||||
Assert.Equal(_crc16_ccitt, hashDict[HashType.CRC16_CCITT]);
|
||||
Assert.Equal(_crc16_ibm, hashDict[HashType.CRC16_IBM]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_ISO]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Naive]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Optimized]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Parallel]);
|
||||
Assert.Equal(_crc64, hashDict[HashType.CRC64]);
|
||||
Assert.Equal(_crc64_reversed, hashDict[HashType.CRC64_Reversed]);
|
||||
Assert.Equal(_fletcher16, hashDict[HashType.Fletcher16]);
|
||||
Assert.Equal(_fletcher32, hashDict[HashType.Fletcher32]);
|
||||
Assert.Equal(_md5, hashDict[HashType.MD5]);
|
||||
Assert.Equal(_sha1, hashDict[HashType.SHA1]);
|
||||
Assert.Equal(_sha256, hashDict[HashType.SHA256]);
|
||||
Assert.Equal(_sha384, hashDict[HashType.SHA384]);
|
||||
Assert.Equal(_sha512, hashDict[HashType.SHA512]);
|
||||
#if NET8_0_OR_GREATER
|
||||
if (System.Security.Cryptography.SHA3_256.IsSupported)
|
||||
Assert.Equal(_sha3_256, hashDict[HashType.SHA3_256]);
|
||||
if (System.Security.Cryptography.SHA3_384.IsSupported)
|
||||
Assert.Equal(_sha3_384, hashDict[HashType.SHA3_384]);
|
||||
if (System.Security.Cryptography.SHA3_512.IsSupported)
|
||||
Assert.Equal(_sha3_512, hashDict[HashType.SHA3_512]);
|
||||
if (System.Security.Cryptography.Shake128.IsSupported)
|
||||
Assert.Equal(_shake128, hashDict[HashType.SHAKE128]);
|
||||
if (System.Security.Cryptography.Shake256.IsSupported)
|
||||
Assert.Equal(_shake256, hashDict[HashType.SHAKE256]);
|
||||
#endif
|
||||
Assert.Equal(_spamsum, hashDict[HashType.SpamSum]);
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
Assert.Equal(_xxhash32, hashDict[HashType.XxHash32]);
|
||||
Assert.Equal(_xxhash64, hashDict[HashType.XxHash64]);
|
||||
Assert.Equal(_xxhash3, hashDict[HashType.XxHash3]);
|
||||
Assert.Equal(_xxhash128, hashDict[HashType.XxHash128]);
|
||||
#endif
|
||||
TestHelper.ValidateHashes(hashDict);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -162,47 +53,7 @@ namespace SabreTools.Hashing.Test
|
||||
{
|
||||
byte[] fileBytes = File.ReadAllBytes(_hashFilePath);
|
||||
var hashDict = HashTool.GetByteArrayHashes(fileBytes);
|
||||
|
||||
Assert.NotNull(hashDict);
|
||||
Assert.Equal(_adler32, hashDict![HashType.Adler32]);
|
||||
#if NET7_0_OR_GREATER
|
||||
Assert.Equal(_blake3, hashDict[HashType.BLAKE3]);
|
||||
#endif
|
||||
Assert.Equal(_crc16_ccitt, hashDict[HashType.CRC16_CCITT]);
|
||||
Assert.Equal(_crc16_ibm, hashDict[HashType.CRC16_IBM]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_ISO]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Naive]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Optimized]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Parallel]);
|
||||
Assert.Equal(_crc64, hashDict[HashType.CRC64]);
|
||||
Assert.Equal(_crc64_reversed, hashDict[HashType.CRC64_Reversed]);
|
||||
Assert.Equal(_fletcher16, hashDict[HashType.Fletcher16]);
|
||||
Assert.Equal(_fletcher32, hashDict[HashType.Fletcher32]);
|
||||
Assert.Equal(_md5, hashDict[HashType.MD5]);
|
||||
Assert.Equal(_sha1, hashDict[HashType.SHA1]);
|
||||
Assert.Equal(_sha256, hashDict[HashType.SHA256]);
|
||||
Assert.Equal(_sha384, hashDict[HashType.SHA384]);
|
||||
Assert.Equal(_sha512, hashDict[HashType.SHA512]);
|
||||
#if NET8_0_OR_GREATER
|
||||
if (System.Security.Cryptography.SHA3_256.IsSupported)
|
||||
Assert.Equal(_sha3_256, hashDict[HashType.SHA3_256]);
|
||||
if (System.Security.Cryptography.SHA3_384.IsSupported)
|
||||
Assert.Equal(_sha3_384, hashDict[HashType.SHA3_384]);
|
||||
if (System.Security.Cryptography.SHA3_512.IsSupported)
|
||||
Assert.Equal(_sha3_512, hashDict[HashType.SHA3_512]);
|
||||
if (System.Security.Cryptography.Shake128.IsSupported)
|
||||
Assert.Equal(_shake128, hashDict[HashType.SHAKE128]);
|
||||
if (System.Security.Cryptography.Shake256.IsSupported)
|
||||
Assert.Equal(_shake256, hashDict[HashType.SHAKE256]);
|
||||
#endif
|
||||
Assert.Equal(_spamsum, hashDict[HashType.SpamSum]);
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
Assert.Equal(_xxhash32, hashDict[HashType.XxHash32]);
|
||||
Assert.Equal(_xxhash64, hashDict[HashType.XxHash64]);
|
||||
Assert.Equal(_xxhash3, hashDict[HashType.XxHash3]);
|
||||
Assert.Equal(_xxhash128, hashDict[HashType.XxHash128]);
|
||||
#endif
|
||||
TestHelper.ValidateHashes(hashDict);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -210,47 +61,7 @@ namespace SabreTools.Hashing.Test
|
||||
{
|
||||
var fileStream = File.OpenRead(_hashFilePath);
|
||||
var hashDict = HashTool.GetStreamHashes(fileStream);
|
||||
|
||||
Assert.NotNull(hashDict);
|
||||
Assert.Equal(_adler32, hashDict![HashType.Adler32]);
|
||||
#if NET7_0_OR_GREATER
|
||||
Assert.Equal(_blake3, hashDict[HashType.BLAKE3]);
|
||||
#endif
|
||||
Assert.Equal(_crc16_ccitt, hashDict[HashType.CRC16_CCITT]);
|
||||
Assert.Equal(_crc16_ibm, hashDict[HashType.CRC16_IBM]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_ISO]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Naive]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Optimized]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Parallel]);
|
||||
Assert.Equal(_crc64, hashDict[HashType.CRC64]);
|
||||
Assert.Equal(_crc64_reversed, hashDict[HashType.CRC64_Reversed]);
|
||||
Assert.Equal(_fletcher16, hashDict[HashType.Fletcher16]);
|
||||
Assert.Equal(_fletcher32, hashDict[HashType.Fletcher32]);
|
||||
Assert.Equal(_md5, hashDict[HashType.MD5]);
|
||||
Assert.Equal(_sha1, hashDict[HashType.SHA1]);
|
||||
Assert.Equal(_sha256, hashDict[HashType.SHA256]);
|
||||
Assert.Equal(_sha384, hashDict[HashType.SHA384]);
|
||||
Assert.Equal(_sha512, hashDict[HashType.SHA512]);
|
||||
#if NET8_0_OR_GREATER
|
||||
if (System.Security.Cryptography.SHA3_256.IsSupported)
|
||||
Assert.Equal(_sha3_256, hashDict[HashType.SHA3_256]);
|
||||
if (System.Security.Cryptography.SHA3_384.IsSupported)
|
||||
Assert.Equal(_sha3_384, hashDict[HashType.SHA3_384]);
|
||||
if (System.Security.Cryptography.SHA3_512.IsSupported)
|
||||
Assert.Equal(_sha3_512, hashDict[HashType.SHA3_512]);
|
||||
if (System.Security.Cryptography.Shake128.IsSupported)
|
||||
Assert.Equal(_shake128, hashDict[HashType.SHAKE128]);
|
||||
if (System.Security.Cryptography.Shake256.IsSupported)
|
||||
Assert.Equal(_shake256, hashDict[HashType.SHAKE256]);
|
||||
#endif
|
||||
Assert.Equal(_spamsum, hashDict[HashType.SpamSum]);
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
Assert.Equal(_xxhash32, hashDict[HashType.XxHash32]);
|
||||
Assert.Equal(_xxhash64, hashDict[HashType.XxHash64]);
|
||||
Assert.Equal(_xxhash3, hashDict[HashType.XxHash3]);
|
||||
Assert.Equal(_xxhash128, hashDict[HashType.XxHash128]);
|
||||
#endif
|
||||
TestHelper.ValidateHashes(hashDict);
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
SabreTools.Hashing.Test/TestData/file-to-hash-multi.zip
Normal file
BIN
SabreTools.Hashing.Test/TestData/file-to-hash-multi.zip
Normal file
Binary file not shown.
BIN
SabreTools.Hashing.Test/TestData/file-to-hash.bin.gz
Normal file
BIN
SabreTools.Hashing.Test/TestData/file-to-hash.bin.gz
Normal file
Binary file not shown.
97
SabreTools.Hashing.Test/TestHelper.cs
Normal file
97
SabreTools.Hashing.Test/TestHelper.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace SabreTools.Hashing.Test
|
||||
{
|
||||
/// <summary>
|
||||
/// Helper class for tests
|
||||
/// </summary>
|
||||
internal static class TestHelper
|
||||
{
|
||||
#region Known File Information
|
||||
|
||||
private const long _hashFileSize = 125;
|
||||
private const string _adler32 = "08562d95";
|
||||
#if NET7_0_OR_GREATER
|
||||
private const string _blake3 = "d4bd7ca6f1ebea9580d9381106b248eb5b6069170d0bfd00b17d659fcd10dcdc";
|
||||
#endif
|
||||
private const string _crc16_ccitt = "482d";
|
||||
private const string _crc16_ibm = "7573";
|
||||
private const string _crc32 = "ba02a660";
|
||||
private const string _crc64 = "a0e0009c18b5338d";
|
||||
private const string _crc64_reversed = "fb49044e8331f6e5";
|
||||
private const string _fletcher16 = "46c1";
|
||||
private const string _fletcher32 = "073f2d94";
|
||||
private const string _md5 = "b722871eaa950016296184d026c5dec9";
|
||||
#if NETFRAMEWORK
|
||||
private const string _ripemd160 = "346361e1d7fdb836650cecdb842b0dbe660eed66";
|
||||
#endif
|
||||
private const string _sha1 = "eea1ee2d801d830c4bdad4df3c8da6f9f52d1a9f";
|
||||
private const string _sha256 = "fdb02dee8c319c52087382c45f099c90d0b6cc824850aff28c1bfb2884b7b855";
|
||||
private const string _sha384 = "e276c49618fff25bc1fe2e0659cd0ef0e7c1186563b063e07c52323b9899f3ce9b091be04d6208444b3ef1265e879074";
|
||||
private const string _sha512 = "15d69514eb628c2403e945a7cafd1d27e557f6e336c69b63ea17e7ed9d256cc374ee662f09305836d6de37fdae59d83883b982aa8446e4ff26346b6b6b50b240";
|
||||
#if NET8_0_OR_GREATER
|
||||
private const string _sha3_256 = "1d76459e68c865b5911ada5104067cc604c5c60b345c4e81b3905e916a43c868";
|
||||
private const string _sha3_384 = "1bcbed87b73f25c0adf486c3afbf0ea3105763c387af3f8b2bd79b0a1964d42832b1d7c6a2225f9153ead26f442e8b67";
|
||||
private const string _sha3_512 = "89852144df37c58d01f5912124f1942dd00bac0346eb3971943416699c3094cff087fb42c356019c3d91f8e8f55b9254c8caec48e9414af6817297d06725ffeb";
|
||||
private const string _shake128 = "e5f88d0db79a71c39490beb9ebac21eaf4a5d6368438fca20f5e4ce77cfee9aa";
|
||||
private const string _shake256 = "24d9e83198bbc7baf4dcd293bfc35ae3fff05399786c37318f1b1ef85f41970c66926f8a2a1f912d96e2d8e45535af88a301a1c200697437c1a65d7e980344bc";
|
||||
#endif
|
||||
private const string _spamsum = "3:hMCPQCE6AFQxWyENFACBE+rW6Tj7SMQmKozr9MVERkL:hZRdxZENFs+rPSromekL";
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
private const string _xxhash32 = "8e331daa";
|
||||
private const string _xxhash64 = "082bf6f0a49e1e18";
|
||||
private const string _xxhash3 = "040474eb0eda9ff2";
|
||||
private const string _xxhash128 = "d934b4b4a5e1e11baeef8012fbcd11e8";
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Validate the hashes in a hash dictionary
|
||||
/// </summary>
|
||||
public static void ValidateHashes(Dictionary<HashType, string?>? hashDict)
|
||||
{
|
||||
Assert.NotNull(hashDict);
|
||||
Assert.Equal(_adler32, hashDict![HashType.Adler32]);
|
||||
#if NET7_0_OR_GREATER
|
||||
Assert.Equal(_blake3, hashDict[HashType.BLAKE3]);
|
||||
#endif
|
||||
Assert.Equal(_crc16_ccitt, hashDict[HashType.CRC16_CCITT]);
|
||||
Assert.Equal(_crc16_ibm, hashDict[HashType.CRC16_IBM]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_ISO]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Naive]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Optimized]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32_Parallel]);
|
||||
Assert.Equal(_crc64, hashDict[HashType.CRC64]);
|
||||
Assert.Equal(_crc64_reversed, hashDict[HashType.CRC64_Reversed]);
|
||||
Assert.Equal(_fletcher16, hashDict[HashType.Fletcher16]);
|
||||
Assert.Equal(_fletcher32, hashDict[HashType.Fletcher32]);
|
||||
Assert.Equal(_md5, hashDict[HashType.MD5]);
|
||||
Assert.Equal(_sha1, hashDict[HashType.SHA1]);
|
||||
Assert.Equal(_sha256, hashDict[HashType.SHA256]);
|
||||
Assert.Equal(_sha384, hashDict[HashType.SHA384]);
|
||||
Assert.Equal(_sha512, hashDict[HashType.SHA512]);
|
||||
#if NET8_0_OR_GREATER
|
||||
if (System.Security.Cryptography.SHA3_256.IsSupported)
|
||||
Assert.Equal(_sha3_256, hashDict[HashType.SHA3_256]);
|
||||
if (System.Security.Cryptography.SHA3_384.IsSupported)
|
||||
Assert.Equal(_sha3_384, hashDict[HashType.SHA3_384]);
|
||||
if (System.Security.Cryptography.SHA3_512.IsSupported)
|
||||
Assert.Equal(_sha3_512, hashDict[HashType.SHA3_512]);
|
||||
if (System.Security.Cryptography.Shake128.IsSupported)
|
||||
Assert.Equal(_shake128, hashDict[HashType.SHAKE128]);
|
||||
if (System.Security.Cryptography.Shake256.IsSupported)
|
||||
Assert.Equal(_shake256, hashDict[HashType.SHAKE256]);
|
||||
#endif
|
||||
Assert.Equal(_spamsum, hashDict[HashType.SpamSum]);
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
Assert.Equal(_xxhash32, hashDict[HashType.XxHash32]);
|
||||
Assert.Equal(_xxhash64, hashDict[HashType.XxHash64]);
|
||||
Assert.Equal(_xxhash3, hashDict[HashType.XxHash3]);
|
||||
Assert.Equal(_xxhash128, hashDict[HashType.XxHash128]);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace Compress.ThreadReaders
|
||||
{
|
||||
internal sealed class ThreadLoadBuffer : IDisposable
|
||||
{
|
||||
private readonly AutoResetEvent _waitEvent;
|
||||
private readonly AutoResetEvent _outEvent;
|
||||
private readonly Thread _tWorker;
|
||||
|
||||
private byte[]? _buffer;
|
||||
private int _size;
|
||||
private readonly Stream _ds;
|
||||
private bool _finished;
|
||||
public bool errorState;
|
||||
|
||||
public int SizeRead;
|
||||
|
||||
public ThreadLoadBuffer(Stream ds)
|
||||
{
|
||||
_waitEvent = new AutoResetEvent(false);
|
||||
_outEvent = new AutoResetEvent(false);
|
||||
_finished = false;
|
||||
_ds = ds;
|
||||
errorState = false;
|
||||
|
||||
_tWorker = new Thread(MainLoop);
|
||||
_tWorker.Start();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_waitEvent.Close();
|
||||
_outEvent.Close();
|
||||
}
|
||||
|
||||
private void MainLoop()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
_waitEvent.WaitOne();
|
||||
if (_finished)
|
||||
{
|
||||
break;
|
||||
}
|
||||
try
|
||||
{
|
||||
if (_buffer != null)
|
||||
SizeRead = _ds.Read(_buffer, 0, _size);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
errorState = true;
|
||||
}
|
||||
_outEvent.Set();
|
||||
}
|
||||
}
|
||||
|
||||
public void Trigger(byte[] buffer, int size)
|
||||
{
|
||||
_buffer = buffer;
|
||||
_size = size;
|
||||
_waitEvent.Set();
|
||||
}
|
||||
|
||||
public void Wait()
|
||||
{
|
||||
_outEvent.WaitOne();
|
||||
}
|
||||
|
||||
public void Finish()
|
||||
{
|
||||
_finished = true;
|
||||
_waitEvent.Set();
|
||||
_tWorker.Join();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ using System.IO;
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
using Compress.ThreadReaders;
|
||||
|
||||
namespace SabreTools.Hashing
|
||||
{
|
||||
@@ -374,8 +373,6 @@ namespace SabreTools.Hashing
|
||||
// Create the output dictionary
|
||||
var hashDict = new Dictionary<HashType, byte[]?>();
|
||||
|
||||
|
||||
|
||||
// Run the hashing
|
||||
var hashers = GetStreamHashesInternal(input, hashTypes, leaveOpen);
|
||||
if (hashers == null)
|
||||
@@ -419,29 +416,18 @@ namespace SabreTools.Hashing
|
||||
hashers[hashType] = new HashWrapper(hashType);
|
||||
}
|
||||
|
||||
// Initialize the hashing helpers
|
||||
var loadBuffer = new ThreadLoadBuffer(input);
|
||||
// Create the buffer for holding data
|
||||
int buffersize = 3 * 1024 * 1024;
|
||||
byte[] buffer0 = new byte[buffersize];
|
||||
byte[] buffer1 = new byte[buffersize];
|
||||
byte[] buffer = new byte[buffersize];
|
||||
int lastRead;
|
||||
|
||||
/*
|
||||
Please note that some of the following code is adapted from
|
||||
RomVault. This is a modified version of how RomVault does
|
||||
threaded hashing. As such, some of the terminology and code
|
||||
is the same, though variable names and comments may have
|
||||
been tweaked to better fit this code base.
|
||||
*/
|
||||
|
||||
// Pre load the first buffer
|
||||
int lastRead = input.Read(buffer0, 0, buffersize);
|
||||
bool bufferSelect = true;
|
||||
|
||||
while (lastRead > 0)
|
||||
// Hash the input data in blocks
|
||||
do
|
||||
{
|
||||
// Trigger the buffer load on the second buffer
|
||||
loadBuffer.Trigger(bufferSelect ? buffer1 : buffer0, buffersize);
|
||||
byte[] buffer = bufferSelect ? buffer0 : buffer1;
|
||||
// Load the buffer and hold the number of bytes read
|
||||
lastRead = input.Read(buffer, 0, buffersize);
|
||||
if (lastRead == 0)
|
||||
break;
|
||||
|
||||
#if NET20 || NET35
|
||||
// Run hashers sequentially on each chunk
|
||||
@@ -453,28 +439,20 @@ namespace SabreTools.Hashing
|
||||
// Run hashers in parallel on each chunk
|
||||
Parallel.ForEach(hashers, h => h.Value.Process(buffer, 0, lastRead));
|
||||
#endif
|
||||
|
||||
// Wait for the load buffer worker, if needed
|
||||
if (loadBuffer.SizeRead > 0)
|
||||
loadBuffer.Wait();
|
||||
|
||||
// Setup for the next hashing step
|
||||
lastRead = loadBuffer.SizeRead;
|
||||
bufferSelect = !bufferSelect;
|
||||
}
|
||||
while (lastRead > 0);
|
||||
|
||||
// Finalize all hashing helpers
|
||||
loadBuffer.Finish();
|
||||
#if NET20 || NET35
|
||||
// Finalize all hashing helpers sequentially
|
||||
foreach (var h in hashers)
|
||||
{
|
||||
h.Value.Terminate();
|
||||
}
|
||||
#else
|
||||
// Finalize all hashing helpers in parallel
|
||||
Parallel.ForEach(hashers, h => h.Value.Terminate());
|
||||
#endif
|
||||
|
||||
loadBuffer.Dispose();
|
||||
return hashers;
|
||||
}
|
||||
catch (IOException)
|
||||
|
||||
@@ -23,11 +23,7 @@ namespace SabreTools.Hashing
|
||||
/// <summary>
|
||||
/// Hash type associated with the current state
|
||||
/// </summary>
|
||||
#if NETFRAMEWORK || NETCOREAPP3_1
|
||||
public HashType HashType { get; private set; }
|
||||
#else
|
||||
public HashType HashType { get; init; }
|
||||
#endif
|
||||
public readonly HashType HashType;
|
||||
|
||||
/// <summary>
|
||||
/// Current hash in bytes
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<Version>1.2.1</Version>
|
||||
<Version>1.2.2</Version>
|
||||
<WarningsNotAsErrors>NU5104</WarningsNotAsErrors>
|
||||
|
||||
<!-- Package Properties -->
|
||||
|
||||
36
publish-nix.sh
Normal file
36
publish-nix.sh
Normal file
@@ -0,0 +1,36 @@
|
||||
#! /bin/bash
|
||||
|
||||
# This batch file assumes the following:
|
||||
# - .NET 8.0 (or newer) SDK is installed and in PATH
|
||||
#
|
||||
# If any of these are not satisfied, the operation may fail
|
||||
# in an unpredictable way and result in an incomplete output.
|
||||
|
||||
# Optional parameters
|
||||
NO_BUILD=false
|
||||
while getopts "uba" OPTION
|
||||
do
|
||||
case $OPTION in
|
||||
b)
|
||||
NO_BUILD=true
|
||||
;;
|
||||
*)
|
||||
echo "Invalid option provided"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Set the current directory as a variable
|
||||
BUILD_FOLDER=$PWD
|
||||
|
||||
# Only build if requested
|
||||
if [ $NO_BUILD = false ]
|
||||
then
|
||||
# Restore Nuget packages for all builds
|
||||
echo "Restoring Nuget packages"
|
||||
dotnet restore
|
||||
|
||||
# Create Nuget Package
|
||||
dotnet pack SabreTools.Hashing/SabreTools.Hashing.csproj --output $BUILD_FOLDER
|
||||
fi
|
||||
26
publish-win.ps1
Normal file
26
publish-win.ps1
Normal file
@@ -0,0 +1,26 @@
|
||||
# This batch file assumes the following:
|
||||
# - .NET 8.0 (or newer) SDK is installed and in PATH
|
||||
#
|
||||
# If any of these are not satisfied, the operation may fail
|
||||
# in an unpredictable way and result in an incomplete output.
|
||||
|
||||
# Optional parameters
|
||||
param(
|
||||
[Parameter(Mandatory = $false)]
|
||||
[Alias("NoBuild")]
|
||||
[switch]$NO_BUILD
|
||||
)
|
||||
|
||||
# Set the current directory as a variable
|
||||
$BUILD_FOLDER = $PSScriptRoot
|
||||
|
||||
# Only build if requested
|
||||
if (!$NO_BUILD.IsPresent)
|
||||
{
|
||||
# Restore Nuget packages for all builds
|
||||
Write-Host "Restoring Nuget packages"
|
||||
dotnet restore
|
||||
|
||||
# Create Nuget Package
|
||||
dotnet pack SabreTools.Hashing\SabreTools.Hashing.csproj --output $BUILD_FOLDER
|
||||
}
|
||||
Reference in New Issue
Block a user