for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php declare(strict_types=1);
namespace ncryptf;
use InvalidArgumentException;
use SodiumException;
class Request
{
/**
* Sodium CryptoBox Keypair
*
* @var string
*/
private $keypair;
* 24 byte nonce
private $nonce;
$nonce
* Constructor
* @param string $secretKey The 32 byte secret key
* @param string $publicKey The 32 byte public key
public function __construct(string $secretKey, string $publicKey)
try {
$this->keypair = \sodium_crypto_box_keypair_from_secretkey_and_publickey(
$secretKey,
$publicKey
);
} catch (SodiumException $e) {
throw new InvalidArgumentException($e->getMessage());
}
* Encrypts a request
* @param string $request The raw HTTP request as a string
* @param string $nonce Optional nonce. If not provided, a 24 byte nonce will be generated
* @return string
public function encrypt(string $request, string $nonce = null)
if ($nonce === null) {
$nonce = \random_bytes(SODIUM_CRYPTO_BOX_NONCEBYTES);
return \sodium_crypto_box(
$request,
$nonce,
$this->keypair
* Creates a detached signature for the keypair
* @param string $request
* @param string $secretKey
public function sign(string $request, string $secretKey)
return \sodium_crypto_sign_detached(
$secretKey