コード例 #1
0
        /// <summary>
        /// Reset the DGCPrng instance
        /// </summary>
        public void Reset()
        {
            if (m_digestEngine != null)
            {
                m_digestEngine.Dispose();
                m_digestEngine = null;
            }
            if (m_seedGenerator != null)
            {
                m_seedGenerator.Dispose();
                m_seedGenerator = null;
            }

            m_digestEngine = GetDigest(m_digestType);
            m_rngGenerator = new DGC(m_digestEngine);

            if (m_stateSeed != null)
            {
                m_rngGenerator.Initialize(m_stateSeed);
            }
            else
            {
                m_seedGenerator = GetSeedGenerator(m_seedType);
                m_rngGenerator.Initialize(m_seedGenerator.GetBytes((m_digestEngine.BlockSize * 2) + 8));   // 2 * block + counter (2*bsz+8)
            }

            m_rngGenerator.Generate(m_byteBuffer);
            m_bufferIndex = 0;
        }
コード例 #2
0
        private void DGCDRBGTest(byte[] Seed, byte[] Expected)
        {
            DGC rGen = new DGC(new SHA256());

            byte[] output = new byte[32];

            rGen.Update(Seed);

            for (int i = 0; i != 1024; i++)
            {
                rGen.Generate(output);
            }

            if (Evaluate.AreEqual(Expected, output) == false)
            {
                throw new Exception("DGCDRBG: Values are not equal! Expected: " + HexConverter.ToString(output) + " Received: " + HexConverter.ToString(Expected));
            }
        }
コード例 #3
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;
                }
            }
        }