About
The maxon::StreamConversionInterface provides a generic interface for any kind of data conversion. This includes string encoding, compression, encryption, hashing etc.
StreamConversionInterface
Conversion
maxon::StreamConversionInterface is used to convert the given source data. The result of this conversion is typically stored in a given array.
- Note
- A maxon::StreamConversionInterface object can be used only once.
const maxon::StreamConversionRef base64encoder = maxon::StreamConversions::Base64Encoder().Create()
iferr_return;
base64encoder.Convert(source, destination, targetSizeEstimation,
true, lastPart)
iferr_return;
const maxon::StreamConversionRef base64encoder = maxon::StreamConversions::Base64Encoder().Create()
iferr_return;
base64encoder.ConvertAll(source, destination, targetSizeEstimation)
iferr_return;
Streams
A given conversion instance can be converted to an input stream. See InputStream Manual.
maxon::InputStreamRef inputStream = maxon::InputStreamInterface::FromBlock().Create(memblock,
false)
iferr_return;
const maxon::StreamConversionRef base64encoder = maxon::StreamConversions::Base64Encoder().Create()
iferr_return;
inputStream = base64encoder.ConvertToStream(inputStream)
iferr_return;
Properties
Various properties of the given conversion type can be accessed with:
{
const maxon::Id decoderID = encoder.GetCounterpart();
if (decoder.GetSourceType() != charDataType)
if (decoder.GetDestinationType() != charDataType)
}
Conversions
Existing conversions are registered at the registry maxon::StreamConversions or presented as published objects. Some conversions can be configured by defining some settings in a maxon::DataDictionary that is used with the Create()
function (maxon::StreamConversionFactory).
Hex
Arbitrary data can be converted to a hexadecimal representation:
- maxon::StreamConversions::HexEncoder: Hexadecimal encoder.
- maxon::StreamConversions::HexDecoder: Hexadecimal decoder.
See also maxon::GetHexadecimalValue().
const maxon::StreamConversionRef encoder = maxon::StreamConversions::HexEncoder().Create()
iferr_return;
UTF
Text can be converted to different Unicode encodings. See maxon::UTFTEXT_OPTIONS.
- maxon::StreamConversions::UtfTextEncoder: Unicode encoder.
- maxon::StreamConversions::UtfTextDecoder: Unicode decoder.
Additional string conversions are registered at maxon::StringEncodings and maxon::StringDecodings, see stringencoding.h
. For string conversions there are also maxon::StringEncodingInterface and maxon::StringDecodingInterface.
Base64
Arbitrary data can be converted to a Base64 representation. See maxon::BASE64_OPTIONS.
- maxon::StreamConversions::Base64Encoder: Base64 encoder.
- maxon::StreamConversions::Base64Decoder: Base64 decoder.
- maxon::StreamConversions::Base64UrlEncoder: Base64 URL encoder.
- maxon::StreamConversions::Base64UrlDecoder: Base64 URL decoder.
{
const maxon::StreamConversionRef encoder = maxon::StreamConversions::Base64Encoder().Create()
iferr_return;
}
{
const maxon::StreamConversionRef decoder = maxon::StreamConversions::Base64Decoder().Create()
iferr_return;
}
Cryptography
Arbitrary data can be encrypted using these algorithms:
- maxon::StreamConversions::AesEncoder: AES encryption.
- maxon::StreamConversions::AesDecoder: AES decryption.
- maxon::StreamConversions::BlowfishEncoder: Blowfish encryption.
- maxon::StreamConversions::BlowfishDecoder: Blowfish decryption.
- maxon::StreamConversions::BlowfishLegacyEncoder: Legacy Blowfish encryption.
- maxon::StreamConversions::BlowfishLegacyDecoder: Legacy Blowfish decryption.
- maxon::StreamConversions::BlowfishLegacyEnDecoder: Legacy Blowfish en/decryption. See maxon::BLOWFISHLEGACYENDECODER_OPTIONS.
The cryptographic key is set as maxon::CryptographyOptions::CRYPTOKEY using maxon::CryptoKeyInterface. To pad a data block with arbitrary data use maxon::SecureRandom.
Encryption algorithms can also be used with the specialized maxon::CryptographyStreamConversionInterface.
const maxon::Id encoderID = maxon::StreamConversions::AesEncoder.GetId();
const maxon::Char* key =
"9USGxEPo0Tx6d8ZRRLbpEc4D88xdU2bb";
maxon::DataDictionary cryptoSettings;
cryptoSettings.Set(maxon::CryptographyOptions::CRYPTOKEY, cryptoKey)
iferr_return;
{
const maxon::StreamConversionRef aesEncoder = maxon::StreamConversions::AesEncoder().Create(cryptoSettings)
iferr_return;
const maxon::Int aesBlockSize = aesEncoder.GetBlockSize();
const maxon::Int targetSize = ((initialSize / blockSize) + 1) * aesBlockSize;
if (diff > 0)
{
const auto randomDataBlock = maxon::ToBlock<maxon::UChar>(randomDataStart, randomDataSize);
}
if (!aesEncoder.SupportInplaceConversion())
}
{
const maxon::StreamConversionRef aesDecoder = maxon::StreamConversions::AesDecoder().Create(cryptoSettings)
iferr_return;
if (!aesDecoder.SupportInplaceConversion())
}
Hashes
Hash values for arbitrary data are calculated with these algorithms:
- maxon::StreamConversions::HashMD5: MD5 hash algorithm.
- maxon::StreamConversions::HashSHA1: SHA1 hash algorithm.
- maxon::StreamConversions::HashSHA256: SHA256 hash algorithm.
- maxon::StreamConversions::HashSHA512: SHA512 hash algorithm.
- maxon::StreamConversions::HashCrc32c: Crc32c hash algorithm. For convenience see also maxon::Crc32C.
- maxon::StreamConversions::HashCrc32zip: Crc32zip hash algorithm.
- maxon::StreamConversions::HashHmac: Hash-based message authentication code. See maxon::HASH_HMAC.
const maxon::StreamConversionRef md5 = maxon::StreamConversions::HashMD5().Create()
iferr_return;
const maxon::StreamConversionRef sha256 = maxon::StreamConversions::HashSHA256().Create()
iferr_return;
For easily handling passwords and hash strings see
auto factory = maxon::StreamConversions::HashSHA256();
Compression
Arbitrary data can be compressed using these algorithms. See datacompression.h:
- Note
- For creating ZIP archives see Archives Manual.
maxon::DataDictionary settings;
// decompress
maxon::BaseArray<maxon::Char> decompressed;
const maxon::StreamConversionRef zipDecoder = maxon::StreamConversions::ZipDecoder().Create(settings) iferr_return;
zipDecoder.ConvertAll(compressed, decompressed) iferr_return;
const maxon::String message(decompressed);
DiagnosticOutput("Uncompressed Message: @", message);
Further Reading