Skip to content

API reference

Greg Bowler edited this page Apr 19, 2026 · 1 revision

This page is a compact reference for Cipher's public API.

For walkthroughs and fuller explanations, use the earlier pages in the guide.

Core value objects

GT\Cipher\Key

  • __construct(?string $bytes = null)
  • __toString(): string
  • getBytes(): string

GT\Cipher\InitVector

  • __construct(int $byteLength = SODIUM_CRYPTO_BOX_NONCEBYTES)
  • getBytes(): string
  • withBytes(string $bytes): self
  • __toString(): string

GT\Cipher\CipherText

  • __construct(string $data, InitVector $iv, Key $key)
  • __toString(): string
  • getBytes(): string
  • getUri(string|Psr\Http\Message\UriInterface $baseUri = ""): Psr\Http\Message\UriInterface

GT\Cipher\EncryptedUri

  • __construct(string|Psr\Http\Message\UriInterface $uri, string $cipherQueryStringParameter = "cipher", string $initVectorStringParameter = "iv")
  • decryptMessage(Key $key): GT\Cipher\Message\PlainTextMessage

Message classes

GT\Cipher\Message\AbstractMessage

  • __construct(string $data, ?GT\Cipher\InitVector $iv = null)
  • __toString(): string
  • getIv(): GT\Cipher\InitVector

GT\Cipher\Message\PlainTextMessage

  • encrypt(GT\Cipher\Key $key): GT\Cipher\CipherText

GT\Cipher\Message\EncryptedMessage

  • decrypt(GT\Cipher\Key $key): GT\Cipher\Message\PlainTextMessage

Exception classes

Base type

  • GT\Cipher\CipherException

Encryption and URI exceptions

  • GT\Cipher\EncryptionFailureException
  • GT\Cipher\MissingQueryStringException
  • GT\Cipher\UriDecryptionFailureException

Message exceptions

  • GT\Cipher\Message\DecryptionFailureException

Typical data flow

Separate fields

$message = new GT\Cipher\Message\PlainTextMessage("hello");
$cipherText = $message->encrypt($key);

$received = new GT\Cipher\Message\EncryptedMessage(
	(string)$cipherText,
	$message->getIv(),
);

$plainText = $received->decrypt($key);

Query string transport

$message = new GT\Cipher\Message\PlainTextMessage("hello");
$cipherText = $message->encrypt($key);
$uri = $cipherText->getUri("https://example.com/");

$received = new GT\Cipher\EncryptedUri((string)$uri);
$plainText = $received->decryptMessage($key);

Related package requirements

  • PHP >= 8.4
  • ext-sodium
  • phpgt/http