ChannelSwitch

From Crypto++ Wiki

Jump to: navigation, search
ChannelSwitch.png

The ChannelSwitch class can be used to enable Read-Once Write-Many functionality into a program.

[edit] Usage

The logical view when using the ChannelSwitch class is depicted below with a File/Hash Combination.

ChannelSwitchLogical.png

The code below does not use (a bit of hand waving here):

channel->AddDefaultRoute( filterSHA1 ( new HexEncoder ( digest ) ) );

because only the last result would be obtainable due to reuse of digest. The SignerVerifier class is created on the Stack also since one generally desires the DecodingResult.

[edit] Example

MD5  hashMD5;
HashFilter filterMD5(hashMD5);

SHA1 hashSHA1;
HashFilter filterSHA1(hashSHA1);

std::auto_ptr<ChannelSwitch> channel( new ChannelSwitch );

channel->AddDefaultRoute(filterMD5);
channel->AddDefaultRoute(filterSHA1);

StringSource( "abcdefghijklmnopqrstuvwxyz",
              true, channel.release());

string digest;
HexEncoder encoder( new StringSink( digest ),
                    true /* uppercase */ ); 

filterMD5.TransferTo( encoder );
cout << filterMD5.AlgorithmName() << ": " << digest << endl;
digest.erase();

filterSHA1.TransferTo( encoder );
cout << filterSHA1.AlgorithmName() << ": " << digest << endl;
digest.erase();

[edit] Sample

ChannelSwitch.zip - Demonstrates using the ChannelSwitch class with a FileSource and multiple Hash objects - 7.9 kB

Personal tools