Table of Contents

crypto

`crypto` encrypts and decrypts `data_array` values or files.

A crypto object starts without a key. Set a key before calling `encrypt`, `decrypt`, `encrypt_file`, or `decrypt_file`.


Constructor

crypto()

Creates an empty crypto object.

c = crypto();
print(c.ready);      // false
print(c.mode);       // none

Properties

Property Type Description
`ready` boolean `true` when a key has been configured.
`mode` string Current key mode: `none`, `raw`, `password`, or `controller`.

Methods

clear()

Clears the configured key and returns the crypto object.

c = crypto().key_password('secret');
c.clear();
print(c.ready);      // false

key_raw(key)

Sets a raw key from a `data_array` and returns the crypto object.

Arguments:

Argument Description
`key` Raw key bytes as a `data_array`.
key = data_array();
key.add_string('my raw key');
c = crypto().key_raw(key);

key_password(password)

Derives a key from `password` and returns the crypto object.

c = crypto().key_password('secret');

key_controller(uniqueId)

Sets a controller-based key and returns the crypto object.

Arguments:

Argument Description
`uniqueId` Controller unique ID bytes as a `data_array`.

key_export()

Exports the current key and returns it as a string.

c = crypto().key_password('secret');
key_text = c.key_export();

key_export(filePath)

Exports the current key to `filePath` and returns `true` on success.

crypto().key_password('secret').key_export('key.txt');

key_import(keyOrFile)

Imports a key from an exported key string or from an existing file path. Returns the crypto object.

c1 = crypto().key_password('secret');
key_text = c1.key_export();
 
c2 = crypto().key_import(key_text);

encrypt(data)

Encrypts a `data_array` and returns encrypted bytes as a new `data_array`.

plain = data_array();
plain.add_string('secret text');
 
encrypted = crypto().key_password('pw').encrypt(plain);

decrypt(data)

Decrypts a `data_array` and returns decrypted bytes as a new `data_array`.

c = crypto().key_password('pw');
encrypted = c.encrypt(data_array().add_string('ABC'));
plain = c.decrypt(encrypted);

encrypt_file(sourcePath, destinationPath)

Encrypts one file into another file and returns `true` on success.

c = crypto().key_password('pw');
c.encrypt_file('plain.bin', 'plain.bin.crp');

decrypt_file(sourcePath, destinationPath)

Decrypts one file into another file and returns `true` on success.

c = crypto().key_password('pw');
c.decrypt_file('plain.bin.crp', 'plain_restored.bin');

Previous: Utils

Next: timer