Skip to content

Instantly share code, notes, and snippets.

@mjs3339
Created January 12, 2021 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjs3339/c773263870a65c85b928d32b80924947 to your computer and use it in GitHub Desktop.
Save mjs3339/c773263870a65c85b928d32b80924947 to your computer and use it in GitHub Desktop.
Variable 16Bit to 512Bit Hashing Algorithm
using System;
using System.Security.Cryptography;
[Serializable]
public class Sha16512 : HashAlgorithm
{
private int _bitWidth;
private SHA512Managed _hash = new SHA512Managed();
public Sha16512(int bitWidth)
{
if (bitWidth < 16 || bitWidth > 512)
throw new ArgumentException($"Bit Width {bitWidth} must be between 16 and 512.");
_bitWidth = bitWidth;
}
public override int HashSize => _bitWidth;
public override void Initialize()
{
}
protected override void HashCore(byte[] bytes, int ibStart, int cbSize)
{
var buf = bytes.SubByte(ibStart, cbSize);
HashValue = _hash.ComputeHash(buf).SubByte(0, _bitWidth >> 3);
}
protected override byte[] HashFinal()
{
return (byte[]) HashValue.Clone();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment