/*! \brief Compute MD5 checksum */ void compute_md5(char *dst, char *src, int src_len) { MD5_CTX context; unsigned char digest[16]; MD5Init (&context); MD5Update (&context, src, src_len); U_MD5Final (digest, &context); string2hex(digest, 16, dst); }
/*! \brief * Calculate a MD5 digest over a file. * This function assumes 32 bytes in the destination buffer. * \param dest destination * \param file_name file for that the digest should be calculated * \return zero on success, negative on errors */ static int MD5File(char *dest, const char *file_name) { MD5_CTX context; FILE *input; unsigned char buffer[32768]; unsigned char hash[16]; unsigned int counter, size; struct stat stats; if (!dest || !file_name) { LM_ERR("invalid parameter value\n"); return -1; } if (stat(file_name, &stats) != 0) { LM_ERR("could not stat file %s\n", file_name); return -1; } size = stats.st_size; MD5Init(&context); if((input = fopen(file_name, "rb")) == NULL) { LM_ERR("could not open file %s\n", file_name); return -1; } while(size) { counter = (size > sizeof(buffer)) ? sizeof(buffer) : size; if ((counter = fread(buffer, 1, counter, input)) <= 0) { fclose(input); return -1; } U_MD5Update(&context, buffer, counter); size -= counter; } fclose(input); U_MD5Final(hash, &context); string2hex(hash, 16, dest); LM_DBG("MD5 calculated: %.*s for file %s\n", MD5_LEN, dest, file_name); return 0; }
/*! * \brief Calculate a MD5 digests over a string array * * Calculate a MD5 digests over a string array and stores the result in the * destination char array. This function assumes 32 bytes in the destination * buffer. * \param dst destination * \param src string input array * \param size elements in the input array */ void MD5StringArray (char *dst, str src[], int size) { MD5_CTX context; unsigned char digest[16]; int i; int len; char *s; MD5Init (&context); for (i=0; i<size; i++) { trim_len( len, s, src[i] ); if (len > 0) MD5Update (&context, s, len); } U_MD5Final (digest, &context); string2hex(digest, 16, dst ); DBG("DEBUG: MD5 calculated: %.*s\n", MD5_LEN, dst ); }