mdanter/ecc
Pure-PHP elliptic curve cryptography for ECDSA signatures and Diffie-Hellman key exchange. Supports common NIST and secp curves (including secp256k1), deterministic HMAC-based k generation, and constant-time scalar multiplication. Requires GMP.
Installation:
composer require mdanter/ecc
Add to composer.json if using Laravel’s require-dev for testing.
First Use Case:
Generate a key pair for ECDSA (e.g., nistp256):
use mdanter\ecc\EccKey;
use mdanter\ecc\EccCurve;
$curve = EccCurve::nistp256();
$key = EccKey::create($curve);
$privateKey = $key->getPrivate()->toHex();
$publicKey = $key->getPublic()->toHex();
Where to Look First:
nistp256 or secp256k1 for balance of security and performance.
$curve = EccCurve::nistp256(); // or EccCurve::secp256k1()
$key = EccKey::fromHex($privateKeyHex, $curve);
KeyManager service to handle generation, storage, and rotation.$signature = $key->sign('message_to_sign');
$signatureHex = $signature->toHex();
$publicKey = EccKey::fromHex($publicKeyHex, $curve);
$isValid = $publicKey->verify('message_to_sign', $signature);
EccSignature::verifyBatch() for multiple signatures (optimization for APIs).$peerPublicKey = EccKey::fromHex($peerPublicKeyHex, $curve);
$sharedSecret = $key->getSharedSecret($peerPublicKey);
// app/Providers/EccServiceProvider.php
public function register()
{
$this->app->singleton('ecc.key', function () {
return EccKey::create(EccCurve::nistp256());
});
}
defuse/php-encryption for hybrid encryption.Nonce (k) Reuse:
k in ECDSA breaks security. Use a CSPRNG (e.g., random_bytes()).EccKey::generateNonce() or inject a custom RNG.
$key->setNonceGenerator(function () {
return random_bytes(32); // For 256-bit curves
});
Curve Mismatch:
EccKey::getCurve()->getName() before operations.Large Key Sizes:
nistp521 keys are ~130 chars (hex). Validate storage limits (e.g., DB TEXT fields).PHP Extensions:
gmp/bcmath for performance-critical apps (this library uses pure PHP).Signature Validation:
verify() fails, check:
$key->getCurve()->getName()).hash('sha256', $message) explicitly if needed.Key Import:
EccException. Validate with:
try {
EccKey::fromHex($hexKey, $curve);
} catch (\Exception $e) {
// Handle error
}
Custom Curves:
EccCurve for non-standard curves (rarely needed).Nonce Customization:
EccKey::generateNonce() for deterministic nonces (e.g., HKDF-derived).Serialization:
JsonSerializable for API responses:
$key->jsonSerialize() => ['private' => $privateHex, 'public' => $publicHex, 'curve' => 'nistp256'];
Performance:
EccCurve instances (they’re immutable).$curves = collect(['nistp256', 'secp256k1'])->mapWithKeys(fn ($name) => [
$name => EccCurve::fromName($name)
]);
// config/ecc.php
return [
'default_curve' => 'nistp256',
'key_encryption' => env('ECC_KEY_ENCRYPTION', 'none'),
];
// app/Console/Commands/GenerateEccKey.php
public function handle()
{
$key = EccKey::create(EccCurve::nistp256());
$this->info("Private: {$key->getPrivate()->toHex()}");
}
EccKey::fromHex() with known test vectors (e.g., NIST P-256).How can I help you explore Laravel packages today?