Skip to content

Commit

Permalink
Added documentation.
Browse files Browse the repository at this point in the history
Fixed the internal modifiers on the hashes.
  • Loading branch information
DJGosnell committed Mar 15, 2021
1 parent dbf65f9 commit b1aa46a
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/DtronixHash/DtronixHash.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<VersionPrefix>0.1.0</VersionPrefix>
<VersionPrefix>0.1.1</VersionPrefix>
<Description>DtronixHash contains hashing algorithms utilizing modern .NET methodologies for simple and efficient usage.</Description>
<PackageOutputPath>$(ProjectDir)..\..\artifacts\</PackageOutputPath>
<Company>Dtronix</Company>
Expand Down
18 changes: 18 additions & 0 deletions src/DtronixHash/MurMur3/MurMur3Hash128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,48 @@

namespace DtronixHash.MurMur3
{
/// <inheritdoc />
public abstract class MurMur3Hash128 : NcHashAlgorithm
{
/// <summary>
/// Seed of this instance.
/// </summary>
public uint Seed { get; }

/// <summary>
/// Length of the read data.
/// </summary>
public int Length { get; private set; }

/// <inheritdoc />
public override int HashSize => 128;

/// <inheritdoc />
public override int InputBlockSize => 128;

/// <inheritdoc />
public override int OutputBlockSize => 128;

/// <inheritdoc />
protected MurMur3Hash128(uint seed)
{
Seed = seed;
}

/// <inheritdoc />
public override void Initialize()
{
Length = 0;
}

/// <inheritdoc />
public override void TransformBlock(ReadOnlyMemory<byte> memory)
{
// store the length of the hash (for use later)
Length += memory.Length;
}

/// <inheritdoc />
public override Memory<byte> ComputeHash(Memory<byte> block)
{
Initialize();
Expand Down
2 changes: 1 addition & 1 deletion src/DtronixHash/MurMur3/MurMur3Hash128X64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MurMur3Hash128X64 : MurMur3Hash128



internal MurMur3Hash128X64(uint seed = 0)
public MurMur3Hash128X64(uint seed = 0)
: base(seed)
{
Reset();
Expand Down
2 changes: 1 addition & 1 deletion src/DtronixHash/MurMur3/MurMur3Hash128X86.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class MurMur3Hash128X86 : MurMur3Hash128
const uint C2 = 0xab0e9789;
const uint C3 = 0x38b34ae5;
const uint C4 = 0xa1e38b93;
internal MurMur3Hash128X86(uint seed = 0)
public MurMur3Hash128X86(uint seed = 0)
: base(seed)
{
Reset();
Expand Down
31 changes: 31 additions & 0 deletions src/DtronixHash/NcHashAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,43 @@ namespace DtronixHash
/// </summary>
public abstract class NcHashAlgorithm
{
/// <summary>
/// Size of the hash in bits.
/// </summary>
public abstract int HashSize { get; }

/// <summary>
/// Size of the full input blocks.
/// </summary>
public abstract int InputBlockSize { get; }

/// <summary>
/// Size of the hash result.
/// </summary>
public abstract int OutputBlockSize { get; }

/// <summary>
/// Resets the hash to its initial state.
/// </summary>
public abstract void Initialize();

/// <summary>
/// Transforms the passed block of data into the hash.
/// </summary>
/// <param name="block">Data to hash.</param>
public abstract void TransformBlock(ReadOnlyMemory<byte> block);

/// <summary>
/// Completes the input transformation and returns the block's hash.
/// </summary>
/// <returns>Hash of data.</returns>
public abstract Memory<byte> FinalizeHash();

/// <summary>
/// Hashes the entire passed block and finalizes the output.
/// </summary>
/// <param name="block">Complete data to hash.</param>
/// <returns>Hash of passed data.</returns>
public abstract Memory<byte> ComputeHash(Memory<byte> block);
}
}
23 changes: 20 additions & 3 deletions src/DtronixHash/NcHashBuffer.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;
using DtronixHash.MurMur3;

namespace DtronixHash
{
/// <summary>
/// Non Cryptographic Hash Buffer.
/// </summary>
public class NcHashBuffer
{
private readonly NcHashAlgorithm _algorithm;
private readonly Memory<byte> _buffer;
private int _bufferPosition;
private readonly int _blockSizeBytes;

/// <summary>
/// Algorithm used by the buffer.
/// </summary>
public NcHashAlgorithm Algorithm => _algorithm;

/// <summary>
/// Creates a new instance of the Non Cryptographic Hash Buffer.
/// </summary>
/// <param name="algorithm">Algorithm to use while hashing.</param>
public NcHashBuffer(NcHashAlgorithm algorithm)
{
_algorithm = algorithm;
_blockSizeBytes = algorithm.InputBlockSize / 8;
_buffer = new Memory<byte>(new byte[_blockSizeBytes]);
}

/// <summary>
/// Writes data to the managed hash algorithm.
/// </summary>
/// <param name="data">Data to hash.</param>
public void Write(ReadOnlyMemory<byte> data)
{
var remainder = data.Length & (_blockSizeBytes - 1);
Expand Down Expand Up @@ -75,6 +88,10 @@ public void Write(ReadOnlyMemory<byte> data)
}
}

/// <summary>
/// Flushes any buffered data and returns the final hash result.
/// </summary>
/// <returns>Hash bytes</returns>
public Memory<byte> FinalizeHash()
{
if(_bufferPosition > 0)
Expand Down

0 comments on commit b1aa46a

Please sign in to comment.