Skip to main content

encoder-create

// The fixed length each MTE packet will be equal.
// To avoid data loss this number must be larger
// than any anticipated input data.
int fixedBytes = 20;

// Create MTE Core Encoder with defaults
MteFlenEnc flenEncoder = new MteFlenEnc(fixedBytes);

// Need entropy, nonce and personalization string
// *** IMPORTANT NOTE ***
// Entropy is critical to security and should be created and
// handled like encryption keys. To permit decoding of encoded
// value, Encoder and Decoder must use identical Entropy, Nonce and
// Personalization String as well as Options . See Developer Guides for more information.
//-----------------------------------------------------------------
// The following personalization string and nonce values are for
// demonstration only and should not be used in a production environment
string personalizationString = "MySecretPersonalizationStringGoesHere";
long nonce = 12345678;

// Check how long of entropy we need.
// This example fills entropy with at string of zeros
// this should NOT be used in a production environment
int entropyMinBytes = flenEncoder.GetDrbgsEntropyMinBytes(flenEncoder.GetDrbg());
string entropy = (entropyMinBytes > 0)
? new String('0', entropyMinBytes)
: string.Empty;

// Set Encoder entropy and nonce
flenEncoder.SetEntropy(Encoding.UTF8.GetBytes(entropy));
flenEncoder.SetNonce(nonce);

// Initialize MTE Encoder
MteStatus encoderStatus = flenEncoder.Instantiate(personalizationString);
if(encoderStatus !=MteStatus.mte_status_success)
{
// MTE cannot continue so handle failure appropriately
// Below is an Example
throw new ApplicationException($"Failed to initialize the MTE Encoder engine." +
$"Status: {flenEncoder.GetStatusName(encoderStatus)} / " +
$"{flenEncoder.GetStatusDescription(encoderStatus)}");
}