RandomStream.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // RandomStream.cpp
  3. //
  4. // $Id: //poco/Main/Foundation/src/RandomStream.cpp#14 $
  5. //
  6. // Library: Foundation
  7. // Package: Crypt
  8. // Module: RandomStream
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // Permission is hereby granted, free of charge, to any person or organization
  14. // obtaining a copy of the software and accompanying documentation covered by
  15. // this license (the "Software") to use, reproduce, display, distribute,
  16. // execute, and transmit the Software, and to prepare derivative works of the
  17. // Software, and to permit third-parties to whom the Software is furnished to
  18. // do so, all subject to the following:
  19. //
  20. // The copyright notices in the Software and this entire statement, including
  21. // the above license grant, this restriction and the following disclaimer,
  22. // must be included in all copies of the Software, in whole or in part, and
  23. // all derivative works of the Software, unless such copies or derivative
  24. // works are solely in the form of machine-executable object code generated by
  25. // a source language processor.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  30. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  31. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  32. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. // DEALINGS IN THE SOFTWARE.
  34. //
  35. #include "Poco/RandomStream.h"
  36. #include "Poco/Random.h"
  37. #include "Poco/SHA1Engine.h"
  38. #if defined(POCO_OS_FAMILY_WINDOWS)
  39. #include "Poco/UnWindows.h"
  40. #include <wincrypt.h>
  41. #elif defined(POCO_OS_FAMILY_UNIX)
  42. #include <fcntl.h>
  43. #include <unistd.h>
  44. #endif
  45. #include <ctime>
  46. namespace Poco {
  47. RandomBuf::RandomBuf(): BufferedStreamBuf(256, std::ios::in)
  48. {
  49. }
  50. RandomBuf::~RandomBuf()
  51. {
  52. }
  53. int RandomBuf::readFromDevice(char* buffer, std::streamsize length)
  54. {
  55. int n = 0;
  56. #if defined(POCO_OS_FAMILY_WINDOWS)
  57. HCRYPTPROV hProvider = 0;
  58. CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
  59. CryptGenRandom(hProvider, (DWORD) length, (BYTE*) buffer);
  60. CryptReleaseContext(hProvider, 0);
  61. n = static_cast<int>(length);
  62. #else
  63. #if defined(POCO_OS_FAMILY_UNIX)
  64. int fd = open("/dev/urandom", O_RDONLY, 0);
  65. if (fd >= 0)
  66. {
  67. n = read(fd, buffer, length);
  68. close(fd);
  69. }
  70. #endif
  71. if (n <= 0)
  72. {
  73. // x is here as a source of randomness, so it does not make
  74. // much sense to protect it with a Mutex.
  75. static UInt32 x = 0;
  76. Random rnd1(256);
  77. Random rnd2(64);
  78. x += rnd1.next();
  79. n = 0;
  80. SHA1Engine engine;
  81. UInt32 t = (UInt32) std::time(NULL);
  82. engine.update(&t, sizeof(t));
  83. void* p = this;
  84. engine.update(&p, sizeof(p));
  85. engine.update(buffer, length);
  86. UInt32 junk[32];
  87. engine.update(junk, sizeof(junk));
  88. while (n < length)
  89. {
  90. for (int i = 0; i < 100; ++i)
  91. {
  92. UInt32 r = rnd2.next();
  93. engine.update(&r, sizeof(r));
  94. engine.update(&x, sizeof(x));
  95. x += rnd1.next();
  96. }
  97. DigestEngine::Digest d = engine.digest();
  98. for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n < length; ++it, ++n)
  99. {
  100. engine.update(*it);
  101. *buffer++ = *it++;
  102. }
  103. }
  104. }
  105. #endif
  106. return n;
  107. }
  108. RandomIOS::RandomIOS()
  109. {
  110. poco_ios_init(&_buf);
  111. }
  112. RandomIOS::~RandomIOS()
  113. {
  114. }
  115. RandomBuf* RandomIOS::rdbuf()
  116. {
  117. return &_buf;
  118. }
  119. RandomInputStream::RandomInputStream(): std::istream(&_buf)
  120. {
  121. }
  122. RandomInputStream::~RandomInputStream()
  123. {
  124. }
  125. } // namespace Poco