Using the sha1 class
The sha1 class provides a SHA1 implementation which produces a 160-bit (20-byte) hash.
The sha1 class inherits from the hash class and shares a common interface with the other hash classes: append() to feed in data, getHash() to retrieve the hash, getHashSize() to get the hash length in bytes, and clear() to reset. The isSupported() method can be used to determine if the hash algorithm is available on the current platform.
The following example hashes a string, prints the result in hexadecimal, clears the hash, and hashes a different string.
#include <rudiments/sha1.h>
#include <rudiments/charstring.h>
#include <rudiments/stdio.h>
int main(int argc, const char **argv) {
sha1 h;
// check whether sha1 is supported
if (!h.isSupported()) {
stdoutput.write("sha1 is not supported\n");
return 1;
}
// hash some data
const char *data="hello world";
h.append((const byte_t *)data,charstring::getLength(data));
// print the hash
const byte_t *hashval=h.getHash();
uint64_t hashsize=h.getHashSize();
stdoutput.printf("sha1(\"%s\") = ",data);
for (uint64_t i=0; i<hashsize; i++) {
stdoutput.printf("%02x",hashval[i]);
}
stdoutput.write('\n');
// clear and hash different data
h.clear();
data="goodbye world";
h.append((const byte_t *)data,charstring::getLength(data));
hashval=h.getHash();
hashsize=h.getHashSize();
stdoutput.printf("sha1(\"%s\") = ",data);
for (uint64_t i=0; i<hashsize; i++) {
stdoutput.printf("%02x",hashval[i]);
}
stdoutput.write('\n');
}