.TH "FBB::HMacBuf" "3bobcat" "2005\-2023" "libbobcat\-dev_6\&.04\&.00" "Compute HMAC Message Digests" .PP .SH "NAME" FBB::HMacBuf \- Computes HMAC Message Digests from information inserted into a std::ostream .PP .SH "SYNOPSIS" \fB#include \fP .br Linking option: \fI \-lbobcat \-lcrypto\fP .PP .SH "DESCRIPTION" \fBFBB::HMacBuf\fP objects are \fBstd::streambuf\fP objects that can be used to initialize \fIstd::ostream\fP objects with\&. .PP All information inserted into such a \fIstd::ostream\fP is used to compute a message HMAC\&. .PP All the message digest and cipher algorithms defined by the OpenSSL library that can be selected by name, may be used in combination with \fIHMacBuf\fP objects\&. .PP For the currently supported message digest algorithms issue the command .nf openssl list \-digest\-commands .fi For the currently supported message cipher algorithms issue the command .nf openssl list \-cipher\-commands .fi The defaults used by \fIHMacBuf\fP constructors are the \fIsha256\fP digest algorithm and the \fIaes\-128\-cbc\fP cipher algorithm\&. .PP .SH "NAMESPACE" \fBFBB\fP .br All constructors, members, operators and manipulators, mentioned in this man\-page, are defined in the namespace \fBFBB\fP\&. .PP .SH "INHERITS FROM" \fBstd::streambuf\fP .PP .SH "CONSTRUCTORS" .IP o \fBHMacBuf()\fP: .br The default constructor defines a \fIHmacBuf\fP object\&. It can be used once a non\-default constructed \fIHMacBuf\fP object has been move\-assigned to it; .IP .IP o \fBHMacBuf(HMacBuf &&tmp)\fP: .br The move constructor initialized a \fIHmacBuf\fP object by moving \fItmp\fP into the current \fIHMacBuf\fP object; .IP .IP o \fBHMacBuf(std::string const &key, char const *digest, size_t bufsize)\fP: .br This constructor initializes the streambuf, setting it up for the message digest algorithm specified with \fIdigest\fP\&. E\&.g\&., to use the sha256 algorithm specify \fI\(dq\&sha256\(dq\&\fP\&. .IP The constructor\(cq\&s first argument defines the key to be used when computing the HMAC message digest\&. The key\(cq\&s length must be 16 characters\&. An exception is thrown if an empty key is specified\&. .IP The \fIbufsize\fP argument specifies the size (in bytes) of the internal buffer used by \fIHMacBuf\fP to store incoming characters temporarily\&. A value of 1024 should be OK for all normal cases; .IP .IP o \fBHMacBuf(std::string const &key, char const *cipher = \(dq\&aes\-128\-cbc\(dq\&, char const *digest = \(dq\&sha256\(dq\&, size_t bufsize = 1024)\fP: .br Like the previous constructor, but this one offers defaults for the cipher and digest algorithms and buffer size\&. When specifying another cipher algorithm the key length must match the cipher requirement\&. Usually the cipher\(cq\&s name contains a number (like 128), which can be divided by 8 to obtain the required key length of fixed\-key length ciphers\&. .PP .SH "OVERLOADED OPERATOR" .PP .IP o \fBHMacBuf &operator=(HMacBuf &&rhs)\fP: .br The move assignment operator moves the \fIrhs HMacBuf\fP object into the current object; .IP .IP o \fBstd::ostream &operator<<(std::ostream &out, HMacBuf const &hmacbuf)\fP: .br The insertion operator is a free function defined in the namespace \fIFBB\fP\&. It inserts a hash value as a series of hexadecimally displayed values into the provided \fIostream\fP\&. See the example below for an illustration\&. .PP .SH "MEMBER FUNCTIONS" .PP All members of \fBstd::streambuf\fP are available, as \fBFBB::HMacBuf\fP inherits from this class\&. .PP .IP o \fBstd::string const &hash() const\fP: .br This member returns the hash value computed by the \fIHMacBuf\fP object\&. Its value is only defined after having called \fIclose()\fP\&. The hash value is returned in a \fIstd::string\fP object\&. This string\(cq\&s \fIlength()\fP member contains the number of characters used by the hash value, and its \fIdata()\fP member refers to the hash value\(cq\&s characters\&. Note that a hash value\(cq\&s character value may be 0 (not to be confused with \fI\(cq\&0\(cq\&\fP)\&. .IP When called from a default constructed \fIHMacBuf\fP object an empty string is returned; .IP .IP o \fBvoid reset()\fP: .br This member reinitializes the message hmac computation\&. One a message hmac has been computed for, say a stream \fIstreamA\fP this member can be called after which the hmac for a stream \fIstreamB\fP can be computed using the same \fIHMacBuf\fP object\&. .IP No action is performed When called from a default constructed \fIHMacBuf\fP object; .IP .IP o \fBvoid eoi()\fP: .br This member can be called to complete the message digest computation\&. Instead of calling this member the \fIeoi\fP manipulator (see below) can be used\&. .PP .SH "MANIPULATOR" .IP o \fBFBB::eoi\fP: .br The \fIeoi\fP manipulator can be inserted into the \fIostream\fP to complete the digest computation\&. If it is inserted into a plain \fIstd::ostream\fP nothing happens\&. .IP \fIeoi\fP can also be called as a function, receiving the stream that uses the \fIHMacBuf\fP as its \fIstreambuf\fP, but it must be called either way as the \fIHMacBuf\fP object itself cannot decide whether all information to compute the digest for has yet been received or not\&. The general approach for computing a message hmac is therefore: .nf 1\&. create a HMacBuf object 2\&. use it to create a std::ostream object 3\&. insert information into the ostream object 4\&. call the HMacBuf object\(cq\&s eoi() member or insert eoi into the ostream object 5\&. obtain/process the hash value from the HMacBuf object\&. .fi .PP .SH "EXAMPLE" .nf #include #include #include using namespace std; using namespace FBB; int main(int argc, char **argv) try { // using the default (sha256) digest algorithm if (argc == 1) throw Exception{} << \(dq\&Usage: arg1: 16\-byte key, arg2: file to process,\en\(dq\& \(dq\& arg3 (opt) buf size (default 1024)\(dq\&; HMacBuf hmacbuf{ argv[1], \(dq\&aes\-128\-cbc\(dq\&, \(dq\&sha256\(dq\&, argc == 3 ? 1024 : stoul(argv[3]) }; HMacBuf empty; // Demo: an empty HMacBuf empty = HMacBuf{ argv[1], \(dq\&sha256\(dq\&, 100 }; // Demo: move assignmeent ostream out(&hmacbuf); // the ostream receiving the // input to compute the hmac of ifstream in{ argv[2] }; // the file to process out << in\&.rdbuf() << eoi; // compute the hmac // and show the hmac value cout << \(dq\& computed hmac value: >\(dq\& << hmacbuf << \(dq\&<\en\(dq\&; in\&.seekg(0); // to illustrate \(cq\&reset\(cq\&: do it hmacbuf\&.reset(); // again out << in\&.rdbuf(); eoi(out); // calling eoi as a function cout << \(dq\&recomputed hmac value: >\(dq\& << hmacbuf << \(dq\&<\en\(dq\&; } catch(exception const &err) { cout << err\&.what() << endl; return errno; } .fi .PP .SH "FILES" \fIbobcat/hmacbuf\fP \- defines the class interface .PP .SH "SEE ALSO" \fBbobcat\fP(7), \fBdigestbuf\fP(3bobcat), \fBstd::streambuf\fP .PP .SH "BUGS" None reported .PP .SH "BOBCAT PROJECT FILES" .PP .IP o \fIhttps://fbb\-git\&.gitlab\&.io/bobcat/\fP: gitlab project page; .IP o \fIbobcat_6\&.04\&.00\-x\&.dsc\fP: detached signature; .IP o \fIbobcat_6\&.04\&.00\-x\&.tar\&.gz\fP: source archive; .IP o \fIbobcat_6\&.04\&.00\-x_i386\&.changes\fP: change log; .IP o \fIlibbobcat1_6\&.04\&.00\-x_*\&.deb\fP: debian package containing the libraries; .IP o \fIlibbobcat1\-dev_6\&.04\&.00\-x_*\&.deb\fP: debian package containing the libraries, headers and manual pages; .PP .SH "BOBCAT" Bobcat is an acronym of `Brokken\(cq\&s Own Base Classes And Templates\(cq\&\&. .PP .SH "COPYRIGHT" This is free software, distributed under the terms of the GNU General Public License (GPL)\&. .PP .SH "AUTHOR" Frank B\&. Brokken (\fBf\&.b\&.brokken@rug\&.nl\fP)\&. .PP