Skip to content

Commit 1006ebf

Browse files
committed
Add tests to project
1 parent 090b7e3 commit 1006ebf

8 files changed

Lines changed: 77 additions & 24 deletions

File tree

FMT.Hash.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio Version 17
33
VisualStudioVersion = 17.5.2.0
44
MinimumVisualStudioVersion = 10.0.40219.1
5-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FMT.Hash", "FMT.Hash.csproj", "{194D2104-87E7-B694-E3FC-A4B94C752228}"
5+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FMT.Hash", "Source\FMT.Hash.csproj", "{194D2104-87E7-B694-E3FC-A4B94C752228}"
6+
EndProject
7+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FMT.Hash.Tests", "Tests\FMT.Hash.Tests\FMT.Hash.Tests.csproj", "{0A23BB75-6C58-4F84-B26B-D995F1A3B170}"
68
EndProject
79
Global
810
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -14,6 +16,10 @@ Global
1416
{194D2104-87E7-B694-E3FC-A4B94C752228}.Debug|Any CPU.Build.0 = Debug|Any CPU
1517
{194D2104-87E7-B694-E3FC-A4B94C752228}.Release|Any CPU.ActiveCfg = Release|Any CPU
1618
{194D2104-87E7-B694-E3FC-A4B94C752228}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{0A23BB75-6C58-4F84-B26B-D995F1A3B170}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{0A23BB75-6C58-4F84-B26B-D995F1A3B170}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{0A23BB75-6C58-4F84-B26B-D995F1A3B170}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{0A23BB75-6C58-4F84-B26B-D995F1A3B170}.Release|Any CPU.Build.0 = Release|Any CPU
1723
EndGlobalSection
1824
GlobalSection(SolutionProperties) = preSolution
1925
HideSolutionNode = FALSE

FMT.Hash.csproj renamed to Source/FMT.Hash.csproj

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
1313
<!-- Allow to be packaged on Pack -->
1414
<IsPackable>true</IsPackable>
15-
<PackageVersion>$([System.DateTime]::Now.ToString("yyyy.MM.dd.HHmm"))</PackageVersion>
15+
<PackageVersion>$([System.DateTime]::Now.ToString("yyyy.MM.dd.HH001"))</PackageVersion>
1616
<PackageReadmeFile>README.md</PackageReadmeFile>
1717

1818
<PackageId>FMT.Hash</PackageId>
@@ -21,27 +21,24 @@
2121
<Company>Paulov / FMTDev</Company>
2222
<Copyright>Paulov / FMTDev / FMT 2019-2025</Copyright>
2323
<Description>A package designed for Frostbite Modding Tool. Useful for plugins and other tooling.</Description>
24-
<PackageProjectUrl>https://github.com/FMTDev/FMT.Releases</PackageProjectUrl>
25-
<RepositoryUrl>https://github.com/FMTDev/FMT.Releases</RepositoryUrl>
24+
<PackageProjectUrl>https://github.com/FMTDev/FMT.Hash</PackageProjectUrl>
25+
<RepositoryUrl>https://github.com/FMTDev/FMT.Hash</RepositoryUrl>
2626
<PackageTags>fmt; frostbite modding tool;</PackageTags>
2727
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
2828
<PackageIcon>FMTIcon24.png</PackageIcon>
29+
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
2930
</PropertyGroup>
3031

3132
<ItemGroup>
3233
<PackageReference Include="HashDepot" Version="3.1.0" />
34+
<PackageReference Include="System.Security.Cryptography.Algorithms" Version="4.3.1" />
3335
</ItemGroup>
3436

3537
<PropertyGroup Condition="'$(Configuration)'=='Release'">
3638
<DebugSymbols>False</DebugSymbols>
3739
<DebugType>None</DebugType>
3840
</PropertyGroup>
3941

40-
<ItemGroup>
41-
<None Include="README.md" Pack="true" PackagePath="\" />
42-
<None Include="LICENSE.md" Pack="true" PackagePath="\" />
43-
</ItemGroup>
44-
4542
<ItemGroup>
4643
<None Update="FMTIcon24.png">
4744
<PackagePath>\</PackagePath>
File renamed without changes.
File renamed without changes.
File renamed without changes.

Sha256.cs renamed to Source/Sha256.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,29 @@ public static uint SHA256Hash32_Lowercase_RT(string strVal, string strSeed = "53
2626
ArgumentNullException.ThrowIfNullOrEmpty(strVal);
2727
ArgumentNullException.ThrowIfNullOrEmpty(strSeed);
2828

29-
uint hashVal = 5381;
29+
var buf = new byte[512];
30+
31+
var sha256Context = SHA256.Create();
3032

3133
// Lower the input value;
3234
strVal = strVal.ToLower();
3335
// Lower the input value;
34-
strSeed = strVal.ToLower();
35-
36-
// Length is the strVal + 1 (null terminator)
37-
var lenVal = strVal.Length + 1;
36+
strSeed = strSeed.ToLower();
3837

39-
// Add the str into a new buffer with the new size (null terminator)
40-
var bufferOfStrVal = new byte[lenVal];
41-
for (var iBufSize = 0; iBufSize < lenVal; iBufSize++)
42-
bufferOfStrVal[iBufSize] = (byte)strVal[iBufSize];
38+
// Length is the strVal
39+
var lenVal = strVal.Length;
40+
buf = Resize(buf, lenVal + 1); // +1 for the \0 terminator. probably not actually required...
4341

44-
// Hash the buffer data once against the buf[0] size * lenVal
45-
var bytes = new byte[256];
46-
var r = SHA256.HashData(new MemoryStream(Encoding.UTF8.GetBytes(strVal)), new Span<byte>(bytes));
47-
var r2 = SHA256.HashData(bufferOfStrVal);
48-
hashVal = (uint)BitConverter.ToUInt64(r2, 0);
42+
// Hash the buffer data once against the buf
43+
buf = sha256Context.ComputeHash(buf, 0, 1 * lenVal);
4944

5045
// Hash the buffer data again against the buf[0] size * strSeed.Length
51-
// TODO: ^^^^^
46+
var lenSeed = strSeed.Length;
47+
buf = Resize(buf, lenSeed);
48+
buf = sha256Context.ComputeHash(buf, 0, 1 * lenSeed);
5249

5350
// Finalize the compute
51+
uint hashVal = (uint)BitConverter.ToUInt64(buf);
5452

5553
// Return the hash as a BigEndian hashed value + 28
5654
return ToBigEndian((hashVal + 28));
@@ -63,6 +61,15 @@ private static uint ToBigEndian(uint littleEndian)
6361
spanage.Reverse();
6462
return BitConverter.ToUInt32(spanage);
6563
}
64+
65+
private static byte[] Resize(byte[] bytes, int length)
66+
{
67+
var buf = new byte[length];
68+
for (var iBufSize = 0; iBufSize < length && iBufSize < bytes.Length; iBufSize++)
69+
buf[iBufSize] = bytes[iBufSize];
70+
71+
return buf;
72+
}
6673
//{
6774
// using fixed_string_buf = eastl::fixed_string<char, 512>;
6875
// fixed_string_buf buf;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>latest</LangVersion>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
10+
<PackageReference Include="MSTest" Version="3.6.4" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\Source\FMT.Hash.csproj" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace FMT.Hash.Tests
2+
{
3+
[TestClass]
4+
public sealed class Sha256Tests
5+
{
6+
7+
/// <summary>
8+
/// Test the Sha256 produces the same result as the true result found via SDK generation
9+
/// TODO: It currently doesn't. :)
10+
/// </summary>
11+
[TestMethod]
12+
13+
public void Test_SHA256Hash32_Lowercase_RT()
14+
{
15+
var attempt1 = Sha256.SHA256Hash32_Lowercase_RT("AttribSchema_gp_kickshot_shooting");
16+
var attempt2 = Sha256.SHA256Hash32_Lowercase_RT("AttribSchema_gp_kickshot_shooting", "1029");
17+
18+
Assert.AreEqual(1303288196u, attempt1);
19+
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)