Skip to content

Commit b167014

Browse files
(WebCrypto / HTTPServer) : Use stream size for secure checksum + Endianess fix.
(WebCrypto / HTTPServer) : Use stream size for secure checksum + Endianess fix.
1 parent 172ec24 commit b167014

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

BackendServices/CyberBackendLibrary/Crypto/WebCrypto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ public static class WebCrypto
405405
.InitiateCTRBuffer(Convert.FromBase64String(Encoding.UTF8.GetString(ByteArrayToDecrypt).Replace("<Secure>", string.Empty).Replace("</Secure>", string.Empty)), Convert.FromBase64String(AccessKey), IV);
406406
}
407407

408-
public static string? ProcessSecureCheckum(byte[] inputData, uint initialValue)
408+
public static string? ProcessSecureCheckum(byte[] inputData, long initialValue)
409409
{
410410
byte currentByte = 0;
411411
int DataIndex = 0;
@@ -416,7 +416,7 @@ public static class WebCrypto
416416

417417
do
418418
{
419-
currentByte = (byte)(inputData[DataIndex] ^ BitConverter.SingleToInt32Bits(tempFloat));
419+
currentByte = (byte)(inputData[DataIndex] ^ BitConverter.SingleToInt32Bits(!BitConverter.IsLittleEndian ? EndianTools.EndianUtils.EndianSwap(tempFloat) : tempFloat));
420420
CheckSumBytes[DataIndex] = currentByte;
421421
tempFloat = (ChecksumFloatBox[currentByte]) /
422422
(float)(((uint)(ChecksumByteBox[(uint)tempFloat & 0xFF]) << 0x10) |

WebServers/HTTPServer/HttpProcessor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,8 @@ private static void WriteResponse(Stream stream, HttpRequest? request, HttpRespo
11061106
{
11071107
string EtagMD5 = ComputeStreamChecksum(response.ContentStream);
11081108

1109+
response.ContentStream.Position = 0;
1110+
11091111
if (!string.IsNullOrEmpty(request.Method) && request.Method == "OPTIONS")
11101112
{
11111113
response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
@@ -1704,6 +1706,8 @@ private static void Handle_LocalFile_Stream(Stream stream, HttpRequest request,
17041706
{
17051707
string EtagMD5 = ComputeStreamChecksum(response.ContentStream);
17061708

1709+
response.ContentStream.Position = 0;
1710+
17071711
if (request.Headers.TryGetValue("If-None-Match", out string? value1) && value1.Equals(EtagMD5))
17081712
{
17091713
response.Headers.Clear();
@@ -1977,13 +1981,11 @@ private static string ComputeStreamChecksum(Stream input)
19771981

19781982
byte[] bytes = MD5.Create().ComputeHash(input);
19791983

1980-
input.Position = 0;
1981-
19821984
StringBuilder builder = new();
19831985
for (int i = 0; i < bytes.Length; i++)
19841986
builder.Append(bytes[i].ToString("x2"));
19851987

1986-
return builder.ToString() + $":{WebCrypto.ProcessSecureCheckum(bytes, 0x5BCD9F0F)}";
1988+
return builder.ToString() + $":{WebCrypto.ProcessSecureCheckum(bytes, input.Length)}";
19871989
}
19881990

19891991
#if NET7_0_OR_GREATER

0 commit comments

Comments
 (0)