Skip to main content

create_mte_states

/// <summary>
/// Creates the mte states.
/// </summary>
/// <param name="personal">The personal.</param>
/// <param name="entropy">The entropy.</param>
/// <param name="nonce">The nonce.</param>
/// <returns>ResponseModel.</returns>
private static ResponseModel CreateMteStates(string personal,
byte[] encoderEntropy,
byte[] decoderEntropy,
ulong nonce)
{
ResponseModel response = new ResponseModel();
try
{
//--------------------
// Create MTE Encoder
//--------------------
MteEnc encoder = new MteEnc();
encoder.SetEntropy(encoderEntropy);
encoder.SetNonce(nonce);
MteStatus status = encoder.Instantiate(personal);
if (status != MteStatus.mte_status_success)
{
Console.WriteLine($"Error creating encoder: Status: " +
"{encoder.GetStatusName(status)} / {encoder.GetStatusDescription(status)}");
response.Message =
$"Error creating encoder: Status: " +
"{encoder.GetStatusName(status)} / {encoder.GetStatusDescription(status)}";
response.ResultCode = Constants.RC_MTE_ENCODE_EXCEPTION;
response.Success = false;
return response;
}

//------------------------
// Save and encrypt state
//------------------------
var encoderState = encoder.SaveStateB64();
var encryptedEncState = _enc.Encrypt(encoderState, personal, _encIV);
Constants.MteClientState.Store($"{Constants.EncoderPrefix}{personal}",
encryptedEncState,
TimeSpan.FromMinutes(Constants.ExpireMinutes));

//--------------------
// Create MTE Decoder
//--------------------
MteDec decoder = new MteDec();
decoder.SetEntropy(decoderEntropy);
decoder.SetNonce(nonce);
status = decoder.Instantiate(personal);
if (status != MteStatus.mte_status_success)
{
Console.WriteLine($"Error creating decoder: Status: " +
"{decoder.GetStatusName(status)} / {decoder.GetStatusDescription(status)}");
response.Message =
$"Error creating decoder: Status: " +
"{decoder.GetStatusName(status)} / {decoder.GetStatusDescription(status)}";
response.ResultCode = Constants.RC_MTE_DECODE_EXCEPTION;
response.Success = false;
return response;
}

//------------------------
// Save and encrypt state
//------------------------
var decodeState = decoder.SaveStateB64();
var encryptedDecState = _enc.Encrypt(decodeState, personal, _encIV);

Constants.MteClientState.Store($"{Constants.DecoderPrefix}{personal}",
encryptedDecState,
TimeSpan.FromMinutes(Constants.ExpireMinutes));
response.Success = true;
response.ResultCode = Constants.RC_SUCCESS;
response.Message = Constants.STR_SUCCESS;
}
catch (Exception ex)
{
response.Message = $"Exception creating MTE state. Ex: {ex.Message}";
response.ResultCode = Constants.RC_MTE_ENCODE_EXCEPTION;
response.Success = false;
}

return response;
}