aboutsummaryrefslogtreecommitdiff
path: root/src/common/crypto.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2007-05-18 21:19:14 +0000
committerNick Mathewson <nickm@torproject.org>2007-05-18 21:19:14 +0000
commita18770487227bce6e356f169aa86bdef70370c76 (patch)
tree2295aeb0a3ed5757e335ac632cab90563194bd02 /src/common/crypto.c
parent43d64df4fc582c698b2e2ebdaff4891409d3b6be (diff)
downloadtor-a18770487227bce6e356f169aa86bdef70370c76.tar.gz
tor-a18770487227bce6e356f169aa86bdef70370c76.zip
r12980@Kushana: nickm | 2007-05-18 14:11:05 -0400
Add a "swap" function to smartlist, add a "shuffle" function for smartlist to crypto.c, and make appropriate hashtable functions be more const. svn:r10208
Diffstat (limited to 'src/common/crypto.c')
-rw-r--r--src/common/crypto.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c
index 40cb5fafae..bcb8a375a8 100644
--- a/src/common/crypto.c
+++ b/src/common/crypto.c
@@ -1677,6 +1677,21 @@ smartlist_choose(const smartlist_t *sl)
return NULL; /* no elements to choose from */
}
+/** Scramble the elements of sl into a random order. */
+void
+smartlist_shuffle(smartlist_t *sl)
+{
+ int i;
+ /* From the end of the list to the front, choose at random from the
+ positions we haven't looked at yet, and swap that position into the
+ current position. Remember to give "no swap" the same probability as
+ any other swap. */
+ for (i = smartlist_len(sl)-1; i > 0; --i) {
+ int j = crypto_rand_int(i+1);
+ smartlist_swap(sl, i, j);
+ }
+}
+
/** Base-64 encode <b>srclen</b> bytes of data from <b>src</b>. Write
* the result into <b>dest</b>, if it will fit within <b>destlen</b>
* bytes. Return the number of bytes written on success; -1 if