Skip to content

Commit

Permalink
imp - Condensed hash verification
Browse files Browse the repository at this point in the history
---

We've condensed hash verification logic by adding a plain hash verification function and using it.

---

Type: imp
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Feb 15, 2024
1 parent f8d7fd2 commit b4d3464
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-rel.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build Project (Linux, Release)
name: Build Project (Release)

on:
push:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build Project (Linux)
name: Build Project (Debug)

on:
push:
Expand Down
140 changes: 55 additions & 85 deletions public/Nitrocid/Drivers/Encryption/HashVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public static class HashVerifier
/// <exception cref="FileNotFoundException"></exception>
public static bool VerifyHashFromHashesFile(string FileName, string HashType, string HashesFile, string ActualHash)
{
int ExpectedHashLength;
string ExpectedHash = "";

FileName = FS.NeutralizePath(FileName);
Expand All @@ -52,7 +51,6 @@ public static bool VerifyHashFromHashesFile(string FileName, string HashType, st
if (Checking.FileExists(FileName))
{
DebugWriter.WriteDebug(DebugLevel.I, "Hash type: {0} ({1})", HashType, HashType.ToString());
ExpectedHashLength = GetExpectedHashLength(HashType);

// Verify the hash
if (Checking.FileExists(HashesFile))
Expand Down Expand Up @@ -91,26 +89,8 @@ public static bool VerifyHashFromHashesFile(string FileName, string HashType, st
throw new KernelException(KernelExceptionType.Encryption, "Hashes file {0} not found.", HashesFile);
}

if (ActualHash.Length == ExpectedHashLength & ExpectedHash.Length == ExpectedHashLength)
{
DebugWriter.WriteDebug(DebugLevel.I, "Hashes are consistent.");
DebugWriter.WriteDebug(DebugLevel.I, "Hashes {0} and {1}", ActualHash, ExpectedHash);
if (ActualHash == ExpectedHash)
{
DebugWriter.WriteDebug(DebugLevel.I, "Hashes match.");
return true;
}
else
{
DebugWriter.WriteDebug(DebugLevel.W, "Hashes don't match.");
return false;
}
}
else
{
DebugWriter.WriteDebug(DebugLevel.E, "{0} ({1}) or {2} ({3}) is malformed. Check the algorithm ({4}). Expected length: {5}", ActualHash, ActualHash.Length, ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
throw new KernelException(KernelExceptionType.InvalidHash, "{0} ({1}) or {2} ({3}) is malformed. Check the algorithm ({4}). Expected length: {5}", ActualHash, ActualHash.Length, ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
}
// Verify the hash
return VerifyHashPlain(HashType, ExpectedHash, ActualHash);
}
else
{
Expand All @@ -129,38 +109,16 @@ public static bool VerifyHashFromHashesFile(string FileName, string HashType, st
/// <exception cref="FileNotFoundException"></exception>
public static bool VerifyHashFromHash(string FileName, string HashType, string ExpectedHash, string ActualHash)
{
int ExpectedHashLength;

FileName = FS.NeutralizePath(FileName);
ExpectedHash = ExpectedHash.ToUpper();
ActualHash = ActualHash.ToUpper();
DebugWriter.WriteDebug(DebugLevel.I, "File name: {0}", FileName);
if (Checking.FileExists(FileName))
{
DebugWriter.WriteDebug(DebugLevel.I, "Hash type: {0} ({1})", HashType, HashType.ToString());
ExpectedHashLength = GetExpectedHashLength(HashType);

// Verify the hash
if (ActualHash.Length == ExpectedHashLength & ExpectedHash.Length == ExpectedHashLength)
{
DebugWriter.WriteDebug(DebugLevel.I, "Hashes are consistent.");
DebugWriter.WriteDebug(DebugLevel.I, "Hashes {0} and {1}", ActualHash, ExpectedHash);
if (ActualHash == ExpectedHash)
{
DebugWriter.WriteDebug(DebugLevel.I, "Hashes match.");
return true;
}
else
{
DebugWriter.WriteDebug(DebugLevel.W, "Hashes don't match.");
return false;
}
}
else
{
DebugWriter.WriteDebug(DebugLevel.E, "{0} ({1}) or {2} ({3}) is malformed. Check the algorithm ({4}). Expected length: {5}", ActualHash, ActualHash.Length, ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
throw new KernelException(KernelExceptionType.InvalidHash, "{0} ({1}) or {2} ({3}) is malformed. Check the algorithm ({4}). Expected length: {5}", ActualHash, ActualHash.Length, ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
}
return VerifyHashPlain(HashType, ExpectedHash, ActualHash);
}
else
{
Expand All @@ -178,7 +136,6 @@ public static bool VerifyHashFromHash(string FileName, string HashType, string E
/// <exception cref="FileNotFoundException"></exception>
public static bool VerifyUncalculatedHashFromHashesFile(string FileName, string HashType, string HashesFile)
{
int ExpectedHashLength;
string ExpectedHash = "";
string ActualHash = "";

Expand All @@ -189,7 +146,6 @@ public static bool VerifyUncalculatedHashFromHashesFile(string FileName, string
if (Checking.FileExists(FileName))
{
DebugWriter.WriteDebug(DebugLevel.I, "Hash type: {0} ({1})", HashType, HashType.ToString());
ExpectedHashLength = GetExpectedHashLength(HashType);

// Verify the hash
if (Checking.FileExists(HashesFile))
Expand Down Expand Up @@ -228,26 +184,8 @@ public static bool VerifyUncalculatedHashFromHashesFile(string FileName, string
throw new KernelException(KernelExceptionType.Encryption, "Hashes file {0} not found.", HashesFile);
}

if (ActualHash.Length == ExpectedHashLength & ExpectedHash.Length == ExpectedHashLength)
{
DebugWriter.WriteDebug(DebugLevel.I, "Hashes are consistent.");
DebugWriter.WriteDebug(DebugLevel.I, "Hashes {0} and {1}", ActualHash, ExpectedHash);
if (ActualHash == ExpectedHash)
{
DebugWriter.WriteDebug(DebugLevel.I, "Hashes match.");
return true;
}
else
{
DebugWriter.WriteDebug(DebugLevel.W, "Hashes don't match.");
return false;
}
}
else
{
DebugWriter.WriteDebug(DebugLevel.E, "{0} ({1}) or {2} ({3}) is malformed. Check the algorithm ({4}). Expected length: {5}", ActualHash, ActualHash.Length, ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
throw new KernelException(KernelExceptionType.InvalidHash, "{0} ({1}) or {2} ({3}) is malformed. Check the algorithm ({4}). Expected length: {5}", ActualHash, ActualHash.Length, ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
}
// Verify the hash
return VerifyHashPlain(HashType, ExpectedHash, ActualHash);
}
else
{
Expand All @@ -265,44 +203,76 @@ public static bool VerifyUncalculatedHashFromHashesFile(string FileName, string
/// <exception cref="FileNotFoundException"></exception>
public static bool VerifyUncalculatedHashFromHash(string FileName, string HashType, string ExpectedHash)
{
int ExpectedHashLength;
string ActualHash;
FileName = FS.NeutralizePath(FileName);
ExpectedHash = ExpectedHash.ToUpper();
DebugWriter.WriteDebug(DebugLevel.I, "File name: {0}", FileName);
if (Checking.FileExists(FileName))
{
DebugWriter.WriteDebug(DebugLevel.I, "Hash type: {0} ({1})", HashType, HashType.ToString());
ExpectedHashLength = GetExpectedHashLength(HashType);

// Calculate the file hash
ActualHash = Encryption.GetEncryptedFile(FileName, HashType).ToUpper();

// Verify the hash
if (ActualHash.Length == ExpectedHashLength & ExpectedHash.Length == ExpectedHashLength)
return VerifyHashPlain(HashType, ExpectedHash, ActualHash);
}
else
{
throw new KernelException(KernelExceptionType.Encryption, "File {0} not found.", FileName);
}
}

/// <summary>
/// Verifies the provided hash sum from expected hash
/// </summary>
/// <param name="HashType">Hash algorithm</param>
/// <param name="ExpectedHash">Expected hash</param>
/// <param name="ActualHash">Actual hash calculated from hash tool</param>
/// <returns>True if they match; else, false.</returns>
/// <exception cref="FileNotFoundException"></exception>
public static bool VerifyHashPlain(string HashType, string ExpectedHash, string ActualHash)
{
ExpectedHash = ExpectedHash.ToUpper();
ActualHash = ActualHash.ToUpper();
DebugWriter.WriteDebug(DebugLevel.I, "Hash type: {0} ({1})", HashType, HashType.ToString());
int ExpectedHashLength = GetExpectedHashLength(HashType);

// Verify the hash
bool actualLengthValid = ActualHash.Length == ExpectedHashLength;
bool expectedLengthValid = ExpectedHash.Length == ExpectedHashLength;
if (actualLengthValid && expectedLengthValid)
{
DebugWriter.WriteDebug(DebugLevel.I, "Hashes are consistent.");
DebugWriter.WriteDebug(DebugLevel.I, "Hashes {0} and {1}", ActualHash, ExpectedHash);
if (ActualHash == ExpectedHash)
{
DebugWriter.WriteDebug(DebugLevel.I, "Hashes are consistent.");
DebugWriter.WriteDebug(DebugLevel.I, "Hashes {0} and {1}", ActualHash, ExpectedHash);
if (ActualHash == ExpectedHash)
{
DebugWriter.WriteDebug(DebugLevel.I, "Hashes match.");
return true;
}
else
{
DebugWriter.WriteDebug(DebugLevel.W, "Hashes don't match.");
return false;
}
DebugWriter.WriteDebug(DebugLevel.I, "Hashes match.");
return true;
}
else
{
DebugWriter.WriteDebug(DebugLevel.E, "{0} ({1}) or {2} ({3}) is malformed. Check the algorithm ({4}). Expected length: {5}", ActualHash, ActualHash.Length, ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
throw new KernelException(KernelExceptionType.InvalidHash, "{0} ({1}) or {2} ({3}) is malformed. Check the algorithm ({4}). Expected length: {5}", ActualHash, ActualHash.Length, ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
DebugWriter.WriteDebug(DebugLevel.W, "Hashes don't match.");
return false;
}
}
else
{
throw new KernelException(KernelExceptionType.Encryption, "File {0} not found.", FileName);
if (actualLengthValid)
{
DebugWriter.WriteDebug(DebugLevel.E, "The actual hash {0} ({1}) is malformed. Check the algorithm ({2}). Expected length: {3}", ActualHash, ActualHash.Length, HashType, ExpectedHashLength);
throw new KernelException(KernelExceptionType.InvalidHash, "The actual hash {0} ({1}) is malformed. Check the algorithm ({2}). Expected length: {3}", ActualHash, ActualHash.Length, HashType, ExpectedHashLength);
}
else if (expectedLengthValid)
{
DebugWriter.WriteDebug(DebugLevel.E, "The expected hash {0} ({1}) is malformed. Check the algorithm ({2}). Expected length: {3}", ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
throw new KernelException(KernelExceptionType.InvalidHash, "The expected hash {0} ({1}) is malformed. Check the algorithm ({2}). Expected length: {3}", ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
}
else
{
DebugWriter.WriteDebug(DebugLevel.E, "Expected {0} ({1}) and actual {2} ({3}) are malformed. Check the algorithm ({4}). Expected length: {5}", ActualHash, ActualHash.Length, ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
throw new KernelException(KernelExceptionType.InvalidHash, "Expected {0} ({1}) and actual {2} ({3}) are malformed. Check the algorithm ({4}). Expected length: {5}", ActualHash, ActualHash.Length, ExpectedHash, ExpectedHash.Length, HashType, ExpectedHashLength);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion public/Nitrocid/Nitrocid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<NitrocidModAPIVersionMajor>3.0.25</NitrocidModAPIVersionMajor>

<!-- Increment NitrocidModAPIVersionChangeset every time there is a breaking change or an API addition in the N-KS API. -->
<NitrocidModAPIVersionChangeset>424</NitrocidModAPIVersionChangeset>
<NitrocidModAPIVersionChangeset>425</NitrocidModAPIVersionChangeset>

<!-- To be installed to the file version -->
<NitrocidModAPIVersion>$(NitrocidModAPIVersionMajor).$(NitrocidModAPIVersionChangeset)</NitrocidModAPIVersion>
Expand Down

0 comments on commit b4d3464

Please sign in to comment.