Box class

Framework\Crypto\Box

Class Box.

Defined in libraries/crypto/src/Box.php

Properties #

$secretKey
-
$publicKey
-
$nonce
-

$secretKey

protected string $secretKey

$publicKey

protected string $publicKey

$nonce

protected ?string $nonce

Methods #

__construct()
Box constructor.
decrypt()
Decrypts a box message ciphertext.
encrypt()
Encrypts a box message.
makeKeyPair()
Makes a keypair.
makeNonce()
Makes a box nonce with the correct length.
makePublicKey()
Makes the public key from a keypair.
makeSecretKey()
Makes the secret key from a keypair.
getKeyPair()
Gets the keypair from the secret and public keys.
getNonce()
-
validateNonce()
Validates a nonce.

__construct()

public function __construct(string $secretKey, string $publicKey, ?string $nonce = NULL)

Box constructor.

Parameters
  • string $secretKey
  • string $publicKey
  • ?string $nonce
throws
LengthException if nonce is set has not the required length

decrypt()

public function decrypt(string $ciphertext, ?string $nonce = NULL): string|false

Decrypts a box message ciphertext.

Parameters
  • string $ciphertext
  • ?string $nonce - The message nonce or null to use the nonce set int the constructor
Returns string|false
throws
LengthException if nonce is set and has not the required length
throws
LogicException if nonce param is null and nonce was not set in the constructor
throws
SodiumException

encrypt()

public function encrypt(string $message, ?string $nonce = NULL): string

Encrypts a box message.

Parameters
  • string $message
  • ?string $nonce - The message nonce or null to use the nonce set int the constructor
Returns string
throws
LengthException if nonce is set and has not the required length
throws
LogicException if nonce param is null and nonce was not set in the constructor
throws
SodiumException

makeKeyPair()

public static function makeKeyPair(): string

Makes a keypair.

Returns string
throws
SodiumException

makeNonce()

public static function makeNonce(): string

Makes a box nonce with the correct length.

Returns string
throws
Exception if fail to get random bytes

makePublicKey()

public static function makePublicKey(string $keyPair): string

Makes the public key from a keypair.

Parameters
  • string $keyPair
Returns string
throws
SodiumException

makeSecretKey()

public static function makeSecretKey(string $keyPair): string

Makes the secret key from a keypair.

Parameters
  • string $keyPair
Returns string
throws
SodiumException

getKeyPair()

protected function getKeyPair(): string

Gets the keypair from the secret and public keys.

Returns string
throws
SodiumException

getNonce()

protected function getNonce(?string $nonce): string
Parameters
  • ?string $nonce
Returns string
throws
LengthException if nonce is set and has not the required length
throws
LogicException if nonce param is null and nonce was not set in constructor

validateNonce()

protected function validateNonce(string $nonce): void

Validates a nonce.

Parameters
  • string $nonce
Returns void
throws
LengthException if nonce has not the required length