RandomStream.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // RandomStream.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/RandomStream.cpp#1 $
  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. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/RandomStream.h"
  16. #include "Poco/Random.h"
  17. #include "Poco/SHA1Engine.h"
  18. #if defined(POCO_OS_FAMILY_WINDOWS)
  19. #include "Poco/UnWindows.h"
  20. #include <wincrypt.h>
  21. #elif defined(POCO_OS_FAMILY_UNIX)
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #endif
  25. #include <ctime>
  26. namespace Poco {
  27. RandomBuf::RandomBuf(): BufferedStreamBuf(256, std::ios::in)
  28. {
  29. }
  30. RandomBuf::~RandomBuf()
  31. {
  32. }
  33. int RandomBuf::readFromDevice(char* buffer, std::streamsize length)
  34. {
  35. int n = 0;
  36. #if defined(POCO_OS_FAMILY_WINDOWS)
  37. HCRYPTPROV hProvider = 0;
  38. CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
  39. CryptGenRandom(hProvider, (DWORD) length, (BYTE*) buffer);
  40. CryptReleaseContext(hProvider, 0);
  41. n = static_cast<int>(length);
  42. #else
  43. #if defined(POCO_OS_FAMILY_UNIX)
  44. int fd = open("/dev/urandom", O_RDONLY, 0);
  45. if (fd >= 0)
  46. {
  47. n = read(fd, buffer, length);
  48. close(fd);
  49. }
  50. #endif
  51. if (n <= 0)
  52. {
  53. // x is here as a source of randomness, so it does not make
  54. // much sense to protect it with a Mutex.
  55. static UInt32 x = 0;
  56. Random rnd1(256);
  57. Random rnd2(64);
  58. x += rnd1.next();
  59. n = 0;
  60. SHA1Engine engine;
  61. UInt32 t = (UInt32) std::time(NULL);
  62. engine.update(&t, sizeof(t));
  63. void* p = this;
  64. engine.update(&p, sizeof(p));
  65. engine.update(buffer, length);
  66. UInt32 junk[32];
  67. engine.update(junk, sizeof(junk));
  68. while (n < length)
  69. {
  70. for (int i = 0; i < 100; ++i)
  71. {
  72. UInt32 r = rnd2.next();
  73. engine.update(&r, sizeof(r));
  74. engine.update(&x, sizeof(x));
  75. x += rnd1.next();
  76. }
  77. DigestEngine::Digest d = engine.digest();
  78. for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n < length; ++it, ++n)
  79. {
  80. engine.update(*it);
  81. *buffer++ = *it++;
  82. }
  83. }
  84. }
  85. #endif
  86. return n;
  87. }
  88. RandomIOS::RandomIOS()
  89. {
  90. poco_ios_init(&_buf);
  91. }
  92. RandomIOS::~RandomIOS()
  93. {
  94. }
  95. RandomBuf* RandomIOS::rdbuf()
  96. {
  97. return &_buf;
  98. }
  99. RandomInputStream::RandomInputStream(): std::istream(&_buf)
  100. {
  101. }
  102. RandomInputStream::~RandomInputStream()
  103. {
  104. }
  105. } // namespace Poco