Rename checksum methods based on HashAlgorithm

This commit is contained in:
Matt Nadareski
2025-01-02 14:35:40 -05:00
parent 80448302e8
commit e5fea69815
14 changed files with 36 additions and 45 deletions

View File

@@ -8,19 +8,19 @@ namespace SabreTools.Hashing.Checksum
{
public Adler32()
{
Reset();
Initialize();
}
/// <summary>
/// Reset the internal hashing state
/// </summary>
public override void Reset()
public override void Initialize()
{
_hash = 1;
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
{
// Split Adler-32 into component sums
uint sum2 = (_hash >> 16) & 0xffff;
@@ -131,7 +131,7 @@ namespace SabreTools.Hashing.Checksum
}
/// <inheritdoc/>
public override byte[] Finalize()
public override byte[] HashFinal()
{
return BitConverter.GetBytes(_hash);
}

View File

@@ -7,18 +7,11 @@ namespace SabreTools.Hashing.Checksum
/// </summary>
public abstract class ChecksumBase
{
/// <summary>
/// Hash a block of data and append it to the existing hash
/// </summary>
/// <param name="data">Byte array representing the data</param>
/// <param name="offset">Offset in the byte array to include</param>
/// <param name="length">Length of the data to hash</param>
public abstract void TransformBlock(byte[] data, int offset, int length);
/// <inheritdoc cref="System.Security.Cryptography.HashAlgorithm.HashCore(byte[], int, int)"/>
public abstract void HashCore(byte[] data, int offset, int length);
/// <summary>
/// Finalize the hash and return as a byte array
/// </summary>
public abstract byte[] Finalize();
/// <inheritdoc cref="System.Security.Cryptography.HashAlgorithm.HashFinal"/>
public abstract byte[] HashFinal();
}
/// <summary>
@@ -31,16 +24,14 @@ namespace SabreTools.Hashing.Checksum
/// </summary>
protected T _hash;
/// <summary>
/// Reset the internal hashing state
/// </summary>
public virtual void Reset()
/// <inheritdoc cref="System.Security.Cryptography.HashAlgorithm.Initialize"/>
public virtual void Initialize()
{
_hash = default;
}
/// <inheritdoc/>
public override byte[] Finalize()
public override byte[] HashFinal()
{
return _hash switch
{

View File

@@ -27,17 +27,17 @@ namespace SabreTools.Hashing.Checksum
}
/// <inheritdoc/>
public override void Reset()
public override void Initialize()
{
_hash = Def.Init;
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
=> _table.TransformBlock(ref _hash, data, offset, length);
/// <inheritdoc/>
public override byte[] Finalize()
public override byte[] HashFinal()
{
// Create a copy of the hash
ulong localHash = _hash;

View File

@@ -8,11 +8,11 @@ namespace SabreTools.Hashing.Checksum
{
_basis = 0;
_prime = FNV32Prime;
Reset();
Initialize();
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
{
for (int i = offset; length > 0; i++, length--)
{

View File

@@ -8,11 +8,11 @@ namespace SabreTools.Hashing.Checksum
{
_basis = 0;
_prime = FNV64Prime;
Reset();
Initialize();
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
{
for (int i = offset; length > 0; i++, length--)
{

View File

@@ -8,11 +8,11 @@ namespace SabreTools.Hashing.Checksum
{
_basis = FNV32Basis;
_prime = FNV32Prime;
Reset();
Initialize();
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
{
for (int i = offset; length > 0; i++, length--)
{

View File

@@ -8,11 +8,11 @@ namespace SabreTools.Hashing.Checksum
{
_basis = FNV64Basis;
_prime = FNV64Prime;
Reset();
Initialize();
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
{
for (int i = offset; length > 0; i++, length--)
{

View File

@@ -8,11 +8,11 @@ namespace SabreTools.Hashing.Checksum
{
_basis = FNV32Basis;
_prime = FNV32Prime;
Reset();
Initialize();
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
{
for (int i = offset; length > 0; i++, length--)
{

View File

@@ -8,11 +8,11 @@ namespace SabreTools.Hashing.Checksum
{
_basis = FNV64Basis;
_prime = FNV64Prime;
Reset();
Initialize();
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
{
for (int i = offset; length > 0; i++, length--)
{

View File

@@ -5,11 +5,11 @@ namespace SabreTools.Hashing.Checksum
{
public Fletcher16()
{
Reset();
Initialize();
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
{
// Split the existing hash
uint c0 = (uint)(_hash & 0x00FF);

View File

@@ -8,11 +8,11 @@ namespace SabreTools.Hashing.Checksum
{
public Fletcher32()
{
Reset();
Initialize();
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
{
// Split Fletcher-32 into component sums
uint c0 = _hash & 0xffff;

View File

@@ -8,11 +8,11 @@ namespace SabreTools.Hashing.Checksum
{
public Fletcher64()
{
Reset();
Initialize();
}
/// <inheritdoc/>
public override void TransformBlock(byte[] data, int offset, int length)
public override void HashCore(byte[] data, int offset, int length)
{
// Split Fletcher-64 into component sums
ulong c0 = _hash & 0xffffffff;

View File

@@ -13,7 +13,7 @@ namespace SabreTools.Hashing.Checksum
protected T _prime;
/// <inheritdoc/>
public override void Reset()
public override void Initialize()
{
_hash = _basis;
}

View File

@@ -35,7 +35,7 @@ namespace SabreTools.Hashing
switch (_hasher)
{
case ChecksumBase cb:
var cbArr = cb.Finalize();
var cbArr = cb.HashFinal();
Array.Reverse(cbArr);
return cbArr;
@@ -88,7 +88,7 @@ namespace SabreTools.Hashing
switch (_hasher)
{
case Crc cr:
var crArr = cr.Finalize();
var crArr = cr.HashFinal();
ulong crHash = BytesToUInt64(crArr);
int length = cr.Def.Width / 4 + (cr.Def.Width % 4 > 0 ? 1 : 0);
return crHash.ToString($"x{length}");
@@ -354,7 +354,7 @@ namespace SabreTools.Hashing
switch (_hasher)
{
case ChecksumBase cb:
cb.TransformBlock(buffer, offset, size);
cb.HashCore(buffer, offset, size);
break;
case HashAlgorithm ha: