`crypto` encrypts and decrypts `bytes` values or files.
A crypto object starts without a key. Set a key before calling `encrypt`, `decrypt`, `encrypt_file`, or `decrypt_file`.
`crypto` is not cloneable because it holds key state.
Creates an empty crypto object.
| Item | Description |
|---|---|
| Syntax | `crypto()` |
| Arguments | none |
| Returns | crypto object |
Example:
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`. |
Properties are read-only. Use key methods to change the crypto state.
Returns a display string for the object.
| Item | Description |
|---|---|
| Syntax | `value.to_string()` |
| Arguments | none |
| Returns | string |
Example:
text = value.to_string();
`clone()` is not supported for `crypto`.
Clears the configured key.
| Item | Description |
|---|---|
| Syntax | `crypto.clear()` |
| Arguments | none |
| Returns | crypto object |
Example:
c = crypto().key_password('secret'); c.clear(); print(c.ready); // false
Sets a raw key from a `bytes` object.
| Argument | Type | Description |
|---|---|---|
| `key` | bytes | Raw key bytes. |
Returns: crypto object.
Example:
key = bytes(); key.add_string('my raw key'); c = crypto().key_raw(key);
Derives a key from `password`.
| Argument | Type | Description |
|---|---|---|
| `password` | string | Password text. |
Returns: crypto object.
Example:
c = crypto().key_password('secret');
Sets a controller-based key.
| Argument | Type | Description |
|---|---|---|
| `uniqueId` | bytes | Controller unique ID bytes. |
Returns: crypto object.
Example:
id = bytes(); id.add_string('controller-id'); c = crypto().key_controller(id);
Exports the current key as text.
| Item | Description |
|---|---|
| Syntax | `crypto.key_export()` |
| Arguments | none |
| Returns | string |
Example:
c = crypto().key_password('secret'); key_text = c.key_export();
Exports the current key to a file.
| Argument | Type | Description |
|---|---|---|
| `filePath` | string | Output file path. |
Returns: boolean-style number.
Example:
crypto().key_password('secret').key_export('key.txt');
Imports a key from exported key text or from an existing file path.
| Argument | Type | Description |
|---|---|---|
| `keyOrFile` | string | Exported key text, or path to a file containing exported key text. |
Returns: crypto object.
Example:
c1 = crypto().key_password('secret'); key_text = c1.key_export(); c2 = crypto().key_import(key_text);
Encrypts a `bytes` object.
| Argument | Type | Description |
|---|---|---|
| `data` | bytes | Plain input bytes. |
Returns: encrypted bytes object.
Example:
plain = bytes(); plain.add_string('secret text'); encrypted = crypto().key_password('pw').encrypt(plain);
Decrypts a `bytes` object.
| Argument | Type | Description |
|---|---|---|
| `data` | bytes | Encrypted input bytes. |
Returns: decrypted bytes object.
Example:
c = crypto().key_password('pw'); encrypted = c.encrypt(bytes().add_string('ABC')); plain = c.decrypt(encrypted);
Encrypts one file into another file.
| Argument | Type | Description |
|---|---|---|
| `sourcePath` | string | Source file path. |
| `destinationPath` | string | Destination file path. |
Returns: boolean-style number.
Example:
c = crypto().key_password('pw'); c.encrypt_file('plain.bin', 'plain.bin.crp');
Decrypts one file into another file.
| Argument | Type | Description |
|---|---|---|
| `sourcePath` | string | Source encrypted file path. |
| `destinationPath` | string | Destination file path. |
Returns: boolean-style number.
Example:
c = crypto().key_password('pw'); c.decrypt_file('plain.bin.crp', 'plain_restored.bin');
Previous: Utils
Next: timer