简体   繁体   English

源自.NET Core中的HashAlgorithm

[英]Deriving from HashAlgorithm in .NET Core

In .NET Framework if we needed to create our own crypto algorithm we could create our own class like: 在.NET Framework中,如果需要创建自己的加密算法,则可以创建自己的类,例如:

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

But in .NET Core it seems very limited because of 但是在.NET Core中,由于

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 . 它没有HashSizeValueState类的东西。

Should we still use HashAlgorithm as base class for own algorithms in .NET Core? 我们是否应该仍将HashAlgorithm用作.NET Core中自己算法的基类?

In your class, implement a 在课堂上,实施一个

public override int HashSize => 256;

(or the value you have). (或您拥有的价值)。

If you are using HashSizeValue , change it to HashSize . 如果使用的是HashSizeValue ,请将其更改为HashSize

If you are using State , readd it as a protected int State . 如果您使用State ,则将其读取为protected int State In the "full" HashAlgorithm it is used by two public methods ( TransformBlock and TransformFinalBlock ). 在“完整” HashAlgorithm ,有两个公共方法( TransformBlockTransformFinalBlock )使用它。 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 ). 如果需要它(并且您正在使用TransformBlockTransformFinalBlock ),则可以从.NET Core HashAlgorithm的较新版本中复制它(可以在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). 如果只检查它,则不需要它,并且可以对其进行注释(因为只有两个缺少的方法正在编写它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM