Add option for choosing hash algo

This commit is contained in:
ProfessionalUwU 2023-07-02 15:32:46 +02:00
parent 9d6b1385c8
commit d80a5f5e6b
Signed by: ProfessionalUwU
GPG Key ID: 9F28CB1645C4BFB5
2 changed files with 21 additions and 13 deletions

View File

@ -8,7 +8,7 @@ public class Program {
Console.WriteLine("Please specify an option.");
PrintAvailableOptions();
return;
} else if (args.Length > 1 && args[0] != "compareDatabases") {
} else if (args.Length > 3) {
Console.WriteLine("Too many options.");
return;
}
@ -25,7 +25,15 @@ public class Program {
Console.WriteLine("Starting the checksum process.");
Console.ResetColor();
utils.doTheThing();
try {
int bufferSize = int.Parse(args[2]);
utils.doTheThing(args[1], bufferSize);
}
catch (FormatException) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Buffer was not a valid integer value. Please specify a valid integer value for the buffer size");
Console.ResetColor();
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Checksum process finished");
@ -57,7 +65,7 @@ public class Program {
static void PrintAvailableOptions() {
String[] options = {
"checksum",
"checksum - MD5, Murmur and XxHash",
"compareDatabases",
"compareChecksums",
"createDB",

View File

@ -109,12 +109,12 @@ public class ChksumUtils {
return new Dictionary<string, string>(checksums);
}
private Dictionary<string, uint> CalculateChecksumsWithMurmur(string[] filenames) {
private Dictionary<string, uint> CalculateChecksumsWithMurmur(string[] filenames, int userDefinedBufferSize) {
ConcurrentDictionary<string, uint> checksums = new ConcurrentDictionary<string, uint>();
Parallel.ForEach(filenames, (filename, state) => {
using (var stream = File.OpenRead(filename)) {
var hash = CalculateMurmurHash32(stream);
var hash = CalculateMurmurHash32(stream, userDefinedBufferSize);
lock (checksums) {
checksums.TryAdd(filename, hash);
}
@ -125,8 +125,8 @@ public class ChksumUtils {
return new Dictionary<string, uint>(checksums);
}
private uint CalculateMurmurHash32(Stream stream) {
const int bufferSize = 4096;
private uint CalculateMurmurHash32(Stream stream, int userDefinedBufferSize) {
int bufferSize = userDefinedBufferSize;
const uint seed = 123456U;
var buffer = new byte[bufferSize];
@ -141,12 +141,12 @@ public class ChksumUtils {
return hash;
}
private Dictionary<string, ulong> CalculateChecksumsWithXxHash3(string[] filenames) {
private Dictionary<string, ulong> CalculateChecksumsWithXxHash3(string[] filenames, int userDefinedBufferSize) {
ConcurrentDictionary<string, ulong> checksums = new ConcurrentDictionary<string, ulong>();
Parallel.ForEach(filenames, (filename, state) => {
using (var stream = File.OpenRead(filename)) {
var hash = CalculateXxHash3(stream);
var hash = CalculateXxHash3(stream, userDefinedBufferSize);
checksums.TryAdd(filename, hash);
}
});
@ -154,8 +154,8 @@ public class ChksumUtils {
return new Dictionary<string, ulong>(checksums);
}
private ulong CalculateXxHash3(Stream stream) {
const int bufferSize = 4096;
private ulong CalculateXxHash3(Stream stream, int userDefinedBufferSize) {
int bufferSize = userDefinedBufferSize;
const ulong seed = 123456U;
var buffer = new byte[bufferSize];
@ -190,11 +190,11 @@ public class ChksumUtils {
fileHashes = fileHashesMD5.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
break;
case "Murmur":
fileHashesMurmur = CalculateChecksumsWithMurmur(indexFiles());
fileHashesMurmur = CalculateChecksumsWithMurmur(indexFiles(), bufferSize);
fileHashes = fileHashesMurmur.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
break;
case "XxHash":
fileHashesXxHash3 = CalculateChecksumsWithXxHash3(indexFiles());
fileHashesXxHash3 = CalculateChecksumsWithXxHash3(indexFiles(), bufferSize);
fileHashes = fileHashesXxHash3.ToDictionary(kv => kv.Key, kv => (object)kv.Value);
break;
default: