decoder-decode-data
- C
- C++
- CSharp
- Java
- JavaScript
- Swift
- Python
- Go
- PHP
/* There are different functions available to use depending on program needs.
* The versions with b64 in the function name involve base 64 conversion,
* suitable for protocols requiring a string representation.
* Otherwise the default will use raw bytes.
*/
/* Decode the message. */
MTE_SET_DEC_IO(d_args, e_args.encoded, e_args.bytes, decoded);
status = mte_mke_dec_decode_b64(decoder, &d_args);
if (mte_base_status_is_error(status))
{
fprintf(stderr, "Decode error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return status;
}
else if (status != mte_status_success)
{
fprintf(stderr, "Decode warning (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
}
The timestamp callback.
static MTE_UINT64_T t_cb(void* context)
{
char line[80];
(void)context;
/* Get the timestamp. */
printf("Enter the timestamp (decimal digits only)> ");
fflush(stdout);
(void)!fgets(line, sizeof(line), stdin);
return strtoul(line, NULL, 10);
}
// There are different functions available to use depending on program needs.
// The versions with b64 in the function name involve base 64 conversion,
// suitable for protocols requiring a string representation.
// Otherwise the default will use raw bytes.
size_t decodedBytes = 0;
// Decode the message.
const void* decoded = decoder.decode(encoded, encodedBytes, decodedBytes, status);
if (MteBase::statusIsError(status))
{
std::cerr << "Decode error ("
<< MteBase::getStatusName(status)
<< "): "
<< MteBase::getStatusDescription(status)
<< std::endl;
return status;
}
else if (status != mte_status_success)
{
std::cerr << "Decode warning ("
<< MteBase::getStatusName(status)
<< "): "
<< MteBase::getStatusDescription(status)
<< std::endl;
}
The timestamp callback.
MTE_UINT64_T Cbs::timestampCallback()
{
// Display a prompt.
std::cout << "Enter the timestamp (decimal digits only)> " << std::flush;
// Get the timestamp and ignore the rest of the line.
MTE_UINT64_T u64;
std::cin >> u64;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return u64;
}
// Use MKE Decoder created in previous example
// Initialize MteStatus
MteStatus decoderStatus;
// Below are 4 different examples of how to decode
// ONLY ONE needs to be used.
// Decode byte[] to a base64 string
string decodedBytesAsString = mkeDecoder.DecodeStr(encodedBytes, out decoderStatus);
if (mkeDecoder.StatusIsError(decoderStatus)) {
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
Console.Error.WriteLine("Decode error ({0}): {1}",
mkeDecoder.GetStatusName(decoderStatus),
mkeDecoder.GetStatusDescription(decoderStatus));
return (int)status;
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
} else if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Decode warning ({0}): {1}",
mkeDecoder.GetStatusName(decoderStatus),
mkeDecoder.GetStatusDescription(decoderStatus));
}
// Decode string to a base64 string
string decodedStringAsString = mkeDecoder.DecodeStrB64(encodedBase64String, out decoderStatus);
if (mkeDecoder.StatusIsError(decoderStatus)) {
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
Console.Error.WriteLine("Decode error ({0}): {1}",
mkeDecoder.GetStatusName(decoderStatus),
mkeDecoder.GetStatusDescription(decoderStatus));
return (int)status;
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
} else if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Decode warning ({0}): {1}",
mkeDecoder.GetStatusName(decoderStatus),
mkeDecoder.GetStatusDescription(decoderStatus));
}
// Decode byte[] to a byte[]
byte[] decodedBytes = mkeDecoder.Decode(encodedBytes, out decoderStatus);
if (mkeDecoder.StatusIsError(decoderStatus)) {
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
Console.Error.WriteLine("Decode error ({0}): {1}",
mkeDecoder.GetStatusName(decoderStatus),
mkeDecoder.GetStatusDescription(decoderStatus));
return (int)status;
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
} else if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Decode warning ({0}): {1}",
mkeDecoder.GetStatusName(decoderStatus),
mkeDecoder.GetStatusDescription(decoderStatus));
}
// Decode string to a byte[]
byte[] decodedStringAsBytes = mkeDecoder.DecodeB64(encodedBase64String, out decoderStatus);
if (mkeDecoder.StatusIsError(decoderStatus)) {
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
Console.Error.WriteLine("Decode error ({0}): {1}",
mkeDecoder.GetStatusName(decoderStatus),
mkeDecoder.GetStatusDescription(decoderStatus));
return (int)status;
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
} else if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Decode warning ({0}): {1}",
mkeDecoder.GetStatusName(decoderStatus),
mkeDecoder.GetStatusDescription(decoderStatus));
}
// Use MKE Decoder created in previous example
// Initialize MteStatus
MteStatus decoderStatus;
// Below are 4 different examples of how to decode
// ONLY ONE needs to be used.
// Decode byte[] to a base64 string
MteBase.StrStatus decoded = mkeDecoder.decodeStr(encodedBytes);
if (MteBase.statusIsError(decoded.status))
{
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
throw new Exception("Failed to decode. Status: "
+ MteBase.getStatusName(decoded.status)+ " / "
+ MteBase.getStatusDescription(decoded.status));
}
else if (decoded.status != MteStatus.mte_status_success)
{
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
System.err.println("Decode warning ("
+ MteBase.getStatusName(decoded.status) + "): "
+ MteBase.getStatusDescription(decoded.status));
}
String decodedBytesAsString = decoded.str;
// Decode string to a base64 string
MteBase.StrStatus decoded = mkeDecoder.decodeStrB64(encodedBase64String);
if (MteBase.statusIsError(decoded.status))
{
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
throw new Exception("Failed to decode. Status: "
+ MteBase.getStatusName(decoded.status)+ " / "
+ MteBase.getStatusDescription(decoded.status));
}
else if (decoded.status != MteStatus.mte_status_success)
{
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
System.err.println("Decode warning ("
+ MteBase.getStatusName(decoded.status) + "): "
+ MteBase.getStatusDescription(decoded.status));
}
String decodedBytesAsString = decoded.str;
// Decode byte[] to a byte[]
MteBase.ArrStatus decoded = mkeDecoder.decode(encodedBytes);
if (MteBase.statusIsError(decoded.status))
{
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
throw new Exception("Failed to decode. Status: "
+ MteBase.getStatusName(decoded.status)+ " / "
+ MteBase.getStatusDescription(decoded.status));
}
else if (decoded.status != MteStatus.mte_status_success)
{
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
System.err.println("Decode warning ("
+ MteBase.getStatusName(decoded.status) + "): "
+ MteBase.getStatusDescription(decoded.status));
}
bytes[] decodedBytesAsBytes = decoded.arr;
// Decode string to a byte[]
MteBase.ArrStatus decoded = mkeDecoder.decodeB64(encodedBase64String, out decoderStatus);
if (MteBase.statusIsError(decoded.status))
{
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
throw new Exception("Failed to decode. Status: "
+ MteBase.getStatusName(decoded.status)+ " / "
+ MteBase.getStatusDescription(decoded.status));
}
else if (decoded.status != MteStatus.mte_status_success)
{
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
System.err.println("Decode warning ("
+ MteBase.getStatusName(decoded.status) + "): "
+ MteBase.getStatusDescription(decoded.status));
}
byte[] decodedStringAsBytes = decoded.arr;
Below are 4 different examples of how to encode. ONLY ONE needs to be used.
// Decode string and return as B64 string
const decoderResult = mkeDecoder.decodeStrB64(str);
if (mkeDecoder.statusIsError(decoderResult.status)) {
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
const status = mkeDecoder.getStatusName(decoderResult.status);
const message = mkeDecoder.getStatusDescription(decoderResult.status);
throw new Error(`Error when MTE decoding data.\n${status}: ${message}`);
return;
} else if (decoderResult.status != MteStatus.mte_status_success) {
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
alert(
"Decode warning (" +
mkeDecoder.getStatusName(decoderResult.status) +
"): " +
mkeDecoder.getStatusDescription(decoderResult.status),
);
}
console.log(`Encode result is: ${decoderResult.str}`);
// Decode string and return as Uint8Array
const decoderResult = mkeDecoder.decoderStr(str);
if (mkeDecoder.statusIsError(decoderResult.status)) {
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
const status = mkeDecoder.getStatusName(decoderResult.status);
const message = mkeDecoder.getStatusDescription(decoderResult.status);
throw new Error(`Error when MTE decoding data.\n${status}: ${message}`);
return;
} else if (decoderResult.status != MteStatus.mte_status_success) {
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
alert(
"Decode warning (" +
mkeDecoder.getStatusName(decoderResult.status) +
"): " +
mkeDecoder.getStatusDescription(decoderResult.status),
);
}
console.log(`Encode result is: ${decoderResult.arr}`);
// Decode Uint8Array and return as b64 string
const decoderResult = mkeDecoder.decodeB64(arr);
if (mkeDecoder.statusIsError(decoderResult.status)) {
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
const status = mkeDecoder.getStatusName(decoderResult.status);
const message = mkeDecoder.getStatusDescription(decoderResult.status);
throw new Error(`Error when MTE decoding data.\n${status}: ${message}`);
return;
} else if (decoderResult.status != MteStatus.mte_status_success) {
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
alert(
"Decode warning (" +
mkeDecoder.getStatusName(decoderResult.status) +
"): " +
mkeDecoder.getStatusDescription(decoderResult.status),
);
}
console.log(`Encode result is: ${decoderResult.str}`);
// Decode Uint8Array and return as Uint8Array
const decoderResult = mkeDecoder.decode(arr);
if (mkeDecoder.statusIsError(decoderResult.status)) {
// Unable to decode, this checks for errors, handle failure appropriately
// Below is only an example
const status = mkeDecoder.getStatusName(decoderResult.status);
const message = mkeDecoder.getStatusDescription(decoderResult.status);
throw new Error(`Error when MTE decoding data.\n${status}: ${message}`);
return;
} else if (decoderResult.status != MteStatus.mte_status_success) {
// This catches Decoder warnings, the decode process was successful
// But depending on the type of security needed these may be ignored
// Please see Developer Guide for more details.
alert(
"Decode warning (" +
mkeDecoder.getStatusName(decoderResult.status) +
"): " +
mkeDecoder.getStatusDescription(decoderResult.status),
);
}
console.log(`Encode result is: ${decoderResult.arr}`);
// There are different functions available to use depending on program needs.
// The versions with b64 in the function name involve base 64 conversion,
// suitable for protocols requiring a string representation.
// Otherwise the default will use raw bytes.
// Decode the message.
let decoded = decoder.decode(Array(encodedRes.encoded))
status = decoded.status
if (MteBase.statusIsError(status)) {
print("Decode error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
return Int32(status.rawValue)
} else if (status != mte_status_success) {
print("Decode warning (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
}
The timestamp callback.
func timestampCallback() -> UInt64 {
// Display a prompt.
print("Enter the timestamp (decimal digits only)> ", terminator: "")
// Get the timestamp and ignore the rest of the line.
return UInt64(readLine()!)!
}
# Use MKE Decoder created in previous example.
# Below are 4 different examples of how to decode.
# ONLY ONE needs to be used.
# Decode byte[] to a base64 string.
(decoded_bytes_as_string, decoder_status) = mke_decoder.decode_str(encoded_bytes)
if MteBase.status_is_error(decoder_status):
# Handle decode failure appropriately.
# Below is only an example.
raise Exception("Failed to decode. Status: {0} / {1}\n".format(MteBase.get_status_name(decoder_status),
MteBase.get_status_description(decoder_status)))
elif decoder_status != MteStatus.mte_status_success:
# This catches Decoder warnings, the decode process was successful
# But depending on the type of security needed these may be ignored
# Please see Developer Guide for more details.
print("Decode warning ({0}): {1}".format(
MteBase.get_status_name(status),
MteBase.get_status_description(status)),
file=sys.stderr)
# Decode string to a base64 string.
(decoded_string_as_string, decoder_status) = mke_decoder.decode_str_b64(encoded_base64_string)
if MteBase.status_is_error(decoder_status):
# Handle decode failure appropriately.
# Below is only an example.
raise Exception("Failed to decode. Status: {0} / {1}\n".format(MteBase.get_status_name(decoder_status),
MteBase.get_status_description(decoder_status)))
elif decoder_status != MteStatus.mte_status_success:
# This catches Decoder warnings, the decode process was successful
# But depending on the type of security needed these may be ignored
# Please see Developer Guide for more details.
print("Decode warning ({0}): {1}".format(
MteBase.get_status_name(status),
MteBase.get_status_description(status)),
file=sys.stderr)
# Decode byte[] to a byte[].
(decoded_bytes, decoder_status) = mke_decoder.decode(encoded_bytes)
if MteBase.status_is_error(decoder_status):
# Handle decode failure appropriately.
# Below is only an example.
raise Exception("Failed to decode. Status: {0} / {1}\n".format(MteBase.get_status_name(decoder_status),
MteBase.get_status_description(decoder_status)))
elif decoder_status != MteStatus.mte_status_success:
# This catches Decoder warnings, the decode process was successful
# But depending on the type of security needed these may be ignored
# Please see Developer Guide for more details.
print("Decode warning ({0}): {1}".format(
MteBase.get_status_name(status),
MteBase.get_status_description(status)),
file=sys.stderr)
# Decode string to a byte[].
(decoded_string_as_bytes, decoder_status) = mke_decoder.decode_b64(encoded_base64_string)
if MteBase.status_is_error(decoder_status):
# Handle decode failure appropriately.
# Below is only an example.
raise Exception("Failed to decode. Status: {0} / {1}\n".format(MteBase.get_status_name(decoder_status),
MteBase.get_status_description(decoder_status)))
elif decoder_status != MteStatus.mte_status_success:
# This catches Decoder warnings, the decode process was successful
# But depending on the type of security needed these may be ignored
# Please see Developer Guide for more details.
print("Decode warning ({0}): {1}".format(
MteBase.get_status_name(status),
MteBase.get_status_description(status)),
file=sys.stderr)
// initialize status
var decoderStatus mte.Status
// Below are 4 different examples of how to decode data
// ONLY ONE needs to be used!!!
// decode byte[] to base64 string
decodedBytesAsString, decodedStatus := mkeDecoder.DecodeStr(encodedBytes)
if mte.StatusIsError(decodedStatus) {
// Handle error correctly
// Below is an example
fmt.Fprintf(os.Stderr, "Decode error (%v): %v\n",
mte.GetStatusName(decodedStatus), mte.GetStatusDescription(decodedStatus))
return int(status)
} else if decodedStatus != mte.Status_mte_status_success {
// Handle warning correctly
// Below is an example
fmt.Fprintf(os.Stderr, "Decode warning (%v): %v\n",
mte.GetStatusName(decodedStatus), mte.GetStatusDescription(decodedStatus))
}
// decode string to base64 string
decodedStringAsString, decodedStatus := mkeDecoder.DecodeStrB64(encodedString)
if mte.StatusIsError(decodedStatus) {
// Handle error correctly
// Below is an example
fmt.Fprintf(os.Stderr, "Decode error (%v): %v\n",
mte.GetStatusName(decodedStatus), mte.GetStatusDescription(decodedStatus))
return int(status)
} else if decodedStatus != mte.Status_mte_status_success {
// Handle warning correctly
// Below is an example
fmt.Fprintf(os.Stderr, "Decode warning (%v): %v\n",
mte.GetStatusName(decodedStatus), mte.GetStatusDescription(decodedStatus))
}
// decode bytes to byte
decodedBytes, decodedStatus := mkeDecoder.Decode(encodedBytes)
if mte.StatusIsError(decodedStatus) {
// Handle error correctly
// Below is an example
fmt.Fprintf(os.Stderr, "Decode error (%v): %v\n",
mte.GetStatusName(decodedStatus), mte.GetStatusDescription(decodedStatus))
return int(status)
} else if decodedStatus != mte.Status_mte_status_success {
// Handle warning correctly
// Below is an example
fmt.Fprintf(os.Stderr, "Decode warning (%v): %v\n",
mte.GetStatusName(decodedStatus), mte.GetStatusDescription(decodedStatus))
}
// decode string to bytes
decodedBytes, decodedStatus := mkeDecoder.DecodeB64(encodedString)
if mte.StatusIsError(decodedStatus) {
// Handle error correctly
// Below is an example
fmt.Fprintf(os.Stderr, "Decode error (%v): %v\n",
mte.GetStatusName(decodedStatus), mte.GetStatusDescription(decodedStatus))
return int(status)
} else if decodedStatus != mte.Status_mte_status_success {
// Handle warning correctly
// Below is an example
fmt.Fprintf(os.Stderr, "Decode warning (%v): %v\n",
mte.GetStatusName(decodedStatus), mte.GetStatusDescription(decodedStatus))
}
<?php
// Decode String to String
$decoded = $mkeDecoder->decode($encoded["str"]);
// Check the status
if (constant($decoded["status"])!=mte_status_success){
// Handle an error here -- below is a sample
throw new Exception("Decoder decode error: "
.$mkeDecoder->getStatusName(constant($decoded["status"])).":"
.$mkeDecoder->getStatusDescription(constant($decoded["status"])));
}
?>
<?php
// Decode Base64 string to string
$decoded = $mkeDecoder->decodeB64($encoded["str"]);
// Check the status
if (constant($decoded["status"])!=mte_status_success){
// Handle an error here -- below is a sample
throw new Exception("Decoder encode error: "
.$mkeDecoder->getStatusName(constant($decoded["status"])).":"
.$mkeDecoder->getStatusDescription(constant($decoded["status"])));
}
?>