BouncyCastle.NetCore : Impossible to find the CryptoApiRandomGenerator class

Viewed 1992

I've installed the BouncyCastle.NetCore package in order to create a self signed certificate but i couldn't found the CryptoApiRandomGenerator class.

Is it normal? if yes what is the substitute?

2 Answers

You can implement your own CryptoApiRandomGenerator. Something like this:

public class MyCryptoApiRandomGenerator
    : IRandomGenerator
{
    private readonly RNGCryptoServiceProvider rndProv;

    public NetMatchCryptoApiRandomGenerator()
    {
        rndProv = new RNGCryptoServiceProvider();
    }

    #region IRandomGenerator Members

    public virtual void AddSeedMaterial(byte[] seed)
    {
        // I don't care about the seed
    }

    public virtual void AddSeedMaterial(long seed)
    {
        // I don't care about the seed
    }

    public virtual void NextBytes(byte[] bytes)
    {
        rndProv.GetBytes(bytes);
    }

    public virtual void NextBytes(byte[] bytes, int start, int len)
    {
        if (start < 0)
            throw new ArgumentException("Start offset cannot be negative", "start");
        if (bytes.Length < (start + len))
            throw new ArgumentException("Byte array too small for requested offset and length");

        if (bytes.Length == len && start == 0)
        {
            NextBytes(bytes);
        }
        else
        {
            byte[] tmpBuf = new byte[len];
            rndProv.GetBytes(tmpBuf);
            Array.Copy(tmpBuf, 0, bytes, start, len);
        }
    }

    #endregion
}

There isn't an implicit implementation for this in .Net Core at the moment.

Andrei Ardel has right if you decompile CryptoApiRandomGenerator class with "reflector" or "just decompile" you find the implementation that you can add on your project :

public class CryptoApiRandomGenerator : IRandomGenerator
    {
        private readonly RandomNumberGenerator rndProv;

        public CryptoApiRandomGenerator() : this(new RNGCryptoServiceProvider())
        {
        }

        public CryptoApiRandomGenerator(RandomNumberGenerator rng)
        {
            this.rndProv = rng;
        }

        public virtual void AddSeedMaterial(byte[] seed)
        {
        }

        public virtual void AddSeedMaterial(long seed)
        {
        }

        public virtual void NextBytes(byte[] bytes)
        {
            this.rndProv.GetBytes(bytes);
        }

        public virtual void NextBytes(byte[] bytes, int start, int len)
        {
            if (start < 0)
            {
                throw new ArgumentException("Start offset cannot be negative", "start");
            }
            if ((int)bytes.Length < start + len)
            {
                throw new ArgumentException("Byte array too small for requested offset and length");
            }
            if ((int)bytes.Length == len && start == 0)
            {
                this.NextBytes(bytes);
                return;
            }
            byte[] numArray = new byte[len];
            this.NextBytes(numArray);
            Array.Copy(numArray, 0, bytes, start, len);
        }
    }
Related