1

In .NET Framework if we needed to create our own crypto algorithm we could create our own class like:

public class MyAlgorithm: System.Security.Cryptography.HashAlgorithm
{
}

But in .NET Core it seems very limited because of

public abstract class HashAlgorithm : IDisposable
    {     

    protected HashAlgorithm();    
    public virtual int HashSize { get; }    
    public byte[] ComputeHash(byte[] buffer);
    public byte[] ComputeHash(byte[] buffer, int offset, int count);
    public byte[] ComputeHash(Stream inputStream);
    public void Dispose();
    public abstract void Initialize();
    protected virtual void Dispose(bool disposing);
    protected abstract void HashCore(byte[] array, int ibStart, int cbSize);
    protected abstract byte[] HashFinal();
}

It doesn't have such things as HashSizeValue or State.

Should we still use HashAlgorithm as base class for own algorithms in .NET Core?

7
  • our own crypto algorithm You are creating a hash algorithm. It is different. Commented Apr 12, 2017 at 7:36
  • If you are building a hash algorithm from scratch, what use do you have for HashSizeValue (that is an unexposed property)? you are declaring what is the hash size of your algorithm, through the HashSize property. And for State... If you need a State, you keep the state. Commented Apr 12, 2017 at 7:40
  • @xanatos What I am actually doing I am porting the code from .NET Framework to .NET Core. Crypto algorithms there are using HashAlgorithm as base class. This is why I am asking Commented Apr 12, 2017 at 7:42
  • What is the correct way in .NET Core? Usage of some base class or no class? Commented Apr 12, 2017 at 7:43
  • 1
    The correct way is deriving from HashAlgorithm. Note that they (Microsoft) readded the two fields in November (github.com/dotnet/corefx/commit/…) and in a previous version. If you need them you'll have to readd them. State is used by TransformBlock/TransformFinalBlock that the .NET Core 1.1 HashAlgorithm doesn't implement. Commented Apr 12, 2017 at 7:55

1 Answer 1

2

In your class, implement a

public override int HashSize => 256;

(or the value you have).

If you are using HashSizeValue, change it to HashSize.

If you are using State, readd it as a protected int State. In the "full" HashAlgorithm it is used by two public methods (TransformBlock and TransformFinalBlock). If you need it (and you are using TransformBlock and TransformFinalBlock) then copy it from a newer version of .NET Core HashAlgorithm (you can find it on github). If you were only checking it then you don't need it and you can comment it (because only two missing methods will be writing it).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.