Skip to content

Commit

Permalink
Refactor Kryptography to 2.1.0;
Browse files Browse the repository at this point in the history
  • Loading branch information
onepiecefreak3 committed Jan 7, 2025
1 parent eadda76 commit c5e583d
Show file tree
Hide file tree
Showing 61 changed files with 6,652 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/lib/Kanvas/Kanvas.Debug.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<group targetFramework="net8.0">
<dependency id="BCnEncoder.Net" version="2.1.0" />
<dependency id="SixLabors.ImageSharp" version="3.1.6" />
<dependency id="Komponent" version="2.1.0" />
<dependency id="Komponent" version="2.1.1" />
</group>
</dependencies>
</metadata>
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Kanvas/Kanvas.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@

<ItemGroup>
<PackageReference Include="BCnEncoder.Net" Version="2.1.0" />
<PackageReference Include="Komponent" Version="2.1.0" />
<PackageReference Include="Komponent" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kanvas.Contract\Kanvas.Contract.csproj" />
</ItemGroup>

</Project>
</Project>
2 changes: 1 addition & 1 deletion src/lib/Kanvas/Kanvas.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<group targetFramework="net8.0">
<dependency id="BCnEncoder.Net" version="2.1.0" />
<dependency id="SixLabors.ImageSharp" version="3.1.6" />
<dependency id="Komponent" version="2.1.0" />
<dependency id="Komponent" version="2.1.1" />
</group>
</dependencies>
</metadata>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Komponent/Komponent.Debug.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Komponent</id>
<version>2.1.1</version>
<version>2.1.0</version>
<description>A library containing all common tools like generic binary reading and writing.</description>

<authors>onepiecefreak;IcySon55</authors>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Komponent/Komponent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
<NuspecFile>Komponent.nuspec</NuspecFile>
</PropertyGroup>

</Project>
</Project>
2 changes: 1 addition & 1 deletion src/lib/Komponent/Komponent.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Komponent</id>
<version>2.1.1</version>
<version>2.1.0</version>
<description>A library containing all common tools like generic binary reading and writing.</description>

<authors>onepiecefreak;IcySon55</authors>
Expand Down
3 changes: 2 additions & 1 deletion src/lib/Kompression/Kompression.Debug.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<group targetFramework="net8.0">
<dependency id="K4os.Compression.LZ4.Streams" version="1.3.8" />
<dependency id="SharpZipLib" version="1.4.2" />
<dependency id="Komponent" version="2.1.1" />
</group>
</dependencies>
</metadata>
Expand All @@ -28,4 +29,4 @@
<file src="bin\Debug\net8.0\*.dll" target="lib\net8.0\"/>
<file src="..\..\..\kuriimu2icon.png" target="kuriimu2icon.png"/>
</files>
</package>
</package>
1 change: 1 addition & 0 deletions src/lib/Kompression/Kompression.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<group targetFramework="net8.0">
<dependency id="K4os.Compression.LZ4.Streams" version="1.3.8" />
<dependency id="SharpZipLib" version="1.4.2" />
<dependency id="Komponent" version="2.1.1" />
</group>
</dependencies>
</metadata>
Expand Down
39 changes: 39 additions & 0 deletions src/lib/Kryptography.Contract/Checksum/IChecksum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Text;

namespace Kryptography.Contract.Checksum
{
/// <summary>
/// Exposes methods to compute a hash over given input data.
/// </summary>
public interface IChecksum
{
/// <summary>
/// Computes a hash from a string encoded in ASCII.
/// </summary>
/// <param name="input">The string to compute the hash to.</param>
/// <returns>The computed hash.</returns>
byte[] Compute(string input);

/// <summary>
/// Computes a hash from a string.
/// </summary>
/// <param name="input">The string to compute the hash to.</param>
/// <param name="enc">The encoding the string should be encoded in.</param>
/// <returns>The computed hash.</returns>
byte[] Compute(string input, Encoding enc);

/// <summary>
/// Computes a hash over a stream of data.
/// </summary>
/// <param name="input">The stream of data to hash.</param>
/// <returns>The computed hash.</returns>
byte[] Compute(Stream input);

/// <summary>
/// Computes a hash over an array of bytes.
/// </summary>
/// <param name="input">The array of bytes to hash.</param>
/// <returns>The computed hash.</returns>
byte[] Compute(Span<byte> input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Kryptography.Contract.Enums.Checksum.Crc
{
public enum Crc32Formula
{
Normal,
Reflected
}
}
9 changes: 9 additions & 0 deletions src/lib/Kryptography.Contract/Kryptography.Contract.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
28 changes: 28 additions & 0 deletions src/lib/Kryptography.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35514.174 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kryptography", "Kryptography\Kryptography.csproj", "{144DD63E-E5AB-463C-BB48-DA43E30E712F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kryptography.Contract", "Kryptography.Contract\Kryptography.Contract.csproj", "{D58B260C-3CC7-4430-A43D-2564340E58E1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{144DD63E-E5AB-463C-BB48-DA43E30E712F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{144DD63E-E5AB-463C-BB48-DA43E30E712F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{144DD63E-E5AB-463C-BB48-DA43E30E712F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{144DD63E-E5AB-463C-BB48-DA43E30E712F}.Release|Any CPU.Build.0 = Release|Any CPU
{D58B260C-3CC7-4430-A43D-2564340E58E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D58B260C-3CC7-4430-A43D-2564340E58E1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D58B260C-3CC7-4430-A43D-2564340E58E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D58B260C-3CC7-4430-A43D-2564340E58E1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
115 changes: 115 additions & 0 deletions src/lib/Kryptography/Checksum/Checksum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System.Text;
using Kryptography.Contract.Checksum;

namespace Kryptography.Checksum
{
public abstract class Checksum<T> : IChecksum
{
/// <inheritdoc cref="Compute(string)"/>
public byte[] Compute(string input)
{
return Compute(input, Encoding.ASCII);
}

/// <inheritdoc cref="Compute(string,Encoding)"/>
public byte[] Compute(string input, Encoding enc)
{
return Compute(enc.GetBytes(input));
}

/// <inheritdoc cref="Compute(Span{byte})"/>
public byte[] Compute(Span<byte> input)
{
return ConvertResult(ComputeValue(input));
}

/// <inheritdoc cref="Compute(Stream)"/>
public byte[] Compute(Stream input)
{
return ConvertResult(ComputeValue(input));
}

/// <summary>
/// Computes a hash from a string encoded in ASCII.
/// </summary>
/// <param name="input">The string to compute the hash to.</param>
/// <returns>The computed hash.</returns>
public T ComputeValue(string input)
{
return ComputeValue(input, Encoding.ASCII);
}

/// <summary>
/// Computes a hash from a string.
/// </summary>
/// <param name="input">The string to compute the hash to.</param>
/// <param name="enc">The encoding the string should be encoded in.</param>
/// <returns>The computed hash.</returns>
public T ComputeValue(string input, Encoding enc)
{
return ComputeValue(enc.GetBytes(input));
}

/// <summary>
/// Computes a hash over a stream of data.
/// </summary>
/// <param name="input">The stream of data to hash.</param>
/// <returns>The computed hash.</returns>
public T ComputeValue(Span<byte> input)
{
var result = CreateInitialValue();
ComputeInternal(input, ref result);

FinalizeResult(ref result);
return result;
}

/// <summary>
/// Computes a hash over an array of bytes.
/// </summary>
/// <param name="input">The array of bytes to hash.</param>
/// <returns>The computed hash.</returns>
public T ComputeValue(Stream input)
{
var result = CreateInitialValue();

var buffer = new byte[4096];
int readSize;
do
{
readSize = input.Read(buffer);
ComputeInternal(buffer, ref result);
} while (readSize > 0);

FinalizeResult(ref result);

return result;
}

/// <summary>
/// Creates the start value of the hash computation of type <typeparamref name="T"/>.
/// </summary>
/// <returns>The initial value for the hash computation.</returns>
protected abstract T CreateInitialValue();

/// <summary>
/// Applies operations on the computed hash after all input data was consumed.
/// </summary>
/// <param name="result">The computed hash after all data was consumed.</param>
protected abstract void FinalizeResult(ref T result);

/// <summary>
/// Computes the hash on a given span of data. This method may be called multiple times and may be handled as an accumulative operation.
/// </summary>
/// <param name="input">The data to consume.</param>
/// <param name="result">The value to hold the computed hash.</param>
protected abstract void ComputeInternal(Span<byte> input, ref T result);

/// <summary>
/// Converts the value of type <typeparamref name="T"/> to a byte array.
/// </summary>
/// <param name="result">The finalized computed hash.</param>
/// <returns>The byte array representing the finalized computed hash.</returns>
protected abstract byte[] ConvertResult(T result);
}
}
58 changes: 58 additions & 0 deletions src/lib/Kryptography/Checksum/Crc/Crc16.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Buffers.Binary;

// https://stackoverflow.com/questions/10564491/function-to-calculate-a-crc16-checksum
// Online tool to check implementation: https://crccalc.com
// This tool seems to utilize a different approach to applying the polynomial.
// There are 2 ways a polynomial can be read and applied, read from LSB to MSB, or vice versa
// Therefore, depending on the implementation, e.g X25 can have a valid polynomial of 0x8404 or 0x1021
// If the polynomials of any CRC16 implementation from the link above is used, its bits have to be reversed, to work properly with this algorithm.
namespace Kryptography.Checksum.Crc
{
public class Crc16 : Checksum<ushort>
{
// https://crccalc.com
public static Crc16 X25 => new Crc16(0x8408, 0xFFFF, 0xFFFF);
public static Crc16 ModBus => new Crc16(0xA001, 0xFFFF, 0x0000);

private readonly ushort _polynomial;
private readonly ushort _initial;
private readonly ushort _xorOut;

private Crc16(ushort polynomial, ushort initial, ushort xorOut)
{
_polynomial = polynomial;
_initial = initial;
_xorOut = xorOut;
}

protected override ushort CreateInitialValue()
{
return _initial;
}

protected override void FinalizeResult(ref ushort result)
{
result ^= _xorOut;
}

protected override void ComputeInternal(Span<byte> input, ref ushort result)
{
foreach (var value in input)
{
result ^= value;
for (var k = 0; k < 8; k++)
result = (result & 1) > 0 ?
(ushort)(result >> 1 ^ _polynomial) :
(ushort)(result >> 1);
}
}

protected override byte[] ConvertResult(ushort result)
{
var buffer = new byte[2];
BinaryPrimitives.WriteUInt16BigEndian(buffer, result);

return buffer;
}
}
}
Loading

0 comments on commit c5e583d

Please sign in to comment.