Skip to content

Contexts

class Context

A context is composed of exactly 8 bytes and is used to separate different domains in the library.

Multiple features of this library require a context. Contexts help to avoid mistakes by separating different domains. The same crypto feature working in different contexts will produce different results.

A context must be a valid ASCII string, and it must be exactly 8 bytes long (it is padded with spaces if shorter).

A context is not a secret.

Context implements the buffer protocol, acting as a bytes-like object of length 8.

Examples:

>>> ctx = Context("domain1")                            # from ascii string
>>> ctx2 = Context(b"another")                          # from bytes
>>> ctx3 = Context(ctx)                                 # from another context
>>> empty_ctx = Context()                               # creates an empty context (8 spaces)
>>> assert(Context("short") == Context("short   "))     # shorter strings are padded with spaces

__init__(ctx=None)

Initializes a new context.

Parameters:

  • ctx (str | bytes | Self | Buffer | None, default: None ) –

    The context to use. If None, an empty context (8 spaces) is created.

Raises:

  • ValueError

    If the passed context is too long or not a valid ASCII string.

empty() classmethod

Returns an empty context, which is a context of 8 spaces.

Returns:

  • Self

    An empty context (8 spaces).

is_empty()

Checks if the context is empty.

Returns:

  • bool

    True if the context is empty (8 spaces), False otherwise.