crypto.xchacha20
XChaCha20 stream cipher library, ChaCha20 with a 192-bit nonce.
The nonce is large enough to be generated randomly for every message.
Encryption and decryption are the same operation, the data is XORed with the keystream.
XChaCha20 alone does not authenticate the data, use a MAC ( e.g. crypto.hmac ) over the ciphertext to detect tampering.
See draft-irtf-cfrg-xchacha for the algorithm.
Fields
| Name | Type | Description |
|---|---|---|
key_size | integer | = 32 |
nonce_size | integer | = 24 |
block_size | integer | = 64 |
Functions
crypto.xchacha20.encrypt | Encrypts or decrypts the data with XChaCha20. |
crypto.xchacha20.hchacha20 | Derives a 32-byte subkey from the key and a 16-byte nonce with HChaCha20. |
#crypto.xchacha20.encrypt
function crypto.xchacha20.encrypt(data: string, key: string, nonce: string, counter?: integer): string
Encrypts or decrypts the data with XChaCha20.
The data can be processed in parts: each part except the last must be a multiple of 64 bytes long, and counter of the next part is counter + #part / 64.
Parameters
| Name | Type | Description |
|---|---|---|
data | string | The plaintext to encrypt or the ciphertext to decrypt. |
key | string | The 32-byte key. |
nonce | string | The 24-byte nonce. |
counteroptional | integer | The initial block counter, default is 0. |
Returns
| Type | Name | Description |
|---|---|---|
string | result | The ciphertext or plaintext, same length as data. |
#crypto.xchacha20.hchacha20
function crypto.xchacha20.hchacha20(key: string, nonce: string): string
Derives a 32-byte subkey from the key and a 16-byte nonce with HChaCha20.
Parameters
| Name | Type | Description |
|---|---|---|
key | string | The 32-byte key. |
nonce | string | The 16-byte nonce. |
Returns
| Type | Name | Description |
|---|---|---|
string | subkey | The 32-byte subkey. |