Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <array>
- #include <vector>
- #include <string>
- #include <iomanip>
- #include <iostream>
- #include <algorithm>
- #include "mbedtls/md5.h"
- #include "mbedtls/base64.h"
- template<std::size_t N>
- std::string toHex(const std::array<unsigned char, N>& values);
- class Md5Hasher {
- private:
- mbedtls_md5_context md5_context;
- public:
- Md5Hasher() {
- mbedtls_md5_init(&md5_context);
- }
- void prepare() {
- //Prepare context for hashing byte sequence
- mbedtls_md5_starts_ret(&md5_context);
- }
- void update(std::string input) {
- //Process message a bit
- mbedtls_md5_update_ret(&md5_context, reinterpret_cast<const unsigned char*>(input.c_str()), input.size());
- }
- std::string finish() {
- std::array<unsigned char, 16> outputArr;
- mbedtls_md5_finish_ret(&md5_context, outputArr.data());
- return toHex(outputArr);
- }
- ~Md5Hasher() {
- mbedtls_md5_free(&md5_context);
- }
- };
- int main() {
- //Collect input
- std::cout << "Input: ";
- std::string input;
- std::getline(std::cin, input);
- Md5Hasher hasher;
- hasher.prepare();
- hasher.update(input);
- std::cout << "MD5 digest is: " << hasher.finish() << "\n";
- }
- template<std::size_t N>
- std::string toHex(const std::array<unsigned char, N>& values) {
- std::stringstream stream;
- for (int i : values) {
- stream << std::setfill ('0') << std::setw(2)
- << std::hex << i;
- }
- return stream.str();
- }
- auto oldmain() -> int {
- //Setup context
- mbedtls_md5_context md5_context;
- //Initialize Context
- mbedtls_md5_init(&md5_context);
- //Prepare context for hashing byte sequence
- mbedtls_md5_starts_ret(&md5_context);
- std::cout << "Input: ";
- std::string input;
- std::getline(std::cin, input);
- //Process message a bit
- mbedtls_md5_update_ret(&md5_context, reinterpret_cast<const unsigned char*>(input.c_str()), input.size());
- std::array<unsigned char, 16> outputArr;
- mbedtls_md5_finish_ret(&md5_context, outputArr.data());
- std::string result = toHex(outputArr);
- std::cout << "Md5 of input is " << std::quoted(result) << "\n";
- // Destroy context
- mbedtls_md5_free(&md5_context);
- std::cout << "Hello\n";
- return 0;
- }
Add Comment
Please, Sign In to add comment