-
Notifications
You must be signed in to change notification settings - Fork 1
ed25519_keys
The protection engine includes a Stateless Ticket validation system. By default, these tickets are secured symmetrically using AES-256-CBC encryption and signed with HMAC-SHA256.
By configuring an Ed25519 asymmetric key pair, you benefit from:
- Enhanced security: The private key used to sign tickets does not need to be present on exposed validation servers (which only require the public key).
- Excellent performance: Ed25519 offers short signatures and ultra-fast validation times without requiring access to your database.
- Zero coupling: No shared secret needs to be synchronized across your entire infrastructure.
The private key must be in PKCS#8 PEM format, and the public key in SPKI PEM format. You can generate them in three different ways.
Run the following commands in your terminal:
# 1. Generate the private key in PKCS#8 format
openssl genpkey -algorithm ed25519 -out private.pem
# 2. Extract the corresponding public key in SPKI format
openssl pkey -in private.pem -pubout -out public.pemYou can use the native crypto module to generate and output your keys:
import { generateKeyPairSync } from 'node:crypto';
const { privateKey, publicKey } = generateKeyPairSync('ed25519', {
privateKeyEncoding: { format: 'pem', type: 'pkcs8' },
publicKeyEncoding: { format: 'pem', type: 'spki' }
});
console.log("--- PRIVATE KEY (ED25519_PRIVATE_KEY) ---");
console.log(privateKey);
console.log("--- PUBLIC KEY (ED25519_PUBLIC_KEY) ---");
console.log(publicKey);If your environment has the openssl extension (PHP 8.0+), you can use this script:
<?php
if (!defined('OPENSSL_KEYTYPE_ED25519')) {
die("Ed25519 is not supported by your version of OpenSSL/PHP.\n");
}
$pkey = openssl_pkey_new(["private_key_type" => OPENSSL_KEYTYPE_ED25519]);
openssl_pkey_export($pkey, $privateKeyPem);
$details = openssl_pkey_get_details($pkey);
$publicKeyPem = $details['key'];
echo "--- PRIVATE KEY (ED25519_PRIVATE_KEY) ---\n" . $privateKeyPem . "\n";
echo "--- PUBLIC KEY (ED25519_PUBLIC_KEY) ---\n" . $publicKeyPem . "\n";For the engine to detect and use the keys, you must declare two environment variables.
PEM keys inherently contain line breaks. In .env files (e.g., via dotenv in Node or vlucas/phpdotenv in PHP), actual line breaks can cause issues. The protection engine natively handles the following two formats:
ED25519_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIP8WOfnN3k28u0B69UjV7yMvI9n7pP+qN6w7z4m6v7vX
-----END PRIVATE KEY-----"
ED25519_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEA/xY5+c3eTby7QHr1SNXvIy8j2fuk/6o3rDvPibq/u9c=
-----END PUBLIC KEY-----"The engine will automatically replace the literal character sequences \\n with actual line breaks:
ED25519_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIP8WOfnN3k28u0B69UjV7yMvI9n7pP+qN6w7z4m6v7vX\n-----END PRIVATE KEY-----"
ED25519_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEA/xY5+c3eTby7QHr1SNXvIy8j2fuk/6o3rDvPibq/u9c=\n-----END PRIVATE KEY-----"The engine is designed to be resilient to failures or missing configurations:
-
Signing (
generateStatelessTicket):
- If If
ED25519_PRIVATE_KEYis defined, the engine attempts to sign the ticket asymmetrically. A ticket generated in this way will begin with the prefixed25519.. - If the key is missing or the signature fails (e.g., corrupted key), the engine automatically and transparently switches to symmetric mode (AES-256-CBC + HMAC). An error is then logged in verbose mode.
-
Validation (
parseStatelessTicket/isTicketValid):
- If the ticket starts with
ed25519., the engine mandates the use ofED25519_PUBLIC_KEYto validate the cryptographic signature. - If the ticket lacks this prefix, the engine uses standard symmetric validation and decryption.