You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

onetimepad.hpp 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // onetimepad.hpp
  2. // Copyright (C) 2006 - 2007 MicroNeil Research Corporation
  3. //
  4. // This module leverages the Mangler encryption engine to create
  5. // cryptographically strong one-time pads and random numbers upon request.
  6. // The engine is seeded by /dev/urandom on *nix machines and by CryptGenRandom
  7. // on win32 machines. Additionally, each call to get new data introduces a
  8. // small amount of entropy based on the jitter in timing between calls and the
  9. // amount of time the application has been running since the generator was
  10. // started. Additional entropy can be provided by the application or again from
  11. // one of the core entropy generators (/dev/urandom or CryptGenRandom).
  12. #pragma once
  13. #include <vector>
  14. #include "mangler.hpp"
  15. namespace codedweller {
  16. typedef std::vector<unsigned char> PadBuffer;
  17. class OneTimePad { // One Time Pad generator.
  18. private:
  19. MANGLER PadGenerator; // MANGLER as a PRNG.
  20. void addLightweightEntropy(); // Add light weight entropy bits.
  21. PadBuffer Entropy(int Length = 1024); // System entropy source.
  22. void* fill(void* Object, int Size); // Internal method to fill an object.
  23. bool StrongEntropyFlag; // True if strong entropy is used.
  24. public:
  25. OneTimePad(); // Constructor initializes w/ Entropy.
  26. bool isStrong(); // True if strong entropy is available.
  27. PadBuffer Pad(int Length); // Get a pad of Length.
  28. void addEntropy(); // Add entropy from the system source.
  29. void addEntropy(PadBuffer Entropy); // Add entropy from this source.
  30. template <typename T> // Fill any kind of object
  31. T& fill(T& Object) { // with random bytes.
  32. fill((void*) &Object, sizeof(Object)); // Get a void ptr to it, fill it,
  33. return Object; // and return it to the caller.
  34. }
  35. };
  36. } // End namespace codedweller