Move ComputeDigest to HashHelper

This commit is contained in:
Michael VanOverbeek 2021-10-31 13:44:52 -04:00
commit a2b6908c07
3 changed files with 33 additions and 41 deletions

View file

@ -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<string> 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<byte>()
: 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;
}
}
}

View file

@ -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<string> 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<byte>()
: 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;
}
}
}

View file

@ -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);
}