WhiZTiM

C++ mbedtls md5 hash example

Aug 16th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <array>
  2. #include <vector>
  3. #include <string>
  4. #include <iomanip>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include "mbedtls/md5.h"
  8. #include "mbedtls/base64.h"
  9.  
  10. template<std::size_t N>
  11. std::string toHex(const std::array<unsigned char, N>& values);
  12.  
  13. class Md5Hasher {
  14. private:
  15.     mbedtls_md5_context md5_context;
  16. public:
  17.     Md5Hasher() {
  18.         mbedtls_md5_init(&md5_context);
  19.     }
  20.  
  21.     void prepare() {
  22.         //Prepare context for hashing byte sequence
  23.         mbedtls_md5_starts_ret(&md5_context);
  24.     }
  25.  
  26.     void update(std::string input) {
  27.         //Process message a bit
  28.         mbedtls_md5_update_ret(&md5_context, reinterpret_cast<const unsigned char*>(input.c_str()), input.size());
  29.     }
  30.  
  31.     std::string finish() {
  32.         std::array<unsigned char, 16> outputArr;
  33.         mbedtls_md5_finish_ret(&md5_context, outputArr.data());
  34.         return toHex(outputArr);
  35.     }
  36.  
  37.     ~Md5Hasher() {
  38.         mbedtls_md5_free(&md5_context);
  39.     }
  40. };
  41.  
  42.  
  43. int main() {
  44.     //Collect input
  45.     std::cout << "Input: ";
  46.     std::string input;
  47.     std::getline(std::cin, input);
  48.  
  49.     Md5Hasher hasher;
  50.     hasher.prepare();
  51.     hasher.update(input);
  52.  
  53.     std::cout << "MD5 digest is: " << hasher.finish() << "\n";
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. template<std::size_t N>
  72. std::string toHex(const std::array<unsigned char, N>& values) {
  73.     std::stringstream stream;
  74.     for (int i : values) {
  75.         stream << std::setfill ('0') << std::setw(2)
  76.                 << std::hex << i;
  77.     }
  78.   return stream.str();
  79. }
  80.  
  81. auto oldmain() -> int {
  82.  
  83.     //Setup context
  84.     mbedtls_md5_context md5_context;
  85.  
  86.     //Initialize Context
  87.     mbedtls_md5_init(&md5_context);
  88.  
  89.     //Prepare context for hashing byte sequence
  90.     mbedtls_md5_starts_ret(&md5_context);
  91.  
  92.     std::cout << "Input: ";
  93.     std::string input;
  94.     std::getline(std::cin, input);
  95.  
  96.     //Process message a bit
  97.     mbedtls_md5_update_ret(&md5_context, reinterpret_cast<const unsigned char*>(input.c_str()), input.size());
  98.  
  99.     std::array<unsigned char, 16> outputArr;
  100.     mbedtls_md5_finish_ret(&md5_context, outputArr.data());
  101.  
  102.     std::string result = toHex(outputArr);
  103.  
  104.     std::cout << "Md5 of input is " << std::quoted(result) << "\n";
  105.  
  106.     // Destroy context
  107.     mbedtls_md5_free(&md5_context);
  108.     std::cout << "Hello\n";
  109.  
  110.     return 0;
  111. }
Add Comment
Please, Sign In to add comment