randpool.cpp

来自「含有多种公开密钥算法、多种块加密、多种数据流加密、多种HASH函数、多种Chec」· C++ 代码 · 共 126 行

CPP
126
字号
/************************************************** Randpool Source File                           ** (C) 1999-2002 The Botan Project                **************************************************/#include <botan/randpool.h>#include <botan/lookup.h>namespace Botan {/************************************************** Generate a random byte                         **************************************************/byte Randpool::random()   {   if(position == buffer.size())      generate(system_clock());   return buffer[position++];   }/************************************************** Generate a buffer of random bytes              **************************************************/void Randpool::randomize(byte out[], u32bit length)   {   while(length >= buffer.size() - position)      {      xor_buf(out, buffer + position, buffer.size() - position);      length -= (buffer.size() - position);      out += (buffer.size() - position);      generate(system_clock());      }   xor_buf(out, buffer + position, length);   position += length;   }/************************************************** Refill the buffer                              **************************************************/void Randpool::generate(u64bit input)   {   static const u32bit MAX_ITERATIONS = 32;   hash->update((byte*)&input, 8);   hash->update(pool, pool.size());   buffer = hash->final();   xor_buf(pool, buffer, buffer.size());   mix_pool();   if(++iteration == MAX_ITERATIONS)      {      reseed();      iteration = 0;      }   position = 0;   }/************************************************** Mix up the pool                                **************************************************/void Randpool::mix_pool()   {   for(u32bit j = 0; j != pool.size(); j += buffer.size())      {      xor_buf(pool + (j + buffer.size()) % pool.size(), pool + j,              buffer.size());      hash->update(pool + j, std::min(3 * buffer.size(), pool.size() - j));      hash->final(pool + j);      }   }/************************************************** Reseed the pool                                **************************************************/void Randpool::reseed()   {   SecureVector<byte> out(pool.size() / 2);   randomize(out, out.size());   randomize(out, out.size());   add_entropy(out, out.size());   }/************************************************** Add entropy to internal state                  **************************************************/void Randpool::add_randomness(const byte data[], u32bit length) throw()   {   while(length)      {      u32bit added = std::min(pool.size() / 2, length);      xor_buf(pool, data, added);      generate(system_clock());      length -= added;      data += added;      }   generate(system_time());   }/************************************************** Clear memory of sensitive data                 **************************************************/void Randpool::clear() throw()   {   hash->clear();   pool.clear();   buffer.clear();   iteration = position = 0;   }/************************************************** Randpool Constructor                           **************************************************/Randpool::Randpool()   {   const HashFunction* haval = retrieve_hash("HAVAL(32)");   if(haval)      hash = haval->clone();   else      hash = get_hash("SHA-1");   buffer.create(hash->OUTPUT_LENGTH);   pool.create(12 * hash->OUTPUT_LENGTH);   iteration = position = 0;   generate(system_time());   }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?