File: es_file.cpp

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (54 lines) | stat: -rw-r--r-- 1,726 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*************************************************
* File EntropySource Source File                 *
* (C) 1999-2005 The Botan Project                *
*************************************************/

#include <botan/es_file.h>
#include <botan/conf.h>
#include <botan/parsing.h>
#include <fstream>

namespace Botan {

/*************************************************
* File_EntropySource Constructor                 *
*************************************************/
File_EntropySource::File_EntropySource(const std::string& sources)
   {
   std::vector<std::string> source_list = split_on(sources, ':');
   std::vector<std::string> defaults = Config::get_list("rng/es_files");

   for(u32bit j = 0; j != source_list.size(); j++)
      add_source(source_list[j]);
   for(u32bit j = 0; j != defaults.size(); j++)
      add_source(defaults[j]);
   }

/*************************************************
* Add another file to the list                   *
*************************************************/
void File_EntropySource::add_source(const std::string& source)
   {
   sources.push_back(source);
   }

/*************************************************
* Gather Entropy from Randomness Source          *
*************************************************/
u32bit File_EntropySource::slow_poll(byte output[], u32bit length)
   {
   u32bit read = 0;
   for(u32bit j = 0; j != sources.size(); j++)
      {
      std::ifstream random_source(sources[j].c_str(), std::ios::binary);
      if(!random_source) continue;
      random_source.read((char*)output + read, length);
      read += random_source.gcount();
      length -= random_source.gcount();
      if(length == 0)
         break;
      }
   return read;
   }

}