mte_flen_console
- C
- C++
- CSharp
- Java
- Swift
- ObjC
- Python
- Go
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Callbacks. */
static mte_status ei_cb(void *, mte_drbg_ei_info *info);
static void n_cb(void *, mte_drbg_nonce_info *info);
static MTE_UINT64_T t_cb(void *);
/* Command line arguments. Default any that are not passed in:
[1] = input data to be tokenized. */
int main(int argc, char **argv)
{
int exitCode;
/* Status. */
mte_status status;
/* Input data. */
const char *input = argc > 1 ? argv[1] : "hello";
MTE_SIZE_T input_bytes = (MTE_SIZE_T)strlen(input);
/* Personalization string. */
const char personal[] = "demo";
/* Options. */
mte_flen_enc_init_info e_info = MTE_FLEN_ENC_INIT_INFO_INIT(
MTE_DRBG_ENUM, MTE_TOKBYTES, MTE_VERIFIERS_ENUM, 8, NULL, NULL);
mte_dec_init_info d_info = MTE_DEC_INIT_INFO_INIT(
MTE_DRBG_ENUM, MTE_TOKBYTES, MTE_VERIFIERS_ENUM, 1, 0, NULL, NULL);
mte_drbg_inst_info i_info =
{ &ei_cb, NULL, &n_cb, NULL, personal, sizeof(personal) - 1 };
/* Encoder. */
char *encoded;
MTE_HANDLE encoder = MTE_ALLOCA(mte_flen_enc_state_bytes(&e_info));
mte_enc_args e_args = MTE_ENC_ARGS_INIT(NULL, 0, NULL, &t_cb, NULL);
/* Decoder. */
char *decoded;
MTE_HANDLE decoder = MTE_ALLOCA(mte_dec_state_bytes(&d_info));
mte_dec_args d_args = MTE_DEC_ARGS_INIT(NULL, 0, NULL, &t_cb, NULL);
/* Licensing. */
const char *company;
const char *license;
/* Initialize MTE. */
if (!mte_init(NULL, NULL))
{
fputs("MTE init error.", stderr);
return -1;
}
/* Initialize MTE license. If a license code is not required (e.g., trial
mode), this can be skipped. This demo attempts to load the license info
from the environment if required. */
if (!mte_license_init("YOUR_COMPANY", "YOUR_LICENSE"))
{
company = getenv("MTE_COMPANY");
license = getenv("MTE_LICENSE");
if (company == NULL || license == NULL ||
!mte_license_init(company, license))
{
fprintf(stderr, "License init error (%s): %s\n",
mte_base_status_name(mte_status_license_error),
mte_base_status_description(mte_status_license_error));
return mte_status_license_error;
}
}
/* Output original data. */
printf("Original data: %s\n", input);
/* Create the encoder. */
status = mte_flen_enc_state_init(encoder, &e_info);
if (status != mte_status_success)
{
fprintf(stderr, "Encoder init error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return status;
}
status = mte_flen_enc_instantiate(encoder, &i_info);
if (status != mte_status_success)
{
fprintf(stderr, "Encoder instantiate error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return status;
}
/* Allocate the buffer for encoding. */
encoded = MTE_ALLOCA(mte_flen_enc_buff_bytes_b64(encoder));
/* Encode the message. */
MTE_SET_ENC_IO(e_args, input, input_bytes, encoded);
status = mte_flen_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;
}
/* Display the encoded message. */
printf("Base64 message: %s\n", (const char *)e_args.encoded);
/* Create the decoder. */
status = mte_dec_state_init(decoder, &d_info);
if (status != mte_status_success)
{
fprintf(stderr, "Decoder init error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return status;
}
status = mte_dec_instantiate(decoder, &i_info);
if (status != mte_status_success)
{
fprintf(stderr, "Decoder instantiate error (%s): %s\n",
mte_base_status_name(status),
mte_base_status_description(status));
return status;
}
/* Allocate the buffer for decoding. */
decoded = MTE_ALLOCA(mte_dec_buff_bytes_b64(decoder, e_args.bytes));
/* Decode the message. */
MTE_SET_DEC_IO(d_args, e_args.encoded, e_args.bytes, decoded);
status = mte_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));
}
/* Output the decoded data. */
printf("Decoded data: %s\n", (const char *)d_args.decoded);
/* Compare the decoded data against the original data. */
if (memcmp(d_args.decoded, input, input_bytes) == 0)
{
exitCode = 0;
puts("The original data and decoded data match.");
}
else
{
exitCode = -1;
puts("The original data and decoded data DO NOT match.");
}
/* Success. */
mte_flen_enc_uninstantiate(encoder);
mte_dec_uninstantiate(decoder);
return exitCode;
}
static mte_status ei_cb(void *context, mte_drbg_ei_info *info)
{
/* Create all-zero entropy for this demo. This should never be done in real
applications. */
(void)context;
info->bytes = info->min_length;
memset(info->buff, 0, info->min_length);
return mte_status_success;
}
static void n_cb(void *context, mte_drbg_nonce_info *info)
{
/* Create all-zero nonce for this demo. This should never be done in real
applications. */
(void)context;
info->bytes = info->min_length;
memset(info->buff, 0, info->min_length);
}
static MTE_UINT64_T t_cb(void *context)
{
/* Return 0 for the timestamp. Real applications would request an actual
timestamp as appropriate for their system. */
(void)context;
return 0;
}
#include <cstdlib>
#include <cstring>
#include <iostream>
// The fixed length.
static const size_t fixedBytes = 8;
// Command line arguments. Default any that are not passed in:
// [1] = input data to be tokenized.
int main(int argc, char **argv)
{
// Status.
mte_status status;
// Input.
std::string input = argc > 1 ? argv[1] : "hello";
// Personalization string.
static const std::string personal("demo");
// Initialize MTE license. If a license code is not required (e.g., trial
// mode), this can be skipped. This demo attempts to load the license info
// from the environment if required.
if (!MteBase::initLicense("YOUR_COMPANY", "YOUR_LICENSE"))
{
const char *company = getenv("MTE_COMPANY");
const char *license = getenv("MTE_LICENSE");
if (company == NULL || license == NULL ||
!MteBase::initLicense(company, license))
{
std::cerr << "License init error ("
<< MteBase::getStatusName(mte_status_license_error)
<< "): "
<< MteBase::getStatusDescription(mte_status_license_error)
<< std::endl;
return mte_status_license_error;
}
}
// Output original data.
std::cout << "Original data: " << input << std::endl;
// Create the encoder and decoder.
MteFlenEnc encoder(fixedBytes);
MteDec decoder;
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
size_t entropyBytes = MteBase::getDrbgsEntropyMinBytes(encoder.getDrbg());
uint8_t *entropy = new uint8_t[entropyBytes];
memset(entropy, 0, entropyBytes);
// Instantiate the encoder.
encoder.setEntropy(entropy, entropyBytes);
encoder.setNonce(0);
status = encoder.instantiate(personal);
if (status != mte_status_success)
{
std::cerr << "Encoder instantiate error ("
<< MteBase::getStatusName(status)
<< "): "
<< MteBase::getStatusDescription(status)
<< std::endl;
delete[] entropy;
return status;
}
// Note that after the instantiate call, the entropy buffer will be zeroized.
// In this demo, the entropy buffer was set to all zeroes to begin with.
// Care should be taken to ensure that if the same entropy buffer is used
// again for a paired encoder/decoder, that buffer must be set identically
// before an instantiate call. Alternatively, two different entropy buffers
// with identical values may be used.
// Encode the input.
const char *encoded = encoder.encodeB64(input, status);
if (status != mte_status_success)
{
std::cerr << "Encode error ("
<< MteBase::getStatusName(status)
<< "): "
<< MteBase::getStatusDescription(status)
<< std::endl;
return status;
}
// Display the message.
std::cout << "Base64 message: " << encoded << std::endl;
// Instantiate the decoder.
decoder.setEntropy(entropy, entropyBytes);
decoder.setNonce(0);
status = decoder.instantiate(personal);
if (status != mte_status_success)
{
std::cerr << "Decoder instantiate error ("
<< MteBase::getStatusName(status)
<< "): "
<< MteBase::getStatusDescription(status)
<< std::endl;
return status;
}
// Decode the message.
std::string decoded;
status = decoder.decodeB64(encoded, decoded);
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;
}
// Output the decoded data.
std::cout << "Decoded data: " << decoded << std::endl;
// Compare the decoded data against the original data.
if (decoded == input)
{
std::cout << "The original data and decoded data match." << std::endl;
}
else
{
std::cout << "The original data and decoded data DO NOT match."
<< std::endl;
return -1;
}
// Success.
delete[] entropy;
return 0;
}
using System;
using System.Text;
namespace MTE {
class demoCsFlen {
// The fixed length each MTE packet will be equal.
// To avoid data loss this number should be larger
// than any anticipated input data.
private const int fixedBytes = 8;
// Command line arguments. Default any that are not passed in:
// [0] = input data to be tokenized.
static int Main(string[] args) {
// Status.
MteStatus status;
// Input.
string input = args.Length > 0 ? args[0] : "hello";
// Personalization string.
string personal = "demo";
// Initialize MTE license. If a license code is not required (e.g., trial
// mode), this can be skipped. This demo attempts to load the license
// info from the environment if required.
MteBase baseObj = new MteBase();
if (!baseObj.InitLicense("YOUR_COMPANY", "YOUR_LICENSE")) {
string company = Environment.GetEnvironmentVariable("MTE_COMPANY");
string license = Environment.GetEnvironmentVariable("MTE_LICENSE");
if (company == null || license == null ||
!baseObj.InitLicense(company, license)) {
status = MteStatus.mte_status_license_error;
Console.Error.WriteLine("License init error ({0}): {1}",
baseObj.GetStatusName(status),
baseObj.GetStatusDescription(status));
return (int)status;
}
}
// Output original data.
Console.WriteLine("Original data: {0}", input);
// Create the Encoder and Decoder.
MteFlenEnc encoder = new MteFlenEnc(fixedBytes);
MteDec decoder = new MteDec();
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
int entropyBytes = encoder.GetDrbgsEntropyMinBytes(encoder.GetDrbg());
byte[] entropy = new byte[entropyBytes];
// Instantiate the Encoder.
encoder.SetEntropy(entropy);
encoder.SetNonce(0);
status = encoder.Instantiate(personal);
if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Encoder instantiate error ({0}): {1}",
encoder.GetStatusName(status),
encoder.GetStatusDescription(status));
return (int)status;
}
// Encode the input.
string encoded = encoder.EncodeB64(input, out status);
if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Encode error ({0}): {1}",
encoder.GetStatusName(status),
encoder.GetStatusDescription(status));
return (int)status;
}
// Display the message.
Console.WriteLine("Base64 message: {0}", encoded);
// Instantiate the Decoder.
decoder.SetEntropy(entropy);
decoder.SetNonce(0);
status = decoder.Instantiate(personal);
if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Decoder instantiate error ({0}): {1}",
decoder.GetStatusName(status),
decoder.GetStatusDescription(status));
return (int)status;
}
// Decode the message.
string decoded = decoder.DecodeStrB64(encoded, out status);
if (decoder.StatusIsError(status)) {
Console.Error.WriteLine("Decode error ({0}): {1}",
decoder.GetStatusName(status),
decoder.GetStatusDescription(status));
return (int)status;
} else if (status != MteStatus.mte_status_success) {
Console.Error.WriteLine("Decode warning ({0}): {1}",
decoder.GetStatusName(status),
decoder.GetStatusDescription(status));
}
// Output the decoded data.
Console.WriteLine("Decoded data: {0}", decoded);
// Compare the decoded data against the original data.
if (decoded == input) {
Console.WriteLine("The original data and decoded data match.");
} else {
Console.WriteLine("The original data and decoded data DO NOT match.");
return -1;
}
// Success.
return 0;
}
}
}
import com.eclypses.mte.*;
public class demoJavaFlen
{
// The fixed length each MTE packet will be equal.
// To avoid data loss this number should be larger
// than any anticipated input data.
private static final int fixedBytes = 8;
// Command line arguments. Default any that are not passed in:
// [0] = input data to be tokenized.
public static void main(String[] args)
{
// Status.
MteStatus status;
// Input.
String input = args.length > 0 ? args[0] : "hello";
// Personalization string.
String personal = "demo";
// Initialize MTE license. If a license code is not required (e.g., trial
// mode), this can be skipped. This demo attempts to load the license info
// from the environment if required.
if (!MteBase.initLicense("YOUR_COMPANY", "YOUR_LICENSE"))
{
String company = System.getenv("MTE_COMPANY");
String license = System.getenv("MTE_LICENSE");
if (company == null || license == null ||
!MteBase.initLicense(company, license))
{
status = MteStatus.mte_status_license_error;
System.err.println("Encode error (" +
MteBase.getStatusName(status) + "): " +
MteBase.getStatusDescription(status));
System.exit(status.getValue());
}
}
// Output original data.
System.out.println("Original data: " + input);
// Create the Encoder and Decoder.
MteFlenEnc encoder = new MteFlenEnc(fixedBytes);
MteDec decoder = new MteDec();
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
int entropyBytes = MteBase.getDrbgsEntropyMinBytes(encoder.getDrbg());
byte[] entropy = new byte[entropyBytes];
// Instantiate the Encoder.
encoder.setEntropy(entropy);
encoder.setNonce(0);
status = encoder.instantiate(personal);
if (status != MteStatus.mte_status_success)
{
System.err.println("Encoder instantiate error (" +
MteBase.getStatusName(status) + "): " +
MteBase.getStatusDescription(status));
System.exit(status.getValue());
}
// Encode the input.
MteBase.StrStatus encoded = encoder.encodeB64(input);
if (encoded.status != MteStatus.mte_status_success)
{
System.err.println("Encode error (" +
MteBase.getStatusName(encoded.status) + "): " +
MteBase.getStatusDescription(encoded.status));
System.exit(encoded.status.getValue());
}
// Display the message.
System.out.println("Base64 message: " + encoded.str);
// Instantiate the Decoder.
decoder.setEntropy(entropy);
decoder.setNonce(0);
status = decoder.instantiate(personal);
if (status != MteStatus.mte_status_success)
{
System.err.println("Decoder instantiate error (" +
MteBase.getStatusName(status) + "): " +
MteBase.getStatusDescription(status));
System.exit(status.getValue());
}
// Decode the message.
MteBase.StrStatus decoded = decoder.decodeStrB64(encoded.str);
if (MteBase.statusIsError(decoded.status))
{
System.err.println("Decode error (" +
MteBase.getStatusName(decoded.status) + "): " +
MteBase.getStatusDescription(decoded.status));
System.exit(decoded.status.getValue());
}
else if (decoded.status != MteStatus.mte_status_success)
{
System.err.println("Decode warning (" +
MteBase.getStatusName(decoded.status) + "): " +
MteBase.getStatusDescription(decoded.status));
}
// Output the decoded data.
System.out.println("Decoded data: " + decoded.str);
// Compare the decoded data against the original data.
if (decoded.str.equals(input))
{
System.out.println("The original data and decoded data match.");
}
else
{
System.out.println("The original data and decoded data DO NOT match.");
System.exit(-1);
}
// Success.
System.exit(0);
}
}
import Foundation
// The fixed length.
let fixedBytes = 8
// Command line arguments. Default any that are not passed in:
// [1] = input data to be tokenized.
public func main() -> Int32 {
// Status.
var status: mte_status
// Input.
let input = CommandLine.argc > 1 ? CommandLine.arguments[1] : "hello"
// Personalization string.
let personal = "demo"
// Initialize MTE license. If a license code is not required (e.g., trial
// mode), this can be skipped. This demo attempts to load the license info
// from the environment if required.
if !MteBase.initLicense("YOUR_COMPANY", "YOUR_LICENSE") {
let company = ProcessInfo.processInfo.environment["MTE_COMPANY"]
let license = ProcessInfo.processInfo.environment["MTE_LICENSE"];
if company == nil || license == nil ||
!MteBase.initLicense(company!, license!) {
status = mte_status_license_error
print("License init error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
return Int32(status.rawValue)
}
}
// Output original data.
print("Original data: \(input)")
// Create the encoder and decoder.
let encoder = try! MteFlenEnc(fixedBytes)
let decoder = try! MteDec()
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
let entropyBytes = MteBase.getDrbgsEntropyMinBytes(encoder.getDrbg())
var entropy = [UInt8](repeating: 0, count: entropyBytes)
// Instantiate the encoder.
encoder.setEntropy(&entropy)
encoder.setNonce(0)
status = encoder.instantiate(personal)
if status != mte_status_success {
print("Encoder instantiate error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
return Int32(status.rawValue)
}
// Encode the input.
var encoded: String
(encoded, status) = encoder.encodeB64(input)
if status != mte_status_success {
print("Encode error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
return Int32(status.rawValue)
}
// Display the message.
print("Base64 message: \(encoded)")
// Instantiate the decoder.
decoder.setEntropy(&entropy)
decoder.setNonce(0)
status = decoder.instantiate(personal)
if status != mte_status_success {
print("Decoder instantiate error (\(MteBase.getStatusName(status))): " +
MteBase.getStatusDescription(status))
return Int32(status.rawValue)
}
// Decode the message.
var decoded: String
(decoded, status) = decoder.decodeStrB64(encoded)
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))
}
// Output the decoded data.
print("Decoded data: \(decoded)")
// Compare the decoded data against the original data.
if decoded == input {
print("The original data and decoded data match.")
} else {
print("The original data and decoded data DO NOT match.")
return -1
}
// Success.
return 0
}
// The Fixed Length Add-On is a replacement for the core encoder that ensures all
// messages are encoded to a fixed length. If the input is shorter than the fixed length
// random padding bytes will be added to the end and if the input is too long, it will be
// truncated to the fixed length, therefore the fixedBytes property must be as large as
// the largest anticipated input length.
#import "MteFlenEnc.h"
#import "MteDec.h"
int main() {
uint64 min = 999999999999;
uint64 max = UINT_MAX;
uint8_t *encoderEntropy[32];
uint64 encoderNonce = 0;
NSString *encoderPersonalityString = @"";
uint8_t *decoderEntropy[32];
uint64 decoderNonce = 0;
NSString *decoderPersonalityString = @"";
// Status.
mte_status status;
NSString *valueToEncode = @"hello";
size_t fixedBytes = 8;
@autoreleasepool {
puts("****** Simple Objective-C MTE FLEN Console Demo ******\n\n");
// Check License
if (![MteBase initLicense:@"YOUR_COMPANY" code:@"YOUR_LICENSE"])
{
status = mte_status_license_error;
fprintf(stderr, "License init error (%s): %s\n",
[[MteBase getStatusName:status] UTF8String],
[[MteBase getStatusDescription:status] UTF8String]);
return status;
}
// Fill the entropy variables
if (SecRandomCopyBytes(kSecRandomDefault, 32, encoderEntropy) != 0) {
printf("%s", "Unable to generate Entropy");
}
memcpy(decoderEntropy, encoderEntropy, sizeof(encoderEntropy));
// Fill the nonce variables
encoderNonce = min + arc4random_uniform((uint32_t)(max - min + 1));
decoderNonce = encoderNonce;
// Fill the PersonalizationString variables
encoderPersonalityString = [[NSUUID UUID] UUIDString];
decoderPersonalityString = encoderPersonalityString;
// Create default Encoder
MteFlenEnc *encoder = MTE_AUTORELEASE([[MteFlenEnc alloc] initWithFixedBytes:fixedBytes]);
[encoder setEntropy:encoderEntropy bytes:32];
[encoder setNonce:encoderNonce];
status = [encoder instantiate:encoderPersonalityString];
if (status != mte_status_success)
{
fprintf(stderr, "Encoder instantiate error (%s): %s\n",
[[MteBase getStatusName:status] UTF8String],
[[MteBase getStatusDescription:status] UTF8String]);
return status;
}
NSString *encoded = [encoder encodeB64:valueToEncode status:&status];
if (status != mte_status_success)
{
fprintf(stderr, "Encode error (%s): %s\n",
[[MteBase getStatusName:status] UTF8String],
[[MteBase getStatusDescription:status] UTF8String]);
return status;
}
// Create default Decoder
MteDec *decoder = MTE_AUTORELEASE([[MteDec alloc] init]);
[decoder setEntropy:decoderEntropy bytes:32];
[decoder setNonce:decoderNonce];
[decoder instantiate:decoderPersonalityString];
if (status != mte_status_success)
{
fprintf(stderr, "Decoder instantiate error (%s): %s\n",
[[MteBase getStatusName:status] UTF8String],
[[MteBase getStatusDescription:status] UTF8String]);
return status;
}
NSString *decoded = [decoder decodeB64:encoded status:&status];
if ([MteBase statusIsError:status])
{
fprintf(stderr, "Decode error (%s): %s\n",
[[MteBase getStatusName:status] UTF8String],
[[MteBase getStatusDescription:status] UTF8String]);
return status;
}
else if (status != mte_status_success)
{
fprintf(stderr, "Decode warning (%s): %s\n",
[[MteBase getStatusName:status] UTF8String],
[[MteBase getStatusDescription:status] UTF8String]);
return status;
}
// Compare the decoded data against the original data.
if ([decoded isEqualToString:valueToEncode])
{
puts("The original data and decoded data match.\n");
}
else
{
puts("The original data and decoded data DO NOT match.");
return -1;
}
}
return 0;
}
#!/usr/bin/env python3
from MteDrbgs import MteDrbgs
from MteVerifiers import MteVerifiers
from MteStatus import MteStatus
from MteBase import MteBase
from MteFlenEnc import MteFlenEnc
from MteDec import MteDec
import os
import sys
# The fixed length.
fixed_bytes = 8
# Command line arguments. Default any that are not passed in:
# [1] = input data to be tokenized.
def main():
# Input.
input_ = "hello" if len(sys.argv) <= 1 else sys.argv[1]
# Personalization string.
personal = "demo"
# Initialize MTE license. If a license code is not required (e.g., trial
# mode), this can be skipped. This demo attempts to load the license info
# from the environment if required.
if not MteBase.init_license("YOUR_COMPANY", "YOUR_LICENSE"):
company = os.getenv("MTE_COMPANY")
license = os.getenv("MTE_LICENSE")
if company is None or \
license is None or \
not MteBase.init_license(company, license):
print("License init error ({0}): {1}".format(
MteBase.get_status_name(MteStatus.mte_status_license_error),
MteBase.get_status_description(MteStatus.mte_status_license_error)),
file=sys.stderr)
return MteStatus.mte_status_license_error.value
# Output original data.
print("Original data: {0}".format(input_))
# Create the Encoder and Decoder.
encoder = MteFlenEnc.fromdefault(fixed_bytes)
decoder = MteDec.fromdefault()
# Create all-zero entropy for this demo. The nonce will also be set to 0.
# This should never be done in real applications.
entropy_bytes = MteBase.get_drbgs_entropy_min_bytes(encoder.get_drbg())
entropy = bytes(entropy_bytes)
# Instantiate the Encoder.
encoder.set_entropy(entropy)
encoder.set_nonce(0)
status = encoder.instantiate(personal)
if status != MteStatus.mte_status_success:
print("Encoder instantiate error ({0}): {1}".format(
MteBase.get_status_name(status),
MteBase.get_status_description(status)),
file=sys.stderr)
return status.value
# Encode the input.
(encoded, status) = encoder.encode_b64(input_)
if status != MteStatus.mte_status_success:
print("Encode error ({0}): {1}".format(
MteBase.get_status_name(status),
MteBase.get_status_description(status)),
file=sys.stderr)
return status.value
# Display the message.
print(f"Base64 message: {encoded}")
# Instantiate the Decoder.
decoder.set_entropy(entropy)
decoder.set_nonce(0)
status = decoder.instantiate(personal)
if status != MteStatus.mte_status_success:
print("Decoder instantiate error ({0}): {1}".format(
MteBase.get_status_name(status),
MteBase.get_status_description(status)),
file=sys.stderr)
return status.value
# Decode the message.
(decoded, status) = decoder.decode_str_b64(encoded)
if MteBase.status_is_error(status):
print("Decode error ({0}): {1}".format(
MteBase.get_status_name(status),
MteBase.get_status_description(status)),
file=sys.stderr)
return status.value
elif status != MteStatus.mte_status_success:
print("Decode warning ({0}): {1}".format(
MteBase.get_status_name(status),
MteBase.get_status_description(status)),
file=sys.stderr)
# Output the decoded data.
print(f"Decoded data: {decoded}")
# Compare the decoded data against the original data.
if decoded == input_:
print("The original data and decoded data match.")
else:
print("The original data and decoded data DO NOT match.")
return -1
# Success.
return 0
if __name__ == "__main__":
sys.exit(main())
package main
import (
"fmt"
"os"
"goFlen/mte"
)
// The fixed length.
const fixedBytes = 8
// Command line arguments. Default any that are not passed in:
// [1] = input data to be tokenized.
func doMain() int {
// Status.
var status mte.Status
// Input.
input := "hello"
if len(os.Args) > 1 {
input = os.Args[1]
}
// Personalization string.
const personal = "demo"
// Initialize MTE license. If a license code is not required (e.g., trial
// mode), this can be skipped. This demo attempts to load the license info
// from the environment if required.
if !mte.InitLicense("YOUR_COMPANY", "YOUR_LICENSE") {
company := os.Getenv("MTE_COMPANY")
license := os.Getenv("MTE_LICENSE")
if len(company) == 0 || len(license) == 0 ||
!mte.InitLicense(company, license) {
fmt.Fprintf(os.Stderr, "License init error (%v): %v\n",
mte.GetStatusName(mte.Status_mte_status_license_error),
mte.GetStatusDescription(mte.Status_mte_status_license_error))
return int(mte.Status_mte_status_license_error)
}
}
// Output original data.
fmt.Printf("Original data: %v\n", input)
// Create the Encoder and Decoder.
encoder := mte.NewFlenEncDef(fixedBytes)
decoder := mte.NewDecDef()
defer encoder.Destroy()
defer decoder.Destroy()
// Create all-zero entropy for this demo. The nonce will also be set to 0.
// This should never be done in real applications.
entropyBytes := mte.GetDrbgsEntropyMinBytes(encoder.GetDrbg())
entropy := make([]byte, entropyBytes)
// Instantiate the Encoder.
encoder.SetEntropy(entropy)
encoder.SetNonceInt(0)
status = encoder.InstantiateStr(personal)
if status != mte.Status_mte_status_success {
fmt.Fprintf(os.Stderr, "Encoder instantiate error (%v): %v\n",
mte.GetStatusName(status), mte.GetStatusDescription(status))
return int(status)
}
// Encode the input.
encoded, status := encoder.EncodeStrB64(input)
if status != mte.Status_mte_status_success {
fmt.Fprintf(os.Stderr, "Encode error (%v): %v\n",
mte.GetStatusName(status), mte.GetStatusDescription(status))
return int(status)
}
// Display the message.
fmt.Printf("Base64 message: %v\n", encoded)
// Instantiate the Decoder.
decoder.SetEntropy(entropy)
decoder.SetNonceInt(0)
status = decoder.InstantiateStr(personal)
if status != mte.Status_mte_status_success {
fmt.Fprintf(os.Stderr, "Decoder instantiate error (%v): %v\n",
mte.GetStatusName(status), mte.GetStatusDescription(status))
return int(status)
}
// Decode the message.
decoded, status := decoder.DecodeStrB64(encoded)
if mte.StatusIsError(status) {
fmt.Fprintf(os.Stderr, "Decode error (%v): %v\n",
mte.GetStatusName(status), mte.GetStatusDescription(status))
return int(status)
} else if status != mte.Status_mte_status_success {
fmt.Fprintf(os.Stderr, "Decode warning (%v): %v\n",
mte.GetStatusName(status), mte.GetStatusDescription(status))
}
// Output the decoded data.
fmt.Printf("Decoded data: %v\n", decoded)
// Compare the decoded data against the original data.
if decoded == input {
fmt.Println("The original data and decoded data match.")
} else {
fmt.Println("The original data and decoded data DO NOT match.")
return -1
}
// Success.
return 0
}
func main() {
os.Exit(doMain())
}