Class ChaCha20Poly1305

Encryptor/decryptor for the ChaCha20 stream cipher with the Poly1305 message authentication code.
It is often used for TLS.
An instance of this class can encrypt or decrypt one message.

Class summary


IV-SIZE -> any
TAG-SIZE -> any
encryptor key/ByteArray initialization-vector/ByteArray -> ChaCha20Poly1305
Initialize a ChaCha20-Poly1305 AEAD class for encryption.
decryptor key/ByteArray initialization-vector/ByteArray -> ChaCha20Poly1305
Initialize a ChaCha20-Poly1305 AEAD class for encryption or decryption.

Statics

decryptor key/ByteArray initialization-vector/ByteArray -> ChaCha20Poly1305
Initialize a ChaCha20-Poly1305 AEAD class for encryption or decryption.
The key must be a 32 byte ChaCha20 key.
The initialization-vector must be 12 bytes of data, obtained from the encrypting counterparty.

encryptor key/ByteArray initialization-vector/ByteArray -> ChaCha20Poly1305
Initialize a ChaCha20-Poly1305 AEAD class for encryption.
The key must be a 32 bytes ChaCha20 key.
The initialization-vector must be 12 bytes of data. It is extremely important that the initialization-vector is not reused with the same key.
The initialization-vector must be known to the decrypting counterparty.

IV-SIZE -> any

TAG-SIZE -> any

Methods

Encrypts or decrypts some data.
Can be called after calling start.
The data argument is consumed by this operation: After this call, the given ByteArray can no longer be used.
The returned ByteArray can be regarded as fresh, though it may be one of the ByteArrays previously passed to this function.
When decrypting it is vital that the decrypted data is not used in any way before the verification tag has been verified with a call to verify.

close -> none
Closes this encrypter/decrypter and releases associated resources.

decrypt ciphertext/ByteArray --authenticated-data/any="" --verification-tag/ByteArray=null -> ByteArray
Decrypts the given ciphertext.
The verification-tag, 16 bytes, is checked and an exception is thrown if it fails.
If the verification-tag is not provided, it is assumed to be appended to the ciphertext.
This method is equivalent to calling start, add, and verify, and therefore it closes this instance.

encrypt plaintext/Data --authenticated-data/any="" -> ByteArray
Encrypts the given plaintext.
The plaintext must be a ByteArray or a string.
If provided, the authenticated-data is data that takes part in the verification tag, but does not get encrypted.
Returns the encrypted plaintext. The verification tag, 16 bytes, is appended to the result.
This method is equivalent to calling start, add, and finish, and therefore it closes this instance.

Finishes encrypting.
Can be called after start and add.
Returns a concatenation of the last encrypted bytes and the verification tag. The last encrypted bytes will have zero length if the size of the plaintext was a multiple of 16.
Closes this instance.

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

start --authenticated-data/any="" -> none
Starts an encryption or decryption.
After calling this method, the add method can be used to encrypt or decrypt a ByteArray.
When decrypting, it is vital that the decrypted data is not used in any way before the verification tag has been verified with a call to verify.

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

verify verification-tag/ByteArray -> ByteArray
Finishes decrypting.
Can be called after start and add.
Throws an exception if the 16 byte verification-tag does not match the decrypted data.
It is vital that the decrypted data is not used in any way before this method has been called.
It is vital that if this method throws an exception, the previously decrypted data is not used.
Returns the last few bytes of the decrypted data. This is an empty ByteArray if the size of the ciphertext was a multiple of 16.
Closes this instance.

Fields

inherited