summaryrefslogtreecommitdiff
path: root/sal/osl/unx/random.cxx
diff options
context:
space:
mode:
authorNorbert Thiebaud <nthiebaud@gmail.com>2015-04-21 20:51:04 -0500
committerMichael Stahl <mstahl@redhat.com>2015-07-20 21:51:28 +0000
commit5ede1d017cb6e602db699a1b2364f9708de441c8 (patch)
treed8331144126b4352e201ba50b6e3a7eed6a11a3c /sal/osl/unx/random.cxx
parent04fb0ba0cc3693dbd89bbcf010e5944805d36172 (diff)
add an internal api to osl to collect 'native' random numbers
use /dev/urandom on unixes and rand_s() on windows Change-Id: Iccd6e01706f16d82f23cda0f0a410953c32fbc02 Reviewed-on: https://gerrit.libreoffice.org/15473 Reviewed-by: Michael Stahl <mstahl@redhat.com> Tested-by: Michael Stahl <mstahl@redhat.com>
Diffstat (limited to 'sal/osl/unx/random.cxx')
-rw-r--r--sal/osl/unx/random.cxx48
1 files changed, 48 insertions, 0 deletions
diff --git a/sal/osl/unx/random.cxx b/sal/osl/unx/random.cxx
new file mode 100644
index 000000000000..2830daaa4ec6
--- /dev/null
+++ b/sal/osl/unx/random.cxx
@@ -0,0 +1,48 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include "system.hxx"
+#include "internal/oslrandom.h"
+
+#include <assert.h>
+
+int osl_get_system_random_data(char* buffer, size_t desired_len)
+{
+ ssize_t nb_read;
+ int fd;
+
+ assert(buffer);
+ fd = open("/dev/urandom", O_RDONLY);
+ if(fd != -1)
+ {
+ while(desired_len)
+ {
+ if ((nb_read = read(fd, buffer, desired_len)) == -1)
+ {
+ if (errno != EINTR)
+ {
+ return false;
+ }
+ }
+ else
+ {
+ buffer += nb_read;
+ desired_len -= nb_read;
+ }
+ }
+ close(fd);
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */