Esempio n. 1
0
        /// <summary>
        /// Fill an array with pseudo random bytes
        /// </summary>
        ///
        /// <param name="Size">Size of requested byte array</param>
        ///
        /// <returns>A pseudo random byte array</returns>
        public byte[] GetBytes(int Size)
        {
            byte[] data = new byte[Size];

            m_rndGenerator.Generate(data);

            return(data);
        }
Esempio n. 2
0
        /// <summary>
        /// Reset the SP20Prng instance
        /// </summary>
        public void Reset()
        {
            if (m_seedGenerator != null)
            {
                m_seedGenerator.Dispose();
                m_seedGenerator = null;
            }
            if (m_rngGenerator != null)
            {
                m_rngGenerator.Dispose();
                m_rngGenerator = null;
            }

            m_seedGenerator = GetSeedGenerator(m_seedType);
            m_rngGenerator  = new SBG(m_rndCount);

            if (m_seedGenerator != null)
            {
                m_rngGenerator.Initialize(m_seedGenerator.GetBytes(m_keySize));
            }
            else
            {
                m_rngGenerator.Initialize(m_stateSeed);
            }

            m_rngGenerator.Generate(m_byteBuffer);
            m_bufferIndex = 0;
        }
Esempio n. 3
0
        /// <summary>
        /// Outputs expected values for the SP20Drbg
        /// </summary>
        public string GetSP20Vector(int KeySize)
        {
            SBG spd = new SBG();

            byte[] key    = new byte[KeySize];
            byte[] output = new byte[1024];
            for (int i = 0; i < KeySize; i++)
            {
                key[i] = (byte)i;
            }

            spd.Initialize(key);
            spd.Generate(output);

            while (output.Length > 32)
            {
                output = Reduce(output);
            }

            return(HexConverter.ToString(output));
        }
Esempio n. 4
0
        /// <summary>
        /// Fill an array with pseudo random bytes
        /// </summary>
        ///
        /// <param name="Output">Array to fill with random bytes</param>
        public void GetBytes(byte[] Output)
        {
            lock (m_objLock)
            {
                if (m_byteBuffer.Length - m_bufferIndex < Output.Length)
                {
                    int bufSize = m_byteBuffer.Length - m_bufferIndex;
                    // copy remaining bytes
                    Buffer.BlockCopy(m_byteBuffer, m_bufferIndex, Output, 0, bufSize);
                    int rem = Output.Length - bufSize;

                    while (rem > 0)
                    {
                        // fill buffer
                        m_rngGenerator.Generate(m_byteBuffer);

                        if (rem > m_byteBuffer.Length)
                        {
                            Buffer.BlockCopy(m_byteBuffer, 0, Output, bufSize, m_byteBuffer.Length);
                            bufSize += m_byteBuffer.Length;
                            rem     -= m_byteBuffer.Length;
                        }
                        else
                        {
                            Buffer.BlockCopy(m_byteBuffer, 0, Output, bufSize, rem);
                            m_bufferIndex = rem;
                            rem           = 0;
                        }
                    }
                }
                else
                {
                    Buffer.BlockCopy(m_byteBuffer, m_bufferIndex, Output, 0, Output.Length);
                    m_bufferIndex += Output.Length;
                }
            }
        }