/** @file * @brief Util namespace header. * * * * @copyright Intra2net AG * @license GPLv2 */ #include "util.hpp" #include #include #include namespace Util { /** * Computes a MD5 Digest from the given string and returns the HEX representation * @param data The string to compute the md5 for * @return The computed md5 in hex */ std::string compute_md5_digest(std::string data) { // compute an MD5 digest. EVP_MD_CTX mdctx; const EVP_MD *md; unsigned char md_value[EVP_MAX_MD_SIZE]; unsigned int md_len = 0; // Add all digest algorithms to the internal table. OpenSSL_add_all_digests(); // Get the md5 digest algorithm from the internal table. md = EVP_get_digestbyname("md5"); // Test if md is initialized. if ( (md == NULL) || (EVP_MD_size(md) == 0) ) throw std::invalid_argument("NULL pointer: md"); // Initalize digest content mdctx. EVP_MD_CTX_init(&mdctx); // Now we can call init_ex. if ( EVP_DigestInit_ex(&mdctx, md, NULL) == 0 ) { EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ EVP_cleanup(); /*lint !e534 */ throw std::invalid_argument("Could not set up digest context correctly"); } // Test if data is empty. if ( data.empty() ) { EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ EVP_cleanup(); /*lint !e534 */ throw std::invalid_argument("Passed data is empty"); } // Hash the data. At this point data is not empty and &mdctx is initialized. if ( EVP_DigestUpdate(&mdctx, data.c_str(), data.size()) == 0 ) { EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ EVP_cleanup(); /*lint !e534 */ throw std::invalid_argument("Could not hash data into digest context"); } // Retrieve the digest value from &mdctx and place it in md_value. if ( EVP_DigestFinal_ex(&mdctx, md_value, &md_len) == 0 ) { EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ EVP_cleanup(); /*lint !e534 */ throw std::invalid_argument("Could not retrieve digest value"); } // Test if md_value is filled correctly and md_len is not zero. if ( (md_len == 0) || (EVP_MD_CTX_size(&mdctx) == 0) ) { EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ EVP_cleanup(); /*lint !e534 */ throw std::invalid_argument("Retrieved invalid digest value"); } // Internal cleanup of the digest content. EVP_MD_CTX_cleanup(&mdctx); /*lint !e534 */ EVP_cleanup(); /*lint !e534 */ // Convert md5 digest C string to hex. std::ostringstream oss_digest_md5_hex; for(unsigned int i = 0; i < md_len; ++i) { // We have to do a static cast to an decimal representation, cause otherwise ostringstream would interpret // the stream as a character and output the character representation of the hex value. oss_digest_md5_hex << std::hex << static_cast(md_value[i]); } return oss_digest_md5_hex.str(); } /** * Get the status code from the given data. * @param data The data containing the status code at front, limited by " ". * @return The parsed status code. */ std::string parse_status_code(std::string data) { std::list tokens; boost::algorithm::split(tokens,data,boost::is_any_of(" ")); if ( tokens.empty() ) return ""; return tokens.front(); } /** * Get the status code from the given data. * @param data The data containing the status code at front, limited by " ". * @param delimiter The delimiter to use. * @return The parsed status code. */ std::string parse_status_code(std::string data, std::string delimiter) { std::list tokens; boost::algorithm::split(tokens,data,boost::is_any_of(delimiter)); if ( tokens.empty() ) return ""; return tokens.front(); } }