mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-19 22:22:00 +00:00
fbe7df73a2
CONTRIB-254
35 lines
758 B
C++
35 lines
758 B
C++
#include <time.h>
|
|
#include <port/unistd.h>
|
|
#include <sys/types.h>
|
|
#include <Common/Exception.h>
|
|
#include <Common/randomSeed.h>
|
|
#include <Common/SipHash.h>
|
|
#include <Core/Types.h>
|
|
#include <port/clock.h>
|
|
|
|
|
|
namespace DB
|
|
{
|
|
namespace ErrorCodes
|
|
{
|
|
extern const int CANNOT_CLOCK_GETTIME;
|
|
}
|
|
}
|
|
|
|
|
|
DB::UInt64 randomSeed()
|
|
{
|
|
struct timespec times;
|
|
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, ×))
|
|
DB::throwFromErrno("Cannot clock_gettime.", DB::ErrorCodes::CANNOT_CLOCK_GETTIME);
|
|
|
|
/// Not cryptographically secure as time, pid and stack address can be predictable.
|
|
|
|
SipHash hash;
|
|
hash.update(times.tv_nsec);
|
|
hash.update(times.tv_sec);
|
|
hash.update(getpid());
|
|
hash.update(×);
|
|
return hash.get64();
|
|
}
|