2 * Copyright (c) 2009 Baptiste Coudurier <baptiste.coudurier@gmail.com>
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #if HAVE_CRYPTGENRANDOM
35 #include "random_seed.h"
37 static int read_random(uint32_t *dst, const char *file)
40 int fd = avpriv_open(file, O_RDONLY);
45 err = read(fd, dst, sizeof(*dst));
54 static uint32_t get_generic_seed(void)
60 float s = 0.000000000001;
62 for (i = 0; bits < 64; i++) {
64 if (last_t && fabs(t - last_t) > s || t == (clock_t) -1) {
65 if (i < 10000 && s < (1 << 24)) {
69 random = 2 * random + (i & 1);
76 random ^= AV_READ_TIME();
81 random += random >> 32;
86 uint32_t av_get_random_seed(void)
90 #if HAVE_CRYPTGENRANDOM
92 if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
93 CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
94 BOOL ret = CryptGenRandom(provider, sizeof(seed), (PBYTE) &seed);
95 CryptReleaseContext(provider, 0);
101 if (read_random(&seed, "/dev/urandom") == sizeof(seed))
103 if (read_random(&seed, "/dev/random") == sizeof(seed))
105 return get_generic_seed();