Skip to content

how to: hash and message digest

amir zamani edited this page Aug 5, 2016 · 1 revision

cryptographic hashes also known as message digests

using namespace mbedcrypto;

// by single shot functions
auto hash_value = make_hash(hash_t::sha256, source_data);
auto hmac_value = hmac::make(hash_t::sha1, key_data, source_data);

std::cout << to_base64(hash_value) << std::end;

// Qt5
QFile f(":/keys/init.sql");
if ( f.open(QFile::readOnly) ) {
    auto qHashValue = make_hash(hash_t::sha512, f.readAll());
    if ( qHashValue != PredefinedHashValue ) {
        // tamper error
    }
}

you can also keep the hash object and reuse it:

hash h0(hash_t::ripemd160);
hash h1(from_string<hash_t>("sha1"));

h1.start();
while ( ... ) {
    h1.update(read_some_data());
}
auto hash_value = h1.finish();

// re-use
h1.start();
h1.update(...); // single or multiple updates
hash_value = h1.finish();

see hash.hpp

Clone this wiki locally