Async 1.8.0
Async::Digest Class Reference

A_brief_class_description. More...

#include <AsyncDigest.h>

Public Types

using Signature = std::vector<uint8_t>
 
using MsgDigest = std::vector<uint8_t>
 

Public Member Functions

 Digest (void)
 Default constructor.
 
 Digest (const Digest &)=delete
 Disallow copy construction.
 
Digestoperator= (const Digest &)=delete
 Disallow copy assignment.
 
 ~Digest (void)
 Destructor.
 
bool mdInit (const std::string &md_alg)
 A_brief_member_function_description.
 
bool mdUpdate (const void *d, size_t dlen)
 
template<class T >
bool mdUpdate (const T &d)
 
bool mdFinal (MsgDigest &md)
 
MsgDigest mdFinal (void)
 
bool md (MsgDigest &digest, const std::string &md_alg, const void *d, size_t dlen)
 
template<class T >
bool md (MsgDigest &digest, const std::string &md_alg, const T &d)
 
template<class T >
MsgDigest md (const std::string &md_alg, const T &d)
 
bool signInit (const std::string &md_alg, SslKeypair &pkey)
 
bool signUpdate (const void *msg, size_t mlen)
 
template<class T >
bool signUpdate (const T &msg)
 
bool signFinal (Signature &sig)
 
Signature signFinal (void)
 
bool sign (Signature &sig, const void *msg, size_t mlen)
 
template<class T >
bool sign (Signature &sig, const T &msg)
 
Signature sign (const void *msg, size_t mlen)
 
template<class T >
Signature sign (const T &msg)
 
bool signVerifyInit (const std::string &md_alg, SslKeypair &pkey)
 
bool signVerifyUpdate (const void *msg, size_t mlen)
 
template<class T >
bool signVerifyUpdate (const T &msg)
 
bool signVerifyFinal (const Signature &sig)
 
bool signVerify (const Signature &sig, const void *msg, size_t mlen)
 
template<class T >
bool signVerify (const Signature &sig, const T &msg)
 

Static Public Member Functions

static bool sigEqual (const Signature &s1, const Signature &s2)
 

Detailed Description

A_brief_class_description.

Author
Tobias Blomberg / SM0SVX
Date
2024-04-27

A_detailed_class_description

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <iterator>
#include <AsyncDigest.h>
int main()
{
const std::string msg("The quick brown fox jumps over the lazy dog");
const std::string md_algorithm("sha256");
const std::string private_key_pem =
"-----BEGIN PRIVATE KEY-----\n"
"MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA9hMmwek/t6lsQ4P1\n"
"mouGSfnKeeJKgQ7V10pF6eLbtgke5bGpvObJpmOC4rhBcvUWRM26fAtN28UB1uTs\n"
"lQoprwIDAQABAkAqeE21I/uiSDRuRqUqAjCwLdN7S8oOEjBoEuKUJlpDRWMTmUIi\n"
"jz5KUF8dKFUESIBr4wm0eFwvEQ3Hc0s+NOxZAiEA+2iLdJjOCIM1Cf/GWS+Zm7VE\n"
"Jigcxb4lmkCp6SieMvUCIQD6katuM9cwxciRyxRw5//eIul75UF3W5YD9+O5uDzr\n"
"kwIhAJAihtlJBc5hktXxuwDExnc7vB94Hc7MzfganI8dB13FAiEAzz85Kdda/443\n"
"jM8JwzFA4rzBnaZLdauc8v9PrccDLF0CIQD4IW5bAfBBCNozBj1937NPMGNF+Jdf\n"
"5bCIctxqutH20w==\n"
"-----END PRIVATE KEY-----\n";
const std::string public_key_pem =
"-----BEGIN PUBLIC KEY-----\n"
"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAPYTJsHpP7epbEOD9ZqLhkn5ynniSoEO\n"
"1ddKReni27YJHuWxqbzmyaZjguK4QXL1FkTNunwLTdvFAdbk7JUKKa8CAwEAAQ==\n"
"-----END PUBLIC KEY-----\n";
Async::SslKeypair private_key;
if (!private_key.privateKeyFromPem(private_key_pem))
{
std::cout << "*** ERROR: Async::Digest::privateKeyFromPem() failed"
<< std::endl;
return 1;
}
std::cout << private_key.privateKeyPem() << std::endl;
Async::SslKeypair public_key;
if (!public_key.publicKeyFromPem(public_key_pem))
{
std::cout << "*** ERROR: Async::Digest::publicKeyFromPem() failed"
<< std::endl;
return 1;
}
std::cout << public_key.publicKeyPem() << std::endl;
if (!sdgst.signInit(md_algorithm, private_key))
{
std::cout << "*** ERROR: Async::Digest::signInit() failed" << std::endl;
return 1;
}
const auto sig = sdgst.sign(msg);
if (sig.empty())
{
std::cout << "*** ERROR: Async::Digest::sign() failed" << std::endl;
return 1;
}
std::cout << "Signature size: " << sig.size() << std::endl;
size_t cnt = 0;
for (const auto& byte : sig)
{
std::cout << std::setfill('0') << std::setw(2) << std::hex
<< unsigned(byte) << " ";
if (++cnt % 16 == 0)
{
std::cout << std::endl;
}
}
std::cout << std::endl;
if (!vdgst.signVerifyInit(md_algorithm, public_key))
{
std::cout << "*** ERROR: Async::Digest::signVerifyInit() failed"
<< std::endl;
return 1;
}
bool verify_ok = vdgst.signVerify(sig, msg);
std::cout << "Verify: " << (verify_ok ? "OK" : "FAIL") << "\n" << std::endl;
if (!dgst.mdInit("sha256"))
{
std::cout << "*** ERROR: Async::Digest::init() failed"
<< std::endl;
return 1;
}
if (!dgst.mdUpdate(msg))
{
std::cout << "*** ERROR: Async::Digest::update() failed"
<< std::endl;
return 1;
}
const auto sha256sum = dgst.mdFinal();
if (sha256sum.empty())
{
std::cout << "*** ERROR: Async::Digest::final() failed"
<< std::endl;
return 1;
}
std::cout << "SHA256SUM:" << std::endl;
cnt = 0;
for (const auto& byte : sha256sum)
{
std::cout << std::setfill('0') << std::setw(2) << std::hex
<< unsigned(byte) << " ";
if (++cnt % 16 == 0)
{
std::cout << std::endl;
}
}
std::cout << std::endl;
return 0;
}
Calculate digests and signatures.
A_brief_class_description.
bool signVerify(const Signature &sig, const void *msg, size_t mlen)
bool mdFinal(MsgDigest &md)
bool sign(Signature &sig, const void *msg, size_t mlen)
bool signInit(const std::string &md_alg, SslKeypair &pkey)
bool signVerifyInit(const std::string &md_alg, SslKeypair &pkey)
A class representing private and public keys.
std::string publicKeyPem(void) const
Get the public key on PEM form.
bool privateKeyFromPem(const std::string &pem)
Create key from the given PEM data.
bool publicKeyFromPem(const std::string &pem)
Create public key from PEM string.
std::string privateKeyPem(void) const
Return the private key on PEM form.
Examples
AsyncDigest_demo.cpp.

Definition at line 123 of file AsyncDigest.h.

Member Typedef Documentation

◆ MsgDigest

using Async::Digest::MsgDigest = std::vector<uint8_t>

Definition at line 127 of file AsyncDigest.h.

◆ Signature

using Async::Digest::Signature = std::vector<uint8_t>

Definition at line 126 of file AsyncDigest.h.

Constructor & Destructor Documentation

◆ Digest() [1/2]

Async::Digest::Digest ( void )
inline

Default constructor.

Definition at line 138 of file AsyncDigest.h.

◆ Digest() [2/2]

Async::Digest::Digest ( const Digest & )
delete

Disallow copy construction.

◆ ~Digest()

Async::Digest::~Digest ( void )
inline

Destructor.

Definition at line 172 of file AsyncDigest.h.

Member Function Documentation

◆ md() [1/3]

template<class T >
MsgDigest Async::Digest::md ( const std::string & md_alg,
const T & d )
inline

Definition at line 258 of file AsyncDigest.h.

References md().

◆ md() [2/3]

template<class T >
bool Async::Digest::md ( MsgDigest & digest,
const std::string & md_alg,
const T & d )
inline

Definition at line 252 of file AsyncDigest.h.

References md().

◆ md() [3/3]

bool Async::Digest::md ( MsgDigest & digest,
const std::string & md_alg,
const void * d,
size_t dlen )
inline

Definition at line 245 of file AsyncDigest.h.

References mdFinal(), mdInit(), and mdUpdate().

Referenced by md(), md(), mdFinal(), mdInit(), signInit(), and signVerifyInit().

◆ mdFinal() [1/2]

bool Async::Digest::mdFinal ( MsgDigest & md)
inline
Examples
AsyncDigest_demo.cpp.

Definition at line 222 of file AsyncDigest.h.

References md().

◆ mdFinal() [2/2]

MsgDigest Async::Digest::mdFinal ( void )
inline

Definition at line 238 of file AsyncDigest.h.

References mdFinal().

Referenced by md(), and mdFinal().

◆ mdInit()

bool Async::Digest::mdInit ( const std::string & md_alg)
inline

A_brief_member_function_description.

Parameters
param1Description_of_param1
Returns
Return_value_of_this_member_function

Definition at line 184 of file AsyncDigest.h.

References md().

Referenced by md().

◆ mdUpdate() [1/2]

template<class T >
bool Async::Digest::mdUpdate ( const T & d)
inline

Definition at line 217 of file AsyncDigest.h.

References mdUpdate().

◆ mdUpdate() [2/2]

bool Async::Digest::mdUpdate ( const void * d,
size_t dlen )
inline

Definition at line 204 of file AsyncDigest.h.

Referenced by md(), and mdUpdate().

◆ operator=()

Digest & Async::Digest::operator= ( const Digest & )
delete

Disallow copy assignment.

◆ sigEqual()

static bool Async::Digest::sigEqual ( const Signature & s1,
const Signature & s2 )
inlinestatic

Definition at line 129 of file AsyncDigest.h.

◆ sign() [1/4]

template<class T >
Signature Async::Digest::sign ( const T & msg)
inline

Definition at line 380 of file AsyncDigest.h.

References sign().

◆ sign() [2/4]

Signature Async::Digest::sign ( const void * msg,
size_t mlen )
inline

Definition at line 372 of file AsyncDigest.h.

References sign().

◆ sign() [3/4]

template<class T >
bool Async::Digest::sign ( Signature & sig,
const T & msg )
inline

Definition at line 367 of file AsyncDigest.h.

References sign().

◆ sign() [4/4]

bool Async::Digest::sign ( Signature & sig,
const void * msg,
size_t mlen )
inline
Examples
AsyncDigest_demo.cpp.

Definition at line 336 of file AsyncDigest.h.

References signFinal(), and signUpdate().

Referenced by sign(), sign(), and sign().

◆ signFinal() [1/2]

bool Async::Digest::signFinal ( Signature & sig)
inline

Definition at line 306 of file AsyncDigest.h.

◆ signFinal() [2/2]

Signature Async::Digest::signFinal ( void )
inline

Definition at line 329 of file AsyncDigest.h.

References signFinal().

Referenced by sign(), and signFinal().

◆ signInit()

bool Async::Digest::signInit ( const std::string & md_alg,
SslKeypair & pkey )
inline
Examples
AsyncDigest_demo.cpp.

Definition at line 266 of file AsyncDigest.h.

References md().

◆ signUpdate() [1/2]

template<class T >
bool Async::Digest::signUpdate ( const T & msg)
inline

Definition at line 301 of file AsyncDigest.h.

References signUpdate().

◆ signUpdate() [2/2]

bool Async::Digest::signUpdate ( const void * msg,
size_t mlen )
inline

Definition at line 287 of file AsyncDigest.h.

Referenced by sign(), and signUpdate().

◆ signVerify() [1/2]

template<class T >
bool Async::Digest::signVerify ( const Signature & sig,
const T & msg )
inline

Definition at line 443 of file AsyncDigest.h.

References signVerify().

◆ signVerify() [2/2]

bool Async::Digest::signVerify ( const Signature & sig,
const void * msg,
size_t mlen )
inline
Examples
AsyncDigest_demo.cpp.

Definition at line 431 of file AsyncDigest.h.

References signVerifyFinal(), and signVerifyUpdate().

Referenced by signVerify().

◆ signVerifyFinal()

bool Async::Digest::signVerifyFinal ( const Signature & sig)
inline

Definition at line 425 of file AsyncDigest.h.

Referenced by signVerify().

◆ signVerifyInit()

bool Async::Digest::signVerifyInit ( const std::string & md_alg,
SslKeypair & pkey )
inline
Examples
AsyncDigest_demo.cpp.

Definition at line 385 of file AsyncDigest.h.

References Async::SslKeypair::isNull(), and md().

◆ signVerifyUpdate() [1/2]

template<class T >
bool Async::Digest::signVerifyUpdate ( const T & msg)
inline

Definition at line 420 of file AsyncDigest.h.

References signVerifyUpdate().

◆ signVerifyUpdate() [2/2]

bool Async::Digest::signVerifyUpdate ( const void * msg,
size_t mlen )
inline

Definition at line 405 of file AsyncDigest.h.

Referenced by signVerify(), and signVerifyUpdate().


The documentation for this class was generated from the following file: