ArraySink

From Crypto++ Wiki
Jump to: navigation, search

An ArraySink, introduced in version 5.6 of Crypto++, is a sink for byte arrays. An ArraySink is typedef'd from a StringSink's third constructor. Prior to version 5.6 of the library, use a StringSink.

An ArraySink functions like any other sinks in the library, including a FileSink and StringSink.

ArraySink
Documentation
#include <cryptopp/filters.h>

Constructor

ArraySink (byte *buffer,
           size_t size)

buffer is a byte array.

size is the length of the byte array.

Example

Below is an example of storing Adler32 digest in a std::array using a pipeline.

Adler32 hash;
std::array<byte, Adler32::DIGESTSIZE> output;
StringSource ss("Wikipedia", true,
                   new HashFilter( hash, 
                       new ArraySink(output.data(), Adler32::DIGESTSIZE)
                   )
               );

for(auto &it: output)
{
    std::cout << std::hex << (int)it << " ";
}

The output is shown below.

11 E6 03 98

If you overcommit the size of the array, then you can get the size of the bytes written to the array with the following. Notice three things. First, a stack based ArraySink was used. Second, TotalPutLength was used to determine how many bytes were written to the array. Third, a Redirector was used to stop ownership so the ArraySink would survive to allow the call to TotalPutLength.

Adler32 hash;
byte output[1024];
ArraySink as(output, sizeof(output));

StringSource ss("Wikipedia", true,
                   new HashFilter( hash, 
                       new Redirector(as)
                   )
               );

for(size_t i = 0; i < as.TotalPutLength(); i++)
{
    std::cout << std::hex << (int)output[i] << " ";
}