From a2b6908c07d1857839d4bb4a07956220da90b982 Mon Sep 17 00:00:00 2001 From: Michael VanOverbeek Date: Sun, 31 Oct 2021 13:44:52 -0400 Subject: [PATCH] Move ComputeDigest to HashHelper --- ProjectLighthouse/DigestUtils.cs | 39 ------------------------- ProjectLighthouse/Helpers/HashHelper.cs | 30 +++++++++++++++++++ ProjectLighthouse/Startup.cs | 5 ++-- 3 files changed, 33 insertions(+), 41 deletions(-) delete mode 100644 ProjectLighthouse/DigestUtils.cs diff --git a/ProjectLighthouse/DigestUtils.cs b/ProjectLighthouse/DigestUtils.cs deleted file mode 100644 index 392054d2..00000000 --- a/ProjectLighthouse/DigestUtils.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.IO; -using System.Security.Cryptography; -using System.Text; -using System.Threading.Tasks; - -namespace LBPUnion.ProjectLighthouse -{ - public static class DigestUtils - { - public static async Task ComputeDigest(string path, string authCookie, Stream body, - string digestKey) - { - var memoryStream = new MemoryStream(); - - var pathBytes = Encoding.UTF8.GetBytes(path); - var cookieBytes = string.IsNullOrEmpty(authCookie) - ? Array.Empty() - : Encoding.UTF8.GetBytes(authCookie); - var keyBytes = Encoding.UTF8.GetBytes(digestKey); - - await body.CopyToAsync(memoryStream); - - var bodyBytes = memoryStream.ToArray(); - - using var sha1 = IncrementalHash.CreateHash(HashAlgorithmName.SHA1); - sha1.AppendData(bodyBytes); - if (cookieBytes.Length > 0) - sha1.AppendData(cookieBytes); - sha1.AppendData(pathBytes); - sha1.AppendData(keyBytes); - - var digestBytes = sha1.GetHashAndReset(); - var digestString = Convert.ToHexString(digestBytes).ToLower(); - - return digestString; - } - } -} \ No newline at end of file diff --git a/ProjectLighthouse/Helpers/HashHelper.cs b/ProjectLighthouse/Helpers/HashHelper.cs index b2ea531e..72c7724a 100644 --- a/ProjectLighthouse/Helpers/HashHelper.cs +++ b/ProjectLighthouse/Helpers/HashHelper.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Security.Cryptography; using System.Text; +using System.Threading.Tasks; namespace LBPUnion.ProjectLighthouse.Helpers { [SuppressMessage("ReSharper", "UnusedMember.Global")] @@ -45,5 +47,33 @@ namespace LBPUnion.ProjectLighthouse.Helpers { return BCryptHash(Sha256Hash(bytes)); } + + public static async Task ComputeDigest(string path, string authCookie, Stream body, + string digestKey) + { + var memoryStream = new MemoryStream(); + + var pathBytes = Encoding.UTF8.GetBytes(path); + var cookieBytes = string.IsNullOrEmpty(authCookie) + ? Array.Empty() + : Encoding.UTF8.GetBytes(authCookie); + var keyBytes = Encoding.UTF8.GetBytes(digestKey); + + await body.CopyToAsync(memoryStream); + + var bodyBytes = memoryStream.ToArray(); + + using var sha1 = IncrementalHash.CreateHash(HashAlgorithmName.SHA1); + sha1.AppendData(bodyBytes); + if (cookieBytes.Length > 0) + sha1.AppendData(cookieBytes); + sha1.AppendData(pathBytes); + sha1.AppendData(keyBytes); + + var digestBytes = sha1.GetHashAndReset(); + var digestString = Convert.ToHexString(digestBytes).ToLower(); + + return digestString; + } } } \ No newline at end of file diff --git a/ProjectLighthouse/Startup.cs b/ProjectLighthouse/Startup.cs index d3210b67..9ab07443 100644 --- a/ProjectLighthouse/Startup.cs +++ b/ProjectLighthouse/Startup.cs @@ -5,6 +5,7 @@ using System.Reflection.Metadata.Ecma335; using System.Runtime.InteropServices.ComTypes; using System.Threading.Tasks; using Kettu; +using LBPUnion.ProjectLighthouse.Helpers; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Serialization; using Microsoft.AspNetCore.Builder; @@ -75,7 +76,7 @@ namespace LBPUnion.ProjectLighthouse var digestPath = context.Request.Path; var body = context.Request.Body; - var clientRequestDigest = await DigestUtils.ComputeDigest(digestPath, authCookie, body, serverDigestKey); + var clientRequestDigest = await HashHelper.ComputeDigest(digestPath, authCookie, body, serverDigestKey); // Check the digest we've just calculated against the X-Digest-A header if the game set the header. They should match. if (context.Request.Headers.TryGetValue("X-Digest-A", out var sentDigest)) @@ -104,7 +105,7 @@ namespace LBPUnion.ProjectLighthouse responseBuffer.Position = 0; // Compute the digest for the response. - var serverDigest = await DigestUtils.ComputeDigest(context.Request.Path, authCookie, + var serverDigest = await HashHelper.ComputeDigest(context.Request.Path, authCookie, responseBuffer, serverDigestKey); context.Response.Headers.Add("X-Digest-A", serverDigest); }