BLI_md5: add a utility function to 'translate' raw 16bytes digest into a nice 32chars hexadecimal string.

That kind of stuff belongs to BLI, not specialized code like thumbs.c
This commit is contained in:
Bastien Montagne 2014-06-20 16:18:26 +02:00
parent fb7c71383b
commit 01d802976f
3 changed files with 20 additions and 3 deletions

View File

@ -41,5 +41,7 @@ void *md5_buffer(const char *buffer, size_t len, void *resblock);
int md5_stream(FILE *stream, void *resblock);
char *md5_to_hexdigest(void *resblock, char r_hex_digest[33]);
#endif

View File

@ -389,3 +389,20 @@ void *md5_buffer(const char *buffer, size_t len, void *resblock)
/* Put result in desired memory area. */
return md5_read_ctx(&ctx, resblock);
}
char *md5_to_hexdigest(void *resblock, char r_hex_digest[33])
{
static const char hex_map[17] = "0123456789abcdef";
const unsigned char *p;
char *q;
short len;
for (q = r_hex_digest, p = (const unsigned char *)resblock, len = 0; len < 16; ++p, ++len) {
const unsigned char c = *p;
*q++ = hex_map[c >> 4];
*q++ = hex_map[c & 15];
}
*q = '\0';
return r_hex_digest;
}

View File

@ -258,9 +258,7 @@ static void thumbname_from_uri(const char *uri, char *thumb, const int thumb_len
md5_buffer(uri, strlen(uri), digest);
hexdigest[0] = '\0';
to_hex_char(hexdigest, digest, 16);
hexdigest[32] = '\0';
BLI_snprintf(thumb, thumb_len, "%s.png", hexdigest);
BLI_snprintf(thumb, thumb_len, "%s.png", md5_to_hexdigest(digest, hexdigest));
// printf("%s: '%s' --> '%s'\n", __func__, uri, thumb);
}