encoder-encode-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.
*/
/* Allocate the buffer for encoding. */
char* encoded = MTE_ALLOCA(mte_enc_buff_bytes_b64(encoder));
/* Encode the message. */
MTE_SET_ENC_IO(e_args, input, input_bytes, encoded);
status = mte_enc_encode_b64(encoder, &e_args);
if (status != mte_status_success)
{
fprintf(stderr, "Encode error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return 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 encodedBytes = 0;
// Encode the message.
const void* encoded = encoder.encode(input, inputBytes, encodedBytes, status);
if (status != mte_status_success)
{
std::cerr << "Encode error ("
<< MteBase::getStatusName(status)
<< "): "
<< MteBase::getStatusDescription(status)
<< std::endl;
return status;
}
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;
}
// Example value to be encoded
string inputString = "What I want to be encoded goes here";
byte[] bytesToBeEncoded = Encoding.ASCII.GetBytes(inputString);
// initialize status
MteStatus encoderStatus;
// Below are 4 different examples of how to encode
// ONLY ONE needs to be used!!!
// Encode byte[] to a base64 string
string encodedBytesAsString = mteEncoder.EncodeB64(bytesToBeEncoded, out encoderStatus);
if(encoderStatus != MteStatus.mte_status_success)
{
// Handle encode failure appropriately
// Below is only an example
throw new Exception("Failed to encode. Status: "
+ mteEncoder.GetStatusName(encoderStatus)+ " / "
+ mteEncoder.GetStatusDescription(encoderStatus));
}
// Encode string to a base64 string
string encodedStringAsString = mteEncoder.EncodeStrB64(inputString, out encoderStatus);
if(encoderStatus != MteStatus.mte_status_success)
{
// Handle encode failure appropriately
// Below is only an example
throw new Exception("Failed to encode. Status: "
+ mteEncoder.GetStatusName(encoderStatus)+ " / "
+ mteEncoder.GetStatusDescription(encoderStatus));
}
// Encode byte[] to a byte[]
byte[] encodedBytesAsBytes = mteEncoder.Encode(bytesToBeEncoded, out encoderStatus);
if(encoderStatus != MteStatus.mte_status_success)
{
// Handle encode failure appropriately
// Below is only an example
throw new Exception("Failed to encode. Status: "
+ mteEncoder.GetStatusName(encoderStatus)+ " / "
+ mteEncoder.GetStatusDescription(encoderStatus));
}
// Encode string to a byte[]
byte[] encodedStringAsBytes = mteEncoder.EncodeStr(inputString, out encoderStatus);
if(encoderStatus != MteStatus.mte_status_success)
{
// Handle encode failure appropriately
// Below is only an example
throw new Exception("Failed to encode. Status: "
+ mteEncoder.GetStatusName(encoderStatus)+ " / "
+ mteEncoder.GetStatusDescription(encoderStatus));
}
// Example value to be encoded
String inputString = "What I want to be encoded goes here";
byte[] bytesToBeEncoded = inputString.getBytes(StandardCharsets.UTF_16);
// initialize status
MteStatus encoderStatus;
// Below are 4 different examples of how to encode
// ONLY ONE needs to be used!!!
// Encode byte[] to a base64 string
String encodedBytesAsString = mteEncoder.encodeB64(bytesToBeEncoded, out encoderStatus);
if(encoderStatus != MteStatus.mte_status_success)
{
// Handle encode failure appropriately
// Below is only an example
throw new Exception("Failed to encode. Status: "
+ MteBase.getStatusName(encoderStatus)+ " / "
+ MteBase.getStatusDescription(encoderStatus));
}
// Encode string to a base64 string
String encodedStringAsString = mteEncoder.encodeStrB64(inputString, out encoderStatus);
if(encoderStatus != MteStatus.mte_status_success)
{
// Handle encode failure appropriately
// Below is only an example
throw new Exception("Failed to encode. Status: "
+ MteBase.getStatusName(encoderStatus)+ " / "
+ MteBase.getStatusDescription(encoderStatus));
}
// Encode byte[] to a byte[]
byte[] encodedBytesAsBytes = mteEncoder.encode(bytesToBeEncoded, out encoderStatus);
if(encoderStatus != MteStatus.mte_status_success)
{
// Handle encode failure appropriately
// Below is only an example
throw new Exception("Failed to encode. Status: "
+ MteBase.getStatusName(encoderStatus)+ " / "
+ MteBase.getStatusDescription(encoderStatus));
}
// Encode string to a byte[]
byte[] encodedStringAsBytes = mteEncoder.encodeStr(inputString, out encoderStatus);
if(encoderStatus != MteStatus.mte_status_success)
{
// Handle encode failure appropriately
// Below is only an example
throw new Exception("Failed to encode. Status: "
+ MteBase.getStatusName(encoderStatus)+ " / "
+ MteBase.getStatusDescription(encoderStatus));
}
// encode string and return as B64 string
const str = "A sensitive string.";
const encoderResult = mteEncoder.encodeStrB64(str);
if (encoderResult.status !== MteStatus.mte_status_success) {
const status = mteBase.getStatusName(encoderResult);
const message = mteBase.getStatusDescription(encoderResult);
throw new Error(`Failed to encode. Status ${status}: ${message}\n`);
}
// encoderResult.str contains encoded result
// encode string and return as Uint8Array
const str = "A sensitive string.";
const encoderResult = mteEncoder.encoderStr(str);
if (encoderResult.status !== MteStatus.mte_status_success) {
const status = mteBase.getStatusName(encoderResult);
const message = mteBase.getStatusDescription(encoderResult);
throw new Error(`Failed to encode. Status ${status}: ${message}\n`);
}
// encoderResult.arr contains encoded result
// encode Uint8Array and return as b64 string
const arr = new Uint8Array([9, 19, 89]);
const encoderResult = mteEncoder.encodeB64(arr);
if (encoderResult.status !== MteStatus.mte_status_success) {
const status = mteBase.getStatusName(encoderResult);
const message = mteBase.getStatusDescription(encoderResult);
throw new Error(`Failed to encode. Status ${status}: ${message}\n`);
}
// encoderResult.str contains encoded result
// encode Uint8Array and return as Uint8Array
const arr = new Uint8Array([9, 19, 89]);
const encoderResult = mteEncoder.encode(arr);
if (encoderResult.status !== MteStatus.mte_status_success) {
const status = mteBase.getStatusName(encoderResult);
const message = mteBase.getStatusDescription(encoderResult);
throw new Error(`Failed to encode. Status ${status}: ${message}\n`);
}
// encoderResult.arr contains encoded result
// 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.
// Encode the message.
let encodedRes = encoder.encode(input)
status = encodedRes.status
if (status != mte_status_success) {
print("Encode error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
return Int32(status.rawValue)
}
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()!)!
}
# Example values to be encoded.
input_string = "What I want to be encoded goes here"
bytes_to_be_encoded = bytes(input_string,'utf-8')
# Below are 4 different examples of how to encode.
# ONLY ONE needs to be used.
# Encode byte[] to a base64 string.
(encoded_bytes_as_string, encoder_status) = mte_encoder.encode_b64(bytes_to_be_encoded)
if encoder_status != MteStatus.mte_status_success:
# Handle encode failure appropriately.
# Below is only an example.
raise Exception("Failed to encode. Status: {0} / {1}\n".format(MteBase.get_status_name(encoder_status),
MteBase.get_status_description(encoder_status)))
# Encode string to a base64 string.
(encoded_string_as_string, encoder_status) = mte_encoder.encode_b64(input_string)
if encoder_status != MteStatus.mte_status_success:
# Handle encode failure appropriately.
# Below is only an example.
raise Exception("Failed to encode. Status: {0} / {1}\n".format(MteBase.get_status_name(encoder_status),
MteBase.get_status_description(encoder_status)))
# Encode byte[] to a byte[].
(encoded_bytes_as_string, encoder_status) = mte_encoder.encode(bytes_to_be_encoded)
if encoder_status != MteStatus.mte_status_success:
# Handle encode failure appropriately.
# Below is only an example.
raise Exception("Failed to encode. Status: {0} / {1}\n".format(MteBase.get_status_name(encoder_status),
MteBase.get_status_description(encoder_status)))
# Encode string to a byte[].
(encoded_string_as_bytes, encoder_status) = mte_encoder.encode(input_string)
if encoder_status != MteStatus.mte_status_success:
# Handle encode failure appropriately.
# Below is only an example.
raise Exception("Failed to encode. Status: {0} / {1}\n".format(MteBase.get_status_name(encoder_status),
MteBase.get_status_description(encoder_status)))
// Example values to be encoded
inputString := "Message we are going to encode"
inputBytes := []byte(inputString)
// initialize status
var encoderStatus mte.Status
// Encode string to a base64 string
encodedString, encoderStatus := mteEncoder.EncodeStrB64(inputString)
if(encoderStatus != mte.Status_mte_status_success){
// Handle error -- example below
fmt.Fprintf(os.Stderr, "Failed to encode. Status (%v): %v\n",
mte.GetStatusName(encoderStatus), mte.GetStatusDescription(encoderStatus))
return int(encoderStatus)
}
// Encode byte[] to a base64 string
encodedString, encoderStatus := mteEncoder.EncodeB64(inputBytes)
if(encoderStatus != mte.Status_mte_status_success){
// Handle error -- example below
fmt.Fprintf(os.Stderr, "Failed to encode. Status (%v): %v\n",
mte.GetStatusName(encoderStatus), mte.GetStatusDescription(encoderStatus))
return int(encoderStatus)
}
// Encode string to a byte array
encodedBytes, encoderStatus := mteEncoder.EncodeStr(inputString)
if(encoderStatus != mte.Status_mte_status_success){
// Handle error -- example below
fmt.Fprintf(os.Stderr, "Failed to encode. Status (%v): %v\n",
mte.GetStatusName(encoderStatus), mte.GetStatusDescription(encoderStatus))
return int(encoderStatus)
}
// Encode bytes to a byte array
encodedBytes, encoderStatus := mteEncoder.Encode(inputBytes)
if(encoderStatus != mte.Status_mte_status_success){
// Handle error -- example below
fmt.Fprintf(os.Stderr, "Failed to encode. Status (%v): %v\n",
mte.GetStatusName(encoderStatus), mte.GetStatusDescription(encoderStatus))
return int(encoderStatus)
}
<?php
// Example value to be encoded
$inputString = "What I want to be encoded goes here";
// Below are 2 different examples of how to encode
// ONLY ONE needs to be used!!!
?>
<?php
// Encode String to String
$encoded = $mteEnc->encode($inputString);
// Check the status
if (constant($encoded["status"])!=mte_status_success){
// Handle an error here -- below is a sample
throw new Exception("Failed to encode. Status "
.$mteEncoder->getStatusName(constant($encoded["status"])).":"
.$mteEncoder->getStatusDescription(constant($encoded["status"])));
}
?>
<?php
// Encode string to Base64 String
$base64encoded = $mteEnc->encodeB64($inputString);
// Check the status
if (constant($base64encoded["status"])!=mte_status_success){
// Handle an error here -- below is a sample
throw new Exception("Failed to encode. Status "
.$mteEncoder->getStatusName(constant($base64encoded["status"])).":"
.$mteEncoder->getStatusDescription(constant($base64encoded["status"])));
}
?>