Class oSHA256
Introduction
This class implements 256-bit encryption according to FIPS180-2 sec 5.3.2 by converting variable-length input to a fixed-length, 32-byte digest. The class is analagous to a file, allowing an unlimited amount of data may be written but only a fixed-length amount of data may be read and each read rewinds the file.
This class is declared in oSHA256.hpp and defined in oSHA256.cpp.
Inheritance
None.
Dependence
None.
Properties
oSHA256::DigestLength
unsigned DigestLength
Return the digest length in bytes. The digest length is 32. This property is implemented as a constant and so parenthesis should be omitted when referencing it, despite what is shown.
Methods
oSHA256::Fetch
oSHA256 & Fetchvoid * memory
End the current encryption cycle and copy the digest to external memory. The digest length is DigestLength bytes so no length argument is needed. Automatically, initialize the object instance for another encryption cycle once the digest has been copied.
oSHA256::Write
oSHA256 & Writevoid const * memorysize_t extent
Encrypt a block of memory. Return the object instance address. This method may be called any number of times with any data of any length. Data encryption is cumulative such that data may be encrypted in one large block or many small ones. All writes prior to a Fetch comprise an encryption cycle
oSHA256::Reset
oSHA256 & Resetvoid
Initialize the class instance in preparation for another encryption cycle. Return the object instance reference. This discards the computed digest in the process. If the digest is needed then call method Fetch, instead.
Examples
Computing an SHA256 Digest
oSHA256 encoder;
uint8_t digest [encoder.DigestLength];
char buffer [1024];
signed length;
signed fd;
while ((length = read (fd, buffer, sizeof (buffer))) > 0)
{
encoder.Write (buffer, length);
}
encoder.Fetch (digest);
This example computes the SHA256 digest for an entire file. An encoder is instantiated and a digest buffer is reserved. As each buffer is read from file, it is encrypted using the Write method and, at the end, the digest is obtained using the Fetch method.
The file content is not important. It may be either text or a binary. The computed digest will, for all practical purposes, be unique and may serve as the file finger-print. Therefore, two files having the same digest are, in all probability, identical.