dreamworkAPI
Ctrl K
GitHub
libraryDocs / crypto / xchacha20

crypto.xchacha20

librarysharedmenusource

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

NameTypeDescription
key_sizeinteger = 32
nonce_sizeinteger = 24
block_sizeinteger = 64

Functions

crypto.xchacha20.encryptEncrypts or decrypts the data with XChaCha20.
crypto.xchacha20.hchacha20Derives a 32-byte subkey from the key and a 16-byte nonce with HChaCha20.

#crypto.xchacha20.encrypt

sharedmenunodiscardsource
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
NameTypeDescription
datastringThe plaintext to encrypt or the ciphertext to decrypt.
keystringThe 32-byte key.
noncestringThe 24-byte nonce.
counteroptionalintegerThe initial block counter, default is 0.
Returns
TypeNameDescription
stringresultThe ciphertext or plaintext, same length as data.

#crypto.xchacha20.hchacha20

sharedmenunodiscardsource
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
NameTypeDescription
keystringThe 32-byte key.
noncestringThe 16-byte nonce.
Returns
TypeNameDescription
stringsubkeyThe 32-byte subkey.
Generated from the dreamwork meta branch.