| 1 | #include <time.h> |
|---|---|
| 2 | #include <port/unistd.h> |
| 3 | #include <sys/types.h> |
| 4 | #include <Common/Exception.h> |
| 5 | #include <Common/randomSeed.h> |
| 6 | #include <Common/SipHash.h> |
| 7 | #include <Core/Types.h> |
| 8 | #include <port/clock.h> |
| 9 | |
| 10 | |
| 11 | namespace DB |
| 12 | { |
| 13 | namespace ErrorCodes |
| 14 | { |
| 15 | extern const int CANNOT_CLOCK_GETTIME; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | |
| 20 | DB::UInt64 randomSeed() |
| 21 | { |
| 22 | struct timespec times; |
| 23 | if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, ×)) |
| 24 | DB::throwFromErrno("Cannot clock_gettime.", DB::ErrorCodes::CANNOT_CLOCK_GETTIME); |
| 25 | |
| 26 | /// Not cryptographically secure as time, pid and stack address can be predictable. |
| 27 | |
| 28 | SipHash hash; |
| 29 | hash.update(times.tv_nsec); |
| 30 | hash.update(times.tv_sec); |
| 31 | hash.update(getpid()); |
| 32 | hash.update(×); |
| 33 | return hash.get64(); |
| 34 | } |
| 35 |