mirror of
https://github.com/SabreTools/SabreTools.Hashing.git
synced 2026-02-08 13:49:54 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45adf3a8cd | ||
|
|
b5fc196018 | ||
|
|
fac3a5ab43 | ||
|
|
8face3e4ed | ||
|
|
047463f726 | ||
|
|
7ab41ce096 |
@@ -44,18 +44,22 @@ namespace SabreTools.Hashing.Test
|
||||
var hashDict = HashTool.GetFileHashes(_hashFilePath);
|
||||
|
||||
Assert.NotNull(hashDict);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict![HashType.CRC32]);
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
Assert.Equal(_crc64, hashDict[HashType.CRC64]);
|
||||
#endif
|
||||
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]);
|
||||
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]
|
||||
@@ -65,21 +69,24 @@ namespace SabreTools.Hashing.Test
|
||||
|
||||
Assert.Equal(_hashFileSize, actualSize);
|
||||
Assert.NotNull(hashDict);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict![HashType.CRC32]);
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
Assert.Equal(_crc64, hashDict[HashType.CRC64]);
|
||||
#endif
|
||||
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]);
|
||||
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 GetByteArrayHashesTest()
|
||||
{
|
||||
@@ -87,18 +94,22 @@ namespace SabreTools.Hashing.Test
|
||||
var hashDict = HashTool.GetByteArrayHashes(fileBytes);
|
||||
|
||||
Assert.NotNull(hashDict);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict![HashType.CRC32]);
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
Assert.Equal(_crc64, hashDict[HashType.CRC64]);
|
||||
#endif
|
||||
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]);
|
||||
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]
|
||||
@@ -108,18 +119,22 @@ namespace SabreTools.Hashing.Test
|
||||
var hashDict = HashTool.GetStreamHashes(fileStream);
|
||||
|
||||
Assert.NotNull(hashDict);
|
||||
Assert.Equal(_crc32, hashDict[HashType.CRC32]);
|
||||
Assert.Equal(_crc32, hashDict![HashType.CRC32]);
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
Assert.Equal(_crc64, hashDict[HashType.CRC64]);
|
||||
#endif
|
||||
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]);
|
||||
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,9 +1,12 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
|
||||
<TargetFrameworks>net452;net462;net6.0;net7.0;net8.0</TargetFrameworks>
|
||||
<CheckEolTargetFramework>false</CheckEolTargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<Nullable>enable</Nullable>
|
||||
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -16,12 +19,15 @@
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<!-- Support for old .NET versions -->
|
||||
<ItemGroup Condition="$(TargetFramework.StartsWith(`net452`))">
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition="$(TargetFramework.StartsWith(`net462`)) OR $(TargetFramework.StartsWith(`net6`)) OR $(TargetFramework.StartsWith(`net7`)) OR $(TargetFramework.StartsWith(`net8`))">
|
||||
<PackageReference Include="xunit" Version="2.6.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.4">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
@@ -29,6 +35,14 @@
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SabreTools.Hashing\SabreTools.Hashing.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
76
SabreTools.Hashing/Constants.cs
Normal file
76
SabreTools.Hashing/Constants.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
namespace SabreTools.Hashing
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
#region 0-byte file constants
|
||||
|
||||
public const long SizeZero = 0;
|
||||
public const string CRCZero = "00000000";
|
||||
public static readonly byte[] CRCZeroBytes = [0x00, 0x00, 0x00, 0x00];
|
||||
public const string MD5Zero = "d41d8cd98f00b204e9800998ecf8427e";
|
||||
public static readonly byte[] MD5ZeroBytes = [ 0xd4, 0x1d, 0x8c, 0xd9,
|
||||
0x8f, 0x00, 0xb2, 0x04,
|
||||
0xe9, 0x80, 0x09, 0x98,
|
||||
0xec, 0xf8, 0x42, 0x7e ];
|
||||
public const string SHA1Zero = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
|
||||
public static readonly byte[] SHA1ZeroBytes = [ 0xda, 0x39, 0xa3, 0xee,
|
||||
0x5e, 0x6b, 0x4b, 0x0d,
|
||||
0x32, 0x55, 0xbf, 0xef,
|
||||
0x95, 0x60, 0x18, 0x90,
|
||||
0xaf, 0xd8, 0x07, 0x09 ];
|
||||
public const string SHA256Zero = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
|
||||
public static readonly byte[] SHA256ZeroBytes = [ 0xba, 0x78, 0x16, 0xbf,
|
||||
0x8f, 0x01, 0xcf, 0xea,
|
||||
0x41, 0x41, 0x40, 0xde,
|
||||
0x5d, 0xae, 0x22, 0x23,
|
||||
0xb0, 0x03, 0x61, 0xa3,
|
||||
0x96, 0x17, 0x7a, 0x9c,
|
||||
0xb4, 0x10, 0xff, 0x61,
|
||||
0xf2, 0x00, 0x15, 0xad ];
|
||||
public const string SHA384Zero = "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7";
|
||||
public static readonly byte[] SHA384ZeroBytes = [ 0xcb, 0x00, 0x75, 0x3f,
|
||||
0x45, 0xa3, 0x5e, 0x8b,
|
||||
0xb5, 0xa0, 0x3d, 0x69,
|
||||
0x9a, 0xc6, 0x50, 0x07,
|
||||
0x27, 0x2c, 0x32, 0xab,
|
||||
0x0e, 0xde, 0xd1, 0x63,
|
||||
0x1a, 0x8b, 0x60, 0x5a,
|
||||
0x43, 0xff, 0x5b, 0xed,
|
||||
0x80, 0x86, 0x07, 0x2b,
|
||||
0xa1, 0xe7, 0xcc, 0x23,
|
||||
0x58, 0xba, 0xec, 0xa1,
|
||||
0x34, 0xc8, 0x25, 0xa7 ];
|
||||
public const string SHA512Zero = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
|
||||
public static readonly byte[] SHA512ZeroBytes = [ 0xdd, 0xaf, 0x35, 0xa1,
|
||||
0x93, 0x61, 0x7a, 0xba,
|
||||
0xcc, 0x41, 0x73, 0x49,
|
||||
0xae, 0x20, 0x41, 0x31,
|
||||
0x12, 0xe6, 0xfa, 0x4e,
|
||||
0x89, 0xa9, 0x7e, 0xa2,
|
||||
0x0a, 0x9e, 0xee, 0xe6,
|
||||
0x4b, 0x55, 0xd3, 0x9a,
|
||||
0x21, 0x92, 0x99, 0x2a,
|
||||
0x27, 0x4f, 0xc1, 0xa8,
|
||||
0x36, 0xba, 0x3c, 0x23,
|
||||
0xa3, 0xfe, 0xeb, 0xbd,
|
||||
0x45, 0x4d, 0x44, 0x23,
|
||||
0x64, 0x3c, 0xe8, 0x0e,
|
||||
0x2a, 0x9a, 0xc9, 0x4f,
|
||||
0xa5, 0x4c, 0xa4, 0x9f ];
|
||||
public const string SpamSumZero = "QXX";
|
||||
public static readonly byte[] SpamSumZeroBytes = [0x51, 0x58, 0x58];
|
||||
|
||||
#endregion
|
||||
|
||||
#region Hash string length constants
|
||||
|
||||
public const int CRCLength = 8;
|
||||
public const int MD5Length = 32;
|
||||
public const int SHA1Length = 40;
|
||||
public const int SHA256Length = 64;
|
||||
public const int SHA384Length = 96;
|
||||
public const int SHA512Length = 128;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -46,18 +46,38 @@ namespace SabreTools.Hashing
|
||||
public static Dictionary<HashType, string?>? GetFileHashes(string filename)
|
||||
=> GetFileHashesAndSize(filename, out _);
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes from an input file path
|
||||
/// </summary>
|
||||
/// <param name="filename">Path to the input file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
public static Dictionary<HashType, byte[]?>? GetFileHashArrays(string filename)
|
||||
=> GetFileHashArraysAndSize(filename, out _);
|
||||
|
||||
/// <summary>
|
||||
/// Get a hash from an input file path
|
||||
/// </summary>
|
||||
/// <param name="filename">Path to the input file</param>
|
||||
/// <param name="hashType">Hash type to get from the file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
/// <returns>Hash on success, null on error</returns>
|
||||
public static string? GetFileHash(string filename, HashType hashType)
|
||||
{
|
||||
var hashes = GetFileHashes(filename, [hashType]);
|
||||
return hashes?[hashType];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a hash from an input file path
|
||||
/// </summary>
|
||||
/// <param name="filename">Path to the input file</param>
|
||||
/// <param name="hashType">Hash type to get from the file</param>
|
||||
/// <returns>Hash on success, null on error</returns>
|
||||
public static byte[]? GetFileHashArray(string filename, HashType hashType)
|
||||
{
|
||||
var hashes = GetFileHashArrays(filename, [hashType]);
|
||||
return hashes?[hashType];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes from an input file path
|
||||
/// </summary>
|
||||
@@ -67,6 +87,15 @@ namespace SabreTools.Hashing
|
||||
public static Dictionary<HashType, string?>? GetFileHashes(string filename, HashType[] hashTypes)
|
||||
=> GetFileHashesAndSize(filename, hashTypes, out _);
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes from an input file path
|
||||
/// </summary>
|
||||
/// <param name="filename">Path to the input file</param>
|
||||
/// <param name="hashTypes">Array of hash types to get from the file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
public static Dictionary<HashType, byte[]?>? GetFileHashArrays(string filename, HashType[] hashTypes)
|
||||
=> GetFileHashArraysAndSize(filename, hashTypes, out _);
|
||||
|
||||
#endregion
|
||||
|
||||
#region File Hashes With Size
|
||||
@@ -85,18 +114,44 @@ namespace SabreTools.Hashing
|
||||
return GetFileHashesAndSize(filename, hashTypes, out size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes and size from an input file path
|
||||
/// </summary>
|
||||
/// <param name="filename">Path to the input file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
public static Dictionary<HashType, byte[]?>? GetFileHashArraysAndSize(string filename, out long size)
|
||||
{
|
||||
// Create a hash array for all entries
|
||||
HashType[] hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
|
||||
|
||||
// Return the hashes from the stream
|
||||
return GetFileHashArraysAndSize(filename, hashTypes, out size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a hash and size from an input file path
|
||||
/// </summary>
|
||||
/// <param name="filename">Path to the input file</param>
|
||||
/// <param name="hashType">Hash type to get from the file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
/// <returns>Hash and size on success, null on error</returns>
|
||||
public static string? GetFileHashAndSize(string filename, HashType hashType, out long size)
|
||||
{
|
||||
var hashes = GetFileHashesAndSize(filename, [hashType], out size);
|
||||
return hashes?[hashType];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a hash and size from an input file path
|
||||
/// </summary>
|
||||
/// <param name="filename">Path to the input file</param>
|
||||
/// <param name="hashType">Hash type to get from the file</param>
|
||||
/// <returns>Hash and size on success, null on error</returns>
|
||||
public static byte[]? GetFileHashArrayAndSize(string filename, HashType hashType, out long size)
|
||||
{
|
||||
var hashes = GetFileHashArraysAndSize(filename, [hashType], out size);
|
||||
return hashes?[hashType];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes and size from an input file path
|
||||
/// </summary>
|
||||
@@ -122,6 +177,31 @@ namespace SabreTools.Hashing
|
||||
return GetStreamHashes(input, hashTypes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes and size from an input file path
|
||||
/// </summary>
|
||||
/// <param name="filename">Path to the input file</param>
|
||||
/// <param name="hashTypes">Array of hash types to get from the file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
public static Dictionary<HashType, byte[]?>? GetFileHashArraysAndSize(string filename, HashType[] hashTypes, out long size)
|
||||
{
|
||||
// If the file doesn't exist, we can't do anything
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
size = -1;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Set the file size
|
||||
size = new FileInfo(filename).Length;
|
||||
|
||||
// Open the input file
|
||||
var input = File.OpenRead(filename);
|
||||
|
||||
// Return the hashes from the stream
|
||||
return GetStreamHashArrays(input, hashTypes);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Byte Array Hashes
|
||||
@@ -140,18 +220,44 @@ namespace SabreTools.Hashing
|
||||
return GetStreamHashes(new MemoryStream(input), hashTypes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes from an input byte array
|
||||
/// </summary>
|
||||
/// <param name="input">Byte array to hash</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
public static Dictionary<HashType, byte[]?>? GetByteArrayHashArrays(byte[] input)
|
||||
{
|
||||
// Create a hash array for all entries
|
||||
HashType[] hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
|
||||
|
||||
// Return the hashes from the stream
|
||||
return GetStreamHashArrays(new MemoryStream(input), hashTypes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a hash from an input byte array
|
||||
/// </summary>
|
||||
/// <param name="input">Byte array to hash</param>
|
||||
/// <param name="hashType">Hash type to get from the file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
/// <returns>Hash on success, null on error</returns>
|
||||
public static string? GetByteArrayHash(byte[] input, HashType hashType)
|
||||
{
|
||||
var hashes = GetStreamHashes(new MemoryStream(input), [hashType]);
|
||||
return hashes?[hashType];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a hash from an input byte array
|
||||
/// </summary>
|
||||
/// <param name="input">Byte array to hash</param>
|
||||
/// <param name="hashType">Hash type to get from the file</param>
|
||||
/// <returns>Hash on success, null on error</returns>
|
||||
public static byte[]? GetByteArrayHashArray(byte[] input, HashType hashType)
|
||||
{
|
||||
var hashes = GetStreamHashArrays(new MemoryStream(input), [hashType]);
|
||||
return hashes?[hashType];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes from an input byte array
|
||||
/// </summary>
|
||||
@@ -161,6 +267,15 @@ namespace SabreTools.Hashing
|
||||
public static Dictionary<HashType, string?>? GetByteArrayHashes(byte[] input, HashType[] hashTypes)
|
||||
=> GetStreamHashes(new MemoryStream(input), hashTypes);
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes from an input byte array
|
||||
/// </summary>
|
||||
/// <param name="input">Byte array to hash</param>
|
||||
/// <param name="hashTypes">Array of hash types to get from the file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
public static Dictionary<HashType, byte[]?>? GetByteArrayHashArrays(byte[] input, HashType[] hashTypes)
|
||||
=> GetStreamHashArrays(new MemoryStream(input), hashTypes);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Stream Hashes
|
||||
@@ -170,13 +285,27 @@ namespace SabreTools.Hashing
|
||||
/// </summary>
|
||||
/// <param name="input">Stream to hash</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
public static Dictionary<HashType, string?>? GetStreamHashes(Stream input)
|
||||
public static Dictionary<HashType, string?>? GetStreamHashes(Stream input, bool leaveOpen = false)
|
||||
{
|
||||
// Create a hash array for all entries
|
||||
HashType[] hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
|
||||
|
||||
// Get the output hashes
|
||||
return GetStreamHashes(input, hashTypes);
|
||||
return GetStreamHashes(input, hashTypes, leaveOpen);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes from an input Stream
|
||||
/// </summary>
|
||||
/// <param name="input">Stream to hash</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
public static Dictionary<HashType, byte[]?>? GetStreamHashArrays(Stream input, bool leaveOpen = false)
|
||||
{
|
||||
// Create a hash array for all entries
|
||||
HashType[] hashTypes = (HashType[])Enum.GetValues(typeof(HashType));
|
||||
|
||||
// Get the output hashes
|
||||
return GetStreamHashArrays(input, hashTypes, leaveOpen);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -184,10 +313,22 @@ namespace SabreTools.Hashing
|
||||
/// </summary>
|
||||
/// <param name="input">Stream to hash</param>
|
||||
/// <param name="hashType">Hash type to get from the file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
public static string? GetStreamHash(Stream input, HashType hashType)
|
||||
/// <returns>Hash on success, null on error</returns>
|
||||
public static string? GetStreamHash(Stream input, HashType hashType, bool leaveOpen = false)
|
||||
{
|
||||
var hashes = GetStreamHashes(input, [hashType]);
|
||||
var hashes = GetStreamHashes(input, [hashType], leaveOpen);
|
||||
return hashes?[hashType];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a hash and size from an input Stream
|
||||
/// </summary>
|
||||
/// <param name="input">Stream to hash</param>
|
||||
/// <param name="hashType">Hash type to get from the file</param>
|
||||
/// <returns>Hash on success, null on error</returns>
|
||||
public static byte[]? GetStreamHashArray(Stream input, HashType hashType, bool leaveOpen = false)
|
||||
{
|
||||
var hashes = GetStreamHashArrays(input, [hashType], leaveOpen);
|
||||
return hashes?[hashType];
|
||||
}
|
||||
|
||||
@@ -197,7 +338,7 @@ namespace SabreTools.Hashing
|
||||
/// <param name="input">Stream to hash</param>
|
||||
/// <param name="hashTypes">Array of hash types to get from the file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
public static Dictionary<HashType, string?>? GetStreamHashes(Stream input, HashType[] hashTypes)
|
||||
public static Dictionary<HashType, string?>? GetStreamHashes(Stream input, HashType[] hashTypes, bool leaveOpen = false)
|
||||
{
|
||||
// Create the output dictionary
|
||||
var hashDict = new Dictionary<HashType, string?>();
|
||||
@@ -297,7 +438,119 @@ namespace SabreTools.Hashing
|
||||
}
|
||||
finally
|
||||
{
|
||||
input.Dispose();
|
||||
if (!leaveOpen)
|
||||
input.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get hashes from an input Stream
|
||||
/// </summary>
|
||||
/// <param name="input">Stream to hash</param>
|
||||
/// <param name="hashTypes">Array of hash types to get from the file</param>
|
||||
/// <returns>Dictionary containing hashes on success, null on error</returns>
|
||||
private static Dictionary<HashType, byte[]?>? GetStreamHashArrays(Stream input, HashType[] hashTypes, bool leaveOpen = false)
|
||||
{
|
||||
// Create the output dictionary
|
||||
var hashDict = new Dictionary<HashType, byte[]?>();
|
||||
|
||||
try
|
||||
{
|
||||
// Get a list of hashers to run over the buffer
|
||||
var hashers = new Dictionary<HashType, HashWrapper>();
|
||||
|
||||
// Add hashers based on requested types
|
||||
foreach (HashType hashType in hashTypes)
|
||||
{
|
||||
hashers[hashType] = new HashWrapper(hashType);
|
||||
}
|
||||
|
||||
// Initialize the hashing helpers
|
||||
var loadBuffer = new ThreadLoadBuffer(input);
|
||||
int buffersize = 3 * 1024 * 1024;
|
||||
byte[] buffer0 = new byte[buffersize];
|
||||
byte[] buffer1 = new byte[buffersize];
|
||||
|
||||
/*
|
||||
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
|
||||
long refsize = input.Length;
|
||||
int next = refsize > buffersize ? buffersize : (int)refsize;
|
||||
input.Read(buffer0, 0, next);
|
||||
int current = next;
|
||||
refsize -= next;
|
||||
bool bufferSelect = true;
|
||||
|
||||
while (current > 0)
|
||||
{
|
||||
// Trigger the buffer load on the second buffer
|
||||
next = refsize > buffersize ? buffersize : (int)refsize;
|
||||
if (next > 0)
|
||||
loadBuffer.Trigger(bufferSelect ? buffer1 : buffer0, next);
|
||||
|
||||
byte[] buffer = bufferSelect ? buffer0 : buffer1;
|
||||
|
||||
#if NET20 || NET35
|
||||
// Run hashers sequentially on each chunk
|
||||
foreach (var h in hashers)
|
||||
{
|
||||
h.Value.Process(buffer, 0, current);
|
||||
}
|
||||
#else
|
||||
// Run hashers in parallel on each chunk
|
||||
Parallel.ForEach(hashers, h => h.Value.Process(buffer, 0, current));
|
||||
#endif
|
||||
|
||||
// Wait for the load buffer worker, if needed
|
||||
if (next > 0)
|
||||
loadBuffer.Wait();
|
||||
|
||||
// Setup for the next hashing step
|
||||
current = next;
|
||||
refsize -= next;
|
||||
bufferSelect = !bufferSelect;
|
||||
}
|
||||
|
||||
// Finalize all hashing helpers
|
||||
loadBuffer.Finish();
|
||||
#if NET20 || NET35
|
||||
foreach (var h in hashers)
|
||||
{
|
||||
h.Value.Terminate();
|
||||
}
|
||||
#else
|
||||
Parallel.ForEach(hashers, h => h.Value.Terminate());
|
||||
#endif
|
||||
|
||||
// Get the results
|
||||
foreach (var hasher in hashers)
|
||||
{
|
||||
hashDict[hasher.Key] = hasher.Value.CurrentHashBytes;
|
||||
}
|
||||
|
||||
// Dispose of the hashers
|
||||
loadBuffer.Dispose();
|
||||
foreach (var hasher in hashers.Values)
|
||||
{
|
||||
hasher.Dispose();
|
||||
}
|
||||
|
||||
return hashDict;
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!leaveOpen)
|
||||
input.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,7 @@ using System;
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
using System.IO.Hashing;
|
||||
#endif
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
using System.Linq;
|
||||
#endif
|
||||
using System.Security.Cryptography;
|
||||
using Aaru.Checksums;
|
||||
using Aaru.CommonTypes.Interfaces;
|
||||
@@ -40,6 +38,8 @@ namespace SabreTools.Hashing
|
||||
IChecksum ic => ic.Final(),
|
||||
#if NET462_OR_GREATER || NETCOREAPP
|
||||
NonCryptographicHashAlgorithm ncha => ncha.GetCurrentHash().Reverse().ToArray(),
|
||||
#else
|
||||
OptimizedCRC.OptimizedCRC ocrc => BitConverter.GetBytes(ocrc.Value).Reverse().ToArray(),
|
||||
#endif
|
||||
_ => null,
|
||||
};
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
<Version>1.1.2</Version>
|
||||
<Version>1.1.4</Version>
|
||||
|
||||
<!-- Package Properties -->
|
||||
<Authors>Matt Nadareski</Authors>
|
||||
@@ -28,6 +28,9 @@
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Support for old .NET versions -->
|
||||
<ItemGroup Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`))">
|
||||
<PackageReference Include="Net30.LinqBridge" Version="1.3.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`)) OR $(TargetFramework.StartsWith(`net40`))">
|
||||
<PackageReference Include="MinAsyncBridge" Version="0.12.4" />
|
||||
<PackageReference Include="MinTasksExtensionsBridge" Version="0.3.4" />
|
||||
|
||||
Reference in New Issue
Block a user