Entropy.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. /*
  7. * This class accumulates entropy from sources such as the process' IO counts
  8. * and the thread's cycle counter (i.e. number of cycles spent in user + kernel
  9. * mode) and is used to improve the entropy of the seed used by Math.random.
  10. */
  11. class Entropy {
  12. public:
  13. Entropy()
  14. {
  15. previousValue = 0;
  16. u.value = 0;
  17. currentIndex = 0;
  18. }
  19. void Initialize();
  20. void Add(const char *buffer, size_t size);
  21. void AddIoCounters();
  22. void AddThreadCycleTime();
  23. unsigned __int64 GetRand() const;
  24. private:
  25. unsigned __int32 previousValue;
  26. union
  27. {
  28. unsigned __int32 value;
  29. char array[sizeof(unsigned __int32)];
  30. } u;
  31. size_t currentIndex;
  32. static const uint32 kInitIterationCount;
  33. void BeginAdd();
  34. void Add(const char byteValue);
  35. void AddCurrentTime();
  36. };