`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`.
Creates an empty crypto object.
c = crypto(); print(c.ready); // false print(c.mode); // none
| Property | Type | Description |
|---|---|---|
| `ready` | boolean | `true` when a key has been configured. |
| `mode` | string | Current key mode: `none`, `raw`, `password`, or `controller`. |
Clears the configured key and returns the crypto object.
c = crypto().key_password('secret'); c.clear(); print(c.ready); // false
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);
Derives a key from `password` and returns the crypto object.
c = crypto().key_password('secret');
Sets a controller-based key and returns the crypto object.
Arguments:
| Argument | Description |
|---|---|
| `uniqueId` | Controller unique ID bytes as a `data_array`. |
Exports the current key and returns it as a string.
c = crypto().key_password('secret'); key_text = c.key_export();
Exports the current key to `filePath` and returns `true` on success.
crypto().key_password('secret').key_export('key.txt');
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);
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);
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);
Encrypts one file into another file and returns `true` on success.
c = crypto().key_password('pw'); c.encrypt_file('plain.bin', 'plain.bin.crp');