diff options
author | Nick Mathewson <nickm@torproject.org> | 2005-09-18 02:18:59 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2005-09-18 02:18:59 +0000 |
commit | f8a80e8d599d87ea18cb72b7b22441612f1e26ac (patch) | |
tree | 08d3f997bbc3bd22143dcedcd74a958eb14e2766 | |
parent | 312af361269a1b82e3e1b2df3a961eba8748f0ad (diff) | |
download | tor-f8a80e8d599d87ea18cb72b7b22441612f1e26ac.tar.gz tor-f8a80e8d599d87ea18cb72b7b22441612f1e26ac.zip |
Helper functions to perform our truncated base64 encoding on hexdigests.
svn:r5087
-rw-r--r-- | src/common/crypto.c | 25 | ||||
-rw-r--r-- | src/common/crypto.h | 6 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c index 113e1dcab2..50f937e703 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -1688,6 +1688,31 @@ base64_decode(char *dest, size_t destlen, const char *src, size_t srclen) return ret; } +int +digest_to_base64(char *d64, const char *digest) +{ + char buf[256]; + base64_encode(buf, sizeof(buf), digest, DIGEST_LEN); + buf[BASE64_DIGEST_LEN] = '\0'; + memcpy(d64, buf, BASE64_DIGEST_LEN+1); + return 0; +} + +int +digest_from_base64(char *digest, const char *d64) +{ + char buf_in[BASE64_DIGEST_LEN+3]; + char buf[256]; + if (strlen(d64) != BASE64_DIGEST_LEN) + return -1; + memcpy(buf_in, d64, BASE64_DIGEST_LEN); + memcpy(buf_in+BASE64_DIGEST_LEN, "=\n\0", 3); + if (base64_decode(buf, sizeof(buf), buf_in, strlen(buf_in)) != DIGEST_LEN) + return -1; + memcpy(digest, buf, DIGEST_LEN); + return 0; +} + /** Implements base32 encoding as in rfc3548. Limitation: Requires * that srclen*8 is a multiple of 5. */ diff --git a/src/common/crypto.h b/src/common/crypto.h index 903fe72ea0..ce01fee103 100644 --- a/src/common/crypto.h +++ b/src/common/crypto.h @@ -24,6 +24,9 @@ /** Length of our DH keys. */ #define DH_BYTES (1024/8) +/* DOCDOC */ +#define BASE64_DIGEST_LEN 27 + /** Constants used to indicate no padding for public-key encryption */ #define PK_NO_PADDING 60000 /** Constants used to indicate PKCS1 padding for public-key encryption */ @@ -155,6 +158,9 @@ int base64_decode(char *dest, size_t destlen, const char *src, size_t srclen); #define BASE32_CHARS "abcdefghijklmnopqrstuvwxyz234567" void base32_encode(char *dest, size_t destlen, const char *src, size_t srclen); +int digest_to_base64(char *d64, const char *digest); +int digest_from_base64(char *digest, const char *d64); + #define S2K_SPECIFIER_LEN 9 void secret_to_key(char *key_out, size_t key_out_len, const char *secret, size_t secret_len, const char *s2k_specifier); |