Skip to content

CalculateHash #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
name: Build & Test
on:
push:
branches: [ main, HashCalc ]
pull_request:
branches: [ main ]
jobs:
linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.100
- name: Install dependencies
run: dotnet restore Examples.sln
- name: Install Steam and Garry's Mod Dedicated Server
run: |
wget https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz
tar -xvzf steamcmd_linux.tar.gz
rm -rfv steamcmd_linux.tar.gz
./steamcmd.sh +login anonymous +force_install_dir gmod "+app_update 4020 -beta x86-64 validate" +quit
- name: Install GmodDotNet
run: |
wget https://gleb-krasilich.fra1.digitaloceanspaces.com/GmodNETStorage/storage/gmod-dot-net-linux.0.7.0-beta.2.30293992.master.tar.gz -O gmoddotnet.tar.gz
mkdir ./gmod/garrysmod/lua/bin
tar -xvzf gmoddotnet.tar.gz -C ./gmod/garrysmod/lua/bin
rm -rfv gmoddotnet.tar.gz
- name: Build Module
run: dotnet publish HashCalc/HashCalc.csproj --configuration Release --framework net5 -o ./gmod/garrysmod/lua/bin/Modules/HashCalc
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2
with:
name: HashCalc linux-x64
path: ./gmod/garrysmod/lua/bin/Modules/HashCalc/*
retention-days: 10
- name: Copy test.lua
run: cp .github/workflows/test.lua ./gmod/garrysmod/lua/autorun
- name: Run Garry's Mod
run: ./srcds_run_x64 -game garrysmod -systemtest -condebug +sv_hibernate_think 1 || true
working-directory: ./gmod/
timeout-minutes: 1
continue-on-error: true
- name: Print log
run: cat gmod/garrysmod/console.log
- name: Tests successfull?
uses: andstor/file-existence-action@v1
with:
files: "gmod/garrysmod/data/success.txt"
allow_failure: true
windows-build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET Core SDK
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.100
- name: Install dependencies
run: dotnet restore Examples.sln
- name: Install Steam and Garry's Mod Dedicated Server
shell: bash
run: |
curl https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip -O -L
powershell -Command 'Expand-Archive -LiteralPath ./steamcmd.zip -DestinationPath ./'
./steamcmd.exe +login anonymous +force_install_dir gmod "+app_update 4020 -beta x86-64 validate" +quit || true
- name: Install GmodDotNet
shell: bash
run: |
curl -o gmoddotnet.zip https://gleb-krasilich.fra1.digitaloceanspaces.com/GmodNETStorage/storage/gmod-dot-net-windows.0.7.0-beta.2.30293992.master.zip -O -L
mkdir ./gmod/garrysmod/lua/bin
powershell -Command 'Expand-Archive -LiteralPath ./gmoddotnet.zip -DestinationPath ./gmod/garrysmod/lua/bin'
- name: Build Module
run: dotnet publish HashCalc/HashCalc.csproj --configuration Release --framework net5 -o ./gmod/garrysmod/lua/bin/Modules/HashCalc
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2
with:
name: HashCalc win-x64
path: ./gmod/garrysmod/lua/bin/Modules/HashCalc/*
retention-days: 10
- name: Copy test.lua
run: cp .github/workflows/test.lua ./gmod/garrysmod/lua/autorun
- name: Run Garry's Mod
shell: bash
run: |
powershell -Command './gmod/srcds_win64.exe -console -systemtest -condebug -game "garrysmod" +exec "server.cfg" +gamemode sandbox +map gm_construct +maxplayers 16 +sv_hibernate_think 1'
powershell -Command 'Wait-Process -Name srcds_win64'
timeout-minutes: 1
continue-on-error: true
- name: Print log
shell: bash
run: cat gmod/garrysmod/console.log
- name: Tests successfull?
id: check_files
uses: andstor/file-existence-action@v1
with:
files: "gmod/garrysmod/data/success.txt"
allow_failure: true
macos-build:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.100
- name: Install dependencies
run: dotnet restore Examples.sln
- name: Build Module
run: dotnet publish Examples.sln --configuration Release --framework net5
- name: Upload a Build Artifact
uses: actions/upload-artifact@v2
with:
name: HashCalc osx-x64
path: HashCalc/bin/Release/net5/osx-x64/publish/*
retention-days: 10
41 changes: 41 additions & 0 deletions .github/workflows/test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
hook.Add("Tick", "Template_CloseServer", engine.CloseServer)
require("dotnet")

local function run_test()
local module_loaded = dotnet.load("HashCalc")
assert(module_loaded)

--------SHA256--------

local sha256 = CalculateHash("SHA256","test")
assert(sha256==string.upper("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"))

local sha256_unicode = CalculateHash("SHA256","♥")
assert(sha256_unicode==string.upper("7c546e1819d9562a67643820b4305c3892fd9a151628e447c8e8dae162fe8f57"))

---------MD5----------

local md5 = CalculateHash("MD5","test")
assert(md5==string.upper("098f6bcd4621d373cade4e832627b4f6"))

local md5_unicode = CalculateHash("MD5","♥")
assert(md5_unicode==string.upper("d7d4076e1ad9c734baad210480cc903a"))

--------SHA512--------

local sha512 = CalculateHash("SHA512","test")
assert(sha512==string.upper("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"))

local sha512_unicode = CalculateHash("SHA512","♥")
assert(sha512_unicode==string.upper("09de9680f8bfc115d42d36646ef28b4cfe3fbffab095bb2ac86c1be9d9ad164a67aa07227d13fea504acf320a68f5fc507c86b803942a7023ccb0e4d23b65f04"))

----------------------

local module_unloaded = dotnet.unload("HashCalc")
assert(module_unloaded)
end

run_test()

print("tests are successful!")
file.Write("success.txt", "done")
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vs
bin
obj
33 changes: 33 additions & 0 deletions Examples.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30803.129
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HashCalc", "HashCalc\HashCalc.csproj", "{5849F24D-B5D7-4A48-9469-021DEE4B8531}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{342AA3D8-AFF4-48DE-B97C-DE6E0CF35634}"
ProjectSection(SolutionItems) = preProject
.gitignore = .gitignore
.github\workflows\build.yml = .github\workflows\build.yml
README.md = README.md
.github\workflows\test.lua = .github\workflows\test.lua
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5849F24D-B5D7-4A48-9469-021DEE4B8531}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5849F24D-B5D7-4A48-9469-021DEE4B8531}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5849F24D-B5D7-4A48-9469-021DEE4B8531}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5849F24D-B5D7-4A48-9469-021DEE4B8531}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {27D856A3-D44D-42EB-AB15-26DF09A9E065}
EndGlobalSection
EndGlobal
64 changes: 64 additions & 0 deletions HashCalc/HashCalc.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using GmodNET.API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;

namespace NativeMath
{
public class HashCalc : IModule
{
public string ModuleName => "HashCalc";

public string ModuleVersion => "0.0.1";

private List<GCHandle> handles;

private static readonly string[] hashNames = new[]{
"SHA", "SHA1", "System.Security.Cryptography.SHA1", "System.Security.Cryptography.HashAlgorithm",
"MD5", "System.Security.Cryptography.MD5",
"SHA256", "SHA-256", "System.Security.Cryptography.SHA256",
"SHA384", "SHA-384", "System.Security.Cryptography.SHA384",
"SHA512", "SHA-512", "System.Security.Cryptography.SHA512"
};

private static int CalculateHash(ILua lua)
{
string hashName = lua.GetString(1);
string data = lua.GetString(2);
if (!hashNames.Contains(hashName)) throw new ArgumentException("invalid hash algorithm name");
HashAlgorithm hashAlgorithm = HashAlgorithm.Create(hashName);
byte[] hash = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(data));
int hashLength = hash.Length;
StringBuilder builder = new();
for (int i = 0; i < hashLength; i++)
{
builder.Append(hash[i].ToString("X2"));
}
lua.PushString(builder.ToString());
return 1;
}

public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context)
{
handles = new();

lua.PushSpecial(SPECIAL_TABLES.SPECIAL_GLOB);
GCHandle handle = lua.PushManagedFunction(CalculateHash);
handles.Add(handle);
lua.SetField(-2, "CalculateHash");
lua.Pop();
}

public void Unload(ILua lua)
{
foreach (var handle in handles)
{
handle.Free();
}
handles = null;
}
}
}
16 changes: 16 additions & 0 deletions HashCalc/HashCalc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net5</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="GmodNET.API" Version="0.7.0-*" />
</ItemGroup>
</Project>
8 changes: 8 additions & 0 deletions HashCalc/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
<add key="gmodnet-packages" value="https://pkgs.dev.azure.com/GmodNET/gmodnet-artifacts/_packaging/gmodnet-packages/nuget/v3/index.json" />
</packageSources>
</configuration>