diff options
author | Nick Mathewson <nickm@torproject.org> | 2004-10-30 19:26:31 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2004-10-30 19:26:31 +0000 |
commit | 5bc0dba9338c6722f6833351b1401571533eb63d (patch) | |
tree | f20dabe9b36bffce31d8b7b6d0edf7773f04277d /src/common/crypto.c | |
parent | 9510d9a79286d4baf3f3bd8ee69ae5ff422f9735 (diff) | |
download | tor-5bc0dba9338c6722f6833351b1401571533eb63d.tar tor-5bc0dba9338c6722f6833351b1401571533eb63d.tar.gz |
Move all util functions that need openssl into crypto.c; make non-openssl functions that util needs into util. Now openssl can be separated.
svn:r2628
Diffstat (limited to 'src/common/crypto.c')
-rw-r--r-- | src/common/crypto.c | 63 |
1 files changed, 10 insertions, 53 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c index 4fe2ae0e8..e63a92c99 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -1470,6 +1470,16 @@ int crypto_pseudo_rand_int(unsigned int max) { } } +/** Return a randomly chosen element of sl; or NULL if sl is empty. + */ +void *smartlist_choose(const smartlist_t *sl) { + size_t len; + len = smartlist_len(sl); + if(len) + return smartlist_get(sl,crypto_pseudo_rand_int(len)); + return NULL; /* no elements to choose from */ +} + /** 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 @@ -1540,59 +1550,6 @@ base32_encode(char *dest, size_t destlen, const char *src, size_t srclen) dest[i] = '\0'; } -void base16_encode(char *dest, size_t destlen, const char *src, size_t srclen) -{ - const char *end; - char *cp; - - tor_assert(destlen >= srclen*2+1); - - cp = dest; - end = src+srclen; - while (src<end) { - sprintf(cp,"%02X",*(const uint8_t*)src); - ++src; - cp += 2; - } - *cp = '\0'; -} - -static const char HEX_DIGITS[] = "0123456789ABCDEFabcdef"; - -static INLINE int hex_decode_digit(char c) -{ - const char *cp; - int n; - cp = strchr(HEX_DIGITS, c); - if (!cp) - return -1; - n = cp-HEX_DIGITS; - if (n<=15) - return n; /* digit or uppercase */ - else - return n-6; /* lowercase */ -} - -int base16_decode(char *dest, size_t destlen, const char *src, size_t srclen) -{ - const char *end; - int v1,v2; - if ((srclen % 2) != 0) - return -1; - if (destlen < srclen/2) - return -1; - end = src+srclen; - while (src<end) { - v1 = hex_decode_digit(*src); - v2 = hex_decode_digit(*(src+1)); - if(v1<0||v2<0) - return -1; - *(uint8_t*)dest = (v1<<4)|v2; - ++dest; - src+=2; - } - return 0; -} /* Local Variables: |