Problem: I take a user string and want to set it as the MD5 checksum header using the S3 C++ SDK
I have this code to convert a string to an MD5 checksum string which I got from the openssl website:
<code>std::string md5(const std::string &str){
unsigned char hash[MD5_DIGEST_LENGTH];
MD5_CTX md5;
MD5_Init(&md5);
MD5_Update(&md5, str.c_str(), str.size());
MD5_Final(hash, &md5);
std::stringstream ss;
for(int i = 0; i < MD5_DIGEST_LENGTH; i++){
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>( hash[i] );
}
return ss.str();
}</code>
<code>std::string md5(const std::string &str){
unsigned char hash[MD5_DIGEST_LENGTH];
MD5_CTX md5;
MD5_Init(&md5);
MD5_Update(&md5, str.c_str(), str.size());
MD5_Final(hash, &md5);
std::stringstream ss;
for(int i = 0; i < MD5_DIGEST_LENGTH; i++){
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>( hash[i] );
}
return ss.str();
}</code>
std::string md5(const std::string &str){
unsigned char hash[MD5_DIGEST_LENGTH];
MD5_CTX md5;
MD5_Init(&md5);
MD5_Update(&md5, str.c_str(), str.size());
MD5_Final(hash, &md5);
std::stringstream ss;
for(int i = 0; i < MD5_DIGEST_LENGTH; i++){
ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>( hash[i] );
}
return ss.str();
}
And I simply take the string and request.SetContentMD5(checksum);
the output of the function.
However, I keep getting the following error:
<code>[InvalidDigest - Unable to parse ExceptionName: InvalidDigest Message: The Content-MD5 specified is invalid.]
</code>
<code>[InvalidDigest - Unable to parse ExceptionName: InvalidDigest Message: The Content-MD5 specified is invalid.]
</code>
[InvalidDigest - Unable to parse ExceptionName: InvalidDigest Message: The Content-MD5 specified is invalid.]
And I have no idea why that is the case. It seems like I am doing the try thing in terms of the conversion, but maybe the output is correct for S3 API?