Files
SabreTools/SabreTools.Library/Tools/Hasher.cs

158 lines
4.1 KiB
C#
Raw Normal View History

2020-06-05 22:26:44 -07:00
using System;
using System.Linq;
using System.Security.Cryptography;
2020-12-07 12:33:24 -08:00
using Aaru.Checksums;
2020-08-01 22:10:29 -07:00
using SabreTools.Library.DatFiles;
2020-08-01 23:12:44 -07:00
using SabreTools.Library.External;
2020-06-05 22:26:44 -07:00
namespace SabreTools.Library.Tools
{
/// <summary>
/// Async hashing class wraper
/// </summary>
2020-06-05 22:26:44 -07:00
public class Hasher
{
public Hash HashType { get; private set; }
private IDisposable _hasher;
public Hasher(Hash hashType)
{
this.HashType = hashType;
GetHasher();
}
/// <summary>
/// Generate the correct hashing class based on the hash type
/// </summary>
private void GetHasher()
{
switch (HashType)
{
case Hash.CRC:
_hasher = new OptimizedCRC();
break;
case Hash.MD5:
_hasher = MD5.Create();
break;
#if NET_FRAMEWORK
case Hash.RIPEMD160:
_hasher = RIPEMD160.Create();
break;
#endif
case Hash.SHA1:
_hasher = SHA1.Create();
break;
case Hash.SHA256:
_hasher = SHA256.Create();
break;
case Hash.SHA384:
_hasher = SHA384.Create();
break;
case Hash.SHA512:
_hasher = SHA512.Create();
break;
case Hash.SpamSum:
_hasher = new SpamSumContext();
break;
2020-06-05 22:26:44 -07:00
}
}
public void Dispose()
{
_hasher.Dispose();
}
/// <summary>
/// Process a buffer of some length with the internal hash algorithm
/// </summary>
2020-10-05 17:43:44 -07:00
public void Process(byte[] buffer, int size)
2020-06-05 22:26:44 -07:00
{
switch (HashType)
{
case Hash.CRC:
2020-10-05 17:43:44 -07:00
(_hasher as OptimizedCRC).Update(buffer, 0, size);
2020-06-05 22:26:44 -07:00
break;
case Hash.MD5:
#if NET_FRAMEWORK
case Hash.RIPEMD160:
#endif
case Hash.SHA1:
case Hash.SHA256:
case Hash.SHA384:
case Hash.SHA512:
2020-10-05 17:43:44 -07:00
(_hasher as HashAlgorithm).TransformBlock(buffer, 0, size, null, 0);
2020-06-05 22:26:44 -07:00
break;
case Hash.SpamSum:
2020-10-05 17:43:44 -07:00
(_hasher as SpamSumContext).Update(buffer);
break;
2020-06-05 22:26:44 -07:00
}
}
/// <summary>
/// Finalize the internal hash algorigthm
/// </summary>
2020-10-05 17:43:44 -07:00
public void Finalize()
2020-06-05 22:26:44 -07:00
{
byte[] emptyBuffer = new byte[0];
switch (HashType)
{
case Hash.CRC:
2020-10-05 17:43:44 -07:00
(_hasher as OptimizedCRC).Update(emptyBuffer, 0, 0);
2020-06-05 22:26:44 -07:00
break;
case Hash.MD5:
#if NET_FRAMEWORK
case Hash.RIPEMD160:
#endif
case Hash.SHA1:
case Hash.SHA256:
case Hash.SHA384:
case Hash.SHA512:
2020-10-05 17:43:44 -07:00
(_hasher as HashAlgorithm).TransformFinalBlock(emptyBuffer, 0, 0);
2020-06-05 22:26:44 -07:00
break;
case Hash.SpamSum:
// No finalization step needed
break;
2020-06-05 22:26:44 -07:00
}
}
/// <summary>
/// Get internal hash as a byte array
/// </summary>
2020-06-05 22:26:44 -07:00
public byte[] GetHash()
{
switch (HashType)
{
case Hash.CRC:
return BitConverter.GetBytes((_hasher as OptimizedCRC).Value).Reverse().ToArray();
case Hash.MD5:
#if NET_FRAMEWORK
case Hash.RIPEMD160:
#endif
case Hash.SHA1:
case Hash.SHA256:
case Hash.SHA384:
case Hash.SHA512:
return (_hasher as HashAlgorithm).Hash;
case Hash.SpamSum:
return (_hasher as SpamSumContext).Final();
2020-06-05 22:26:44 -07:00
}
return null;
}
}
}