Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tor
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
The Tor Project
Core
debian
tor
Commits
006e2e86
Commit
006e2e86
authored
14 years ago
by
Nick Mathewson
Browse files
Options
Downloads
Patches
Plain Diff
Add a function to return a double in range [0,1).
parent
e2b117eb
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/common/crypto.c
+15
-0
15 additions, 0 deletions
src/common/crypto.c
src/common/crypto.h
+1
-0
1 addition, 0 deletions
src/common/crypto.h
src/test/test_crypto.c
+4
-0
4 additions, 0 deletions
src/test/test_crypto.c
with
20 additions
and
0 deletions
src/common/crypto.c
+
15
−
0
View file @
006e2e86
...
...
@@ -2056,6 +2056,21 @@ crypto_rand_uint64(uint64_t max)
}
}
/** Return a pseudorandom double d, chosen uniformly from the range
* 0.0 <= d < 1.0.
*/
double
crypto_rand_double
(
void
)
{
/* We just use an unsigned int here; we don't really care about getting
* more than 32 bits of resolution */
unsigned
int
uint
;
do
{
crypto_rand
((
char
*
)
&
uint
,
sizeof
(
uint
));
}
while
(
uint
==
UINT_MAX
);
return
((
double
)
uint
)
/
(
double
)
UINT_MAX
;
}
/** Generate and return a new random hostname starting with <b>prefix</b>,
* ending with <b>suffix</b>, and containing no less than
* <b>min_rand_len</b> and no more than <b>max_rand_len</b> random base32
...
...
This diff is collapsed.
Click to expand it.
src/common/crypto.h
+
1
−
0
View file @
006e2e86
...
...
@@ -210,6 +210,7 @@ int crypto_seed_rng(int startup);
int
crypto_rand
(
char
*
to
,
size_t
n
);
int
crypto_rand_int
(
unsigned
int
max
);
uint64_t
crypto_rand_uint64
(
uint64_t
max
);
double
crypto_rand_double
(
void
);
char
*
crypto_random_hostname
(
int
min_rand_len
,
int
max_rand_len
,
const
char
*
prefix
,
const
char
*
suffix
);
...
...
This diff is collapsed.
Click to expand it.
src/test/test_crypto.c
+
4
−
0
View file @
006e2e86
...
...
@@ -57,6 +57,7 @@ test_crypto_rng(void)
{
int
i
,
j
,
allok
;
char
data1
[
100
],
data2
[
100
];
double
d
;
/* Try out RNG. */
test_assert
(
!
crypto_seed_rng
(
0
));
...
...
@@ -76,6 +77,9 @@ test_crypto_rng(void)
big
=
crypto_rand_uint64
(
U64_LITERAL
(
5
));
if
(
big
>=
5
)
allok
=
0
;
d
=
crypto_rand_double
();
test_assert
(
d
>=
0
);
test_assert
(
d
<
1
.
0
);
host
=
crypto_random_hostname
(
3
,
8
,
"www."
,
".onion"
);
if
(
strcmpstart
(
host
,
"www."
)
||
strcmpend
(
host
,
".onion"
)
||
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment