mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
[DatTools] Better hashing
This commit is contained in:
@@ -1990,8 +1990,10 @@ namespace SabreTools.Helper
|
|||||||
/// Retrieve file information for a single file
|
/// Retrieve file information for a single file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input">Filename to get information from</param>
|
/// <param name="input">Filename to get information from</param>
|
||||||
|
/// <param name="noMD5">True if MD5 hashes should not be calculated, false otherwise</param>
|
||||||
|
/// <param name="noSHA1">True if SHA-1 hashes should not be calcluated, false otherwise</param>
|
||||||
/// <returns>Populated RomData object if success, empty one on error</returns>
|
/// <returns>Populated RomData object if success, empty one on error</returns>
|
||||||
public static RomData GetSingleFileInfo(string input)
|
public static RomData GetSingleFileInfo(string input, bool noMD5 = false, bool noSHA1 = false)
|
||||||
{
|
{
|
||||||
RomData rom = new RomData
|
RomData rom = new RomData
|
||||||
{
|
{
|
||||||
@@ -2005,24 +2007,39 @@ namespace SabreTools.Helper
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Crc32 crc = new Crc32();
|
using (Crc32 crc = new Crc32())
|
||||||
MD5 md5 = MD5.Create();
|
using (MD5 md5 = MD5.Create())
|
||||||
SHA1 sha1 = SHA1.Create();
|
using (SHA1 sha1 = SHA1.Create())
|
||||||
|
using (FileStream fs = File.OpenRead(input))
|
||||||
using (FileStream fs = File.Open(input, FileMode.Open))
|
|
||||||
{
|
{
|
||||||
foreach (byte b in crc.ComputeHash(fs))
|
byte[] buffer = new byte[1024];
|
||||||
|
int read;
|
||||||
|
while ((read = fs.Read(buffer, 0, buffer.Length)) > 0)
|
||||||
{
|
{
|
||||||
rom.CRC += b.ToString("x2").ToLowerInvariant();
|
crc.TransformBlock(buffer, 0, read, buffer, 0);
|
||||||
|
if (!noMD5)
|
||||||
|
{
|
||||||
|
md5.TransformBlock(buffer, 0, read, buffer, 0);
|
||||||
|
}
|
||||||
|
if (!noSHA1)
|
||||||
|
{
|
||||||
|
sha1.TransformBlock(buffer, 0, read, buffer, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
crc.TransformFinalBlock(buffer, 0, 0);
|
||||||
|
rom.CRC = BitConverter.ToString(crc.Hash).Replace("-", "").ToLowerInvariant();
|
||||||
|
|
||||||
|
if (!noMD5)
|
||||||
|
{
|
||||||
|
md5.TransformFinalBlock(buffer, 0, 0);
|
||||||
|
rom.MD5 = BitConverter.ToString(md5.Hash).Replace("-", "").ToLowerInvariant();
|
||||||
|
}
|
||||||
|
if (!noSHA1)
|
||||||
|
{
|
||||||
|
sha1.TransformFinalBlock(buffer, 0, 0);
|
||||||
|
rom.SHA1 = BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
using (FileStream fs = File.Open(input, FileMode.Open))
|
|
||||||
{
|
|
||||||
rom.MD5 = BitConverter.ToString(md5.ComputeHash(fs)).Replace("-", "").ToLowerInvariant();
|
|
||||||
}
|
|
||||||
using (FileStream fs = File.Open(input, FileMode.Open))
|
|
||||||
{
|
|
||||||
rom.SHA1 = BitConverter.ToString(sha1.ComputeHash(fs)).Replace("-", "").ToLowerInvariant();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException)
|
catch (IOException)
|
||||||
|
|||||||
@@ -371,7 +371,7 @@ namespace SabreTools
|
|||||||
private string ProcessFile(string item, StreamWriter sw, string basepath, string parent, DatData datdata, string lastparent)
|
private string ProcessFile(string item, StreamWriter sw, string basepath, string parent, DatData datdata, string lastparent)
|
||||||
{
|
{
|
||||||
_logger.Log(Path.GetFileName(item) + " treated like a file");
|
_logger.Log(Path.GetFileName(item) + " treated like a file");
|
||||||
RomData rom = DatTools.GetSingleFileInfo(item);
|
RomData rom = DatTools.GetSingleFileInfo(item, _noMD5, _noSHA1);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user