mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-09 05:18:47 +00:00
Move ComputeDigest to HashHelper
This commit is contained in:
parent
dc92cc0319
commit
a2b6908c07
3 changed files with 33 additions and 41 deletions
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,8 +1,10 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.IO;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.Helpers {
|
namespace LBPUnion.ProjectLighthouse.Helpers {
|
||||||
[SuppressMessage("ReSharper", "UnusedMember.Global")]
|
[SuppressMessage("ReSharper", "UnusedMember.Global")]
|
||||||
|
@ -45,5 +47,33 @@ namespace LBPUnion.ProjectLighthouse.Helpers {
|
||||||
|
|
||||||
return BCryptHash(Sha256Hash(bytes));
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,6 +5,7 @@ using System.Reflection.Metadata.Ecma335;
|
||||||
using System.Runtime.InteropServices.ComTypes;
|
using System.Runtime.InteropServices.ComTypes;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Kettu;
|
using Kettu;
|
||||||
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
using LBPUnion.ProjectLighthouse.Logging;
|
using LBPUnion.ProjectLighthouse.Logging;
|
||||||
using LBPUnion.ProjectLighthouse.Serialization;
|
using LBPUnion.ProjectLighthouse.Serialization;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
@ -75,7 +76,7 @@ namespace LBPUnion.ProjectLighthouse
|
||||||
var digestPath = context.Request.Path;
|
var digestPath = context.Request.Path;
|
||||||
var body = context.Request.Body;
|
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.
|
// 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))
|
if (context.Request.Headers.TryGetValue("X-Digest-A", out var sentDigest))
|
||||||
|
@ -104,7 +105,7 @@ namespace LBPUnion.ProjectLighthouse
|
||||||
responseBuffer.Position = 0;
|
responseBuffer.Position = 0;
|
||||||
|
|
||||||
// Compute the digest for the response.
|
// 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);
|
responseBuffer, serverDigestKey);
|
||||||
context.Response.Headers.Add("X-Digest-A", serverDigest);
|
context.Response.Headers.Add("X-Digest-A", serverDigest);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue