Browse Source

Helper functions to perform our truncated base64 encoding on hexdigests.

svn:r5087
Nick Mathewson 18 years ago
parent
commit
f8a80e8d59
2 changed files with 31 additions and 0 deletions
  1. 25 0
      src/common/crypto.c
  2. 6 0
      src/common/crypto.h

+ 25 - 0
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.
  */

+ 6 - 0
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);