OpenSSL has a function:

unsigned char* MD5(const unsigned char* d, unsigned long n, unsigned char* md); 

The problem is that I need to make text input through the console and save it to a string in order to use it as an argument for MD5() and get the value of the hash function, but

 istream& std::istream::getline(char* s, streamsize n); 

accepts only char* arguments.

How to solve the type mismatch problem?

  • (unsigned char *) charPtr will definitely save you. - Vladimir Martyanov

1 answer 1

Do castes, in this case it is useful and safe.

 std::string str; std::getline(std::cin, str); const char* chars = str.data(); const unsigned char* data_bytes = reinterpret_cast<const unsigned char*>(chars); std::array<unsigned char, 16> hash; MD5(data_bytes, str.size(), &hash[0]); 
  • you can even do without an explicit conversion - ampawd
  • @ampawd is not possible, these are not related types - Abyx
  • What kind of "caste"? - Cerbo
  • @Cerbo, this is such a programming terminology, borrowing from cast (English). Of course there is a long Russian analogue, something about transformations and types, but why is it needed if there is a short, commonly understood term. - Abyx