-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eadda76
commit c5e583d
Showing
61 changed files
with
6,652 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,4 +29,4 @@ | |
<NuspecFile>Komponent.nuspec</NuspecFile> | ||
</PropertyGroup> | ||
|
||
</Project> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/lib/Kryptography.Contract/Enums/Checksum/Crc/Crc32Formula.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Oops, something went wrong.