aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_random.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2009-10-08 19:00:19 +0000
committerChristian Grothoff <christian@grothoff.org>2009-10-08 19:00:19 +0000
commitfe947a9704bed14c2fc74b2f2069596fe884cbad (patch)
tree644efdaa37d692b09f8e279bfe5e8b2ebe81448b /src/util/crypto_random.c
parenta8b0ab037820f6a9f405be3855ce8d3ebbd4399b (diff)
downloadgnunet-fe947a9704bed14c2fc74b2f2069596fe884cbad.tar.gz
gnunet-fe947a9704bed14c2fc74b2f2069596fe884cbad.zip
better comments
Diffstat (limited to 'src/util/crypto_random.c')
-rw-r--r--src/util/crypto_random.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/util/crypto_random.c b/src/util/crypto_random.c
index b56523a46..50af9f251 100644
--- a/src/util/crypto_random.c
+++ b/src/util/crypto_random.c
@@ -30,6 +30,10 @@
30#include <gcrypt.h> 30#include <gcrypt.h>
31 31
32/** 32/**
33 * Produce a random value.
34 *
35 * @param mode desired quality of the random number
36 * @param i the upper limit (exclusive) for the random number
33 * @return a random value in the interval [0,i[. 37 * @return a random value in the interval [0,i[.
34 */ 38 */
35uint32_t 39uint32_t
@@ -97,26 +101,31 @@ GNUNET_CRYPTO_random_permute (enum GNUNET_CRYPTO_Quality mode, unsigned int n)
97 101
98/** 102/**
99 * Random on unsigned 64-bit values. 103 * Random on unsigned 64-bit values.
104 *
105 *
106 * @param mode desired quality of the random number
107 * @param max value returned will be in range [0,max) (exclusive)
108 * @return random 64-bit number
100 */ 109 */
101uint64_t 110uint64_t
102GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode, 111GNUNET_CRYPTO_random_u64 (enum GNUNET_CRYPTO_Quality mode,
103 uint64_t u) 112 uint64_t max)
104{ 113{
105 uint64_t ret; 114 uint64_t ret;
106 115
107 GNUNET_assert (u > 0); 116 GNUNET_assert (max > 0);
108 if (mode == GNUNET_CRYPTO_QUALITY_STRONG) 117 if (mode == GNUNET_CRYPTO_QUALITY_STRONG)
109 { 118 {
110 gcry_randomize ((unsigned char *) &ret, 119 gcry_randomize ((unsigned char *) &ret,
111 sizeof (uint64_t), 120 sizeof (uint64_t),
112 GCRY_STRONG_RANDOM); 121 GCRY_STRONG_RANDOM);
113 return ret % u; 122 return ret % max;
114 } 123 }
115 else 124 else
116 { 125 {
117 ret = u * ((double) RANDOM () / RAND_MAX); 126 ret = max * ((double) RANDOM () / RAND_MAX);
118 if (ret >= u) 127 if (ret >= max)
119 ret = u - 1; 128 ret = max - 1;
120 return ret; 129 return ret;
121 } 130 }
122} 131}