PHPackages                             tourze/tls-crypto-asymmetric - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. tourze/tls-crypto-asymmetric

ActiveLibrary[Security](/categories/security)

tourze/tls-crypto-asymmetric
============================

TLS非对称加密算法实现

0.0.1(11mo ago)041MITPHPPHP ^8.1CI passing

Since May 25Pushed 4mo agoCompare

[ Source](https://github.com/tourze/tls-crypto-asymmetric)[ Packagist](https://packagist.org/packages/tourze/tls-crypto-asymmetric)[ RSS](/packages/tourze-tls-crypto-asymmetric/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

TLS-Crypto-Asymmetric
=====================

[](#tls-crypto-asymmetric)

[English](README.md) | [中文](README.zh-CN.md)

[![Latest Stable Version](https://camo.githubusercontent.com/6aee4f5404070e4d8e1b118cfdfbbb31fcee070ef740d84775dea50f018f4158/68747470733a2f2f706f7365722e707567782e6f72672f746f75727a652f746c732d63727970746f2d6173796d6d65747269632f76)](https://packagist.org/packages/tourze/tls-crypto-asymmetric)[![Total Downloads](https://camo.githubusercontent.com/2ded388e27571c3ba23e6ca041510ed8111ff022ec27de112aeff070bade3374/68747470733a2f2f706f7365722e707567782e6f72672f746f75727a652f746c732d63727970746f2d6173796d6d65747269632f646f776e6c6f616473)](https://packagist.org/packages/tourze/tls-crypto-asymmetric)[![License](https://camo.githubusercontent.com/57a7a2f6fbdec0ebc296d3b49e51817e8a709528585319b55c1429684ff21968/68747470733a2f2f706f7365722e707567782e6f72672f746f75727a652f746c732d63727970746f2d6173796d6d65747269632f6c6963656e7365)](https://packagist.org/packages/tourze/tls-crypto-asymmetric)[![PHP Version](https://camo.githubusercontent.com/7663c9d53dc13cedaf0660a8745a7e77d2dd711257f36aa86ebce12a0600ef42/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e312d626c75652e737667)](https://php.net/)![Coverage Status](https://camo.githubusercontent.com/4d261e778f0d76db8917d801e8a0793159b5d1a83059ecf6bc1a7940850edae1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d25334539302532352d627269676874677265656e2e737667)

This package provides comprehensive asymmetric cryptography implementations for the TLS protocol.

Table of Contents
-----------------

[](#table-of-contents)

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
    - [RSA Encryption](#rsa-encryption)
    - [ECDSA Signatures](#ecdsa-signatures)
    - [Ed25519 Signatures](#ed25519-signatures)
    - [Ed448 Signatures](#ed448-signatures)
    - [DSA Signatures](#dsa-signatures)
    - [Signature Verification Utility](#signature-verification-utility)
- [Advanced Usage](#advanced-usage)
- [Supported Algorithms](#supported-algorithms)
- [Key Pair Structure](#key-pair-structure)
- [Exception Handling](#exception-handling)
- [Requirements](#requirements)
- [Testing](#testing)
- [Contributing](#contributing)
- [License](#license)

Features
--------

[](#features)

- **RSA** encryption and signatures (with PKCS#1, OAEP padding)
- **ECDSA** implementation (with multiple curve support)
- **EdDSA** (Ed25519, Ed448) implementation
- **DSA** and traditional signature algorithms
- **Key format handling** (PKCS#1, PKCS#8, etc.)
- **Signature verification** utilities
- **High-performance** cryptographic operations
- **Production-ready** with comprehensive test coverage
- **Easy-to-use** API with consistent interfaces

Installation
------------

[](#installation)

```
composer require tourze/tls-crypto-asymmetric
```

Usage
-----

[](#usage)

### RSA Encryption

[](#rsa-encryption)

```
use Tourze\TLSCryptoAsymmetric\Cipher\RSA;

$rsa = new RSA();

// Generate key pair
$keyPair = $rsa->generateKeyPair(['keySize' => 2048]);

// Encrypt data
$plaintext = 'Hello, World!';
$ciphertext = $rsa->encrypt($plaintext, $keyPair['publicKey']);

// Decrypt data
$decrypted = $rsa->decrypt($ciphertext, $keyPair['privateKey']);

// Sign data
$signature = $rsa->sign($plaintext, $keyPair['privateKey']);

// Verify signature
$isValid = $rsa->verify($plaintext, $signature, $keyPair['publicKey']);
```

### ECDSA Signatures

[](#ecdsa-signatures)

```
use Tourze\TLSCryptoAsymmetric\Cipher\ECDSA;

$ecdsa = new ECDSA();

// Generate key pair with specific curve
$keyPair = $ecdsa->generateKeyPair(['curve' => 'prime256v1']);

// Sign data
$data = 'Message to sign';
$signature = $ecdsa->sign($data, $keyPair['privateKey']);

// Verify signature
$isValid = $ecdsa->verify($data, $signature, $keyPair['publicKey']);
```

### Ed25519 Signatures

[](#ed25519-signatures)

```
use Tourze\TLSCryptoAsymmetric\Cipher\Ed25519;

$ed25519 = new Ed25519();

// Generate key pair
$keyPair = $ed25519->generateKeyPair();

// Sign data
$data = 'Message to sign';
$signature = $ed25519->sign($data, $keyPair['privateKey']);

// Verify signature
$isValid = $ed25519->verify($data, $signature, $keyPair['publicKey']);
```

### Ed448 Signatures

[](#ed448-signatures)

```
use Tourze\TLSCryptoAsymmetric\Cipher\Ed448;

$ed448 = new Ed448();

// Generate key pair
$keyPair = $ed448->generateKeyPair();

// Sign data
$data = 'Message to sign';
$signature = $ed448->sign($data, $keyPair['privateKey']);

// Verify signature
$isValid = $ed448->verify($data, $signature, $keyPair['publicKey']);
```

### DSA Signatures

[](#dsa-signatures)

```
use Tourze\TLSCryptoAsymmetric\Cipher\DSA;

$dsa = new DSA();

// Generate key pair
$keyPair = $dsa->generateKeyPair(['keySize' => 2048]);

// Sign data
$data = 'Message to sign';
$signature = $dsa->sign($data, $keyPair['privateKey']);

// Verify signature
$isValid = $dsa->verify($data, $signature, $keyPair['publicKey']);
```

### Signature Verification Utility

[](#signature-verification-utility)

```
use Tourze\TLSCryptoAsymmetric\Signature\SignatureVerifier;

$verifier = new SignatureVerifier();

// Verify signature with algorithm auto-detection
$isValid = $verifier->verify($data, $signature, $publicKey, $algorithm);
```

### KeyPair Utility Class

[](#keypair-utility-class)

```
use Tourze\TLSCryptoAsymmetric\KeyPair\KeyPair;

// Create KeyPair from array
$keyPairArray = $rsa->generateKeyPair();
$keyPair = KeyPair::fromArray($keyPairArray);

// Access keys
$privateKey = $keyPair->getPrivateKey();
$publicKey = $keyPair->getPublicKey();

// Convert back to array
$arrayFormat = $keyPair->toArray();
```

Advanced Usage
--------------

[](#advanced-usage)

### Custom RSA Key Generation

[](#custom-rsa-key-generation)

```
$rsa = new RSA();

// Generate RSA key with custom parameters
$keyPair = $rsa->generateKeyPair([
    'keySize' => 4096,
    'digest_alg' => 'sha256',
    'private_key_type' => OPENSSL_KEYTYPE_RSA
]);
```

### ECDSA with Custom Curves

[](#ecdsa-with-custom-curves)

```
$ecdsa = new ECDSA();

// Use specific elliptic curve
$keyPair = $ecdsa->generateKeyPair(['curve' => 'secp384r1']);

// Available curves: prime256v1, secp384r1, secp521r1
```

### Error Handling Best Practices

[](#error-handling-best-practices)

```
try {
    $rsa = new RSA();
    $keyPair = $rsa->generateKeyPair();
    $encrypted = $rsa->encrypt($data, $keyPair['publicKey']);
} catch (AsymmetricCipherException $e) {
    // Handle cryptographic errors
    error_log('Crypto error: ' . $e->getMessage());
} catch (\Exception $e) {
    // Handle other errors
    error_log('General error: ' . $e->getMessage());
}
```

Supported Algorithms
--------------------

[](#supported-algorithms)

### RSA

[](#rsa)

- Key sizes: 512 (test only), 1024, 2048, 3072, 4096 bits
- Padding: PKCS#1 v1.5, OAEP
- Supports encryption/decryption and signing/verification

### ECDSA

[](#ecdsa)

- Curves: secp256r1 (prime256v1), secp384r1, secp521r1, and many others
- Supports signing/verification only

### EdDSA

[](#eddsa)

- **Ed25519**: High-speed, high-security signatures
- **Ed448**: Enhanced security with larger key size
- Supports signing/verification only

### DSA

[](#dsa)

- Key sizes: 1024, 2048, 3072 bits
- Supports signing/verification only

Key Pair Structure
------------------

[](#key-pair-structure)

All algorithms return key pairs in the following format:

```
[
    'privateKey' => '-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----',
    'publicKey' => '-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----'
]
```

Exception Handling
------------------

[](#exception-handling)

The package provides specific exceptions for different error conditions:

- `AsymmetricCipherException`: General cryptographic errors
- `CryptoException`: Base cryptographic exception
- `InvalidKeyPairException`: Invalid key pair errors
- `InvalidSignatureAlgorithmException`: Unsupported algorithm errors

```
try {
    $keyPair = $rsa->generateKeyPair(['keySize' => 9999]);
} catch (AsymmetricCipherException $e) {
    echo 'Error: ' . $e->getMessage();
}
```

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- OpenSSL extension
- Sodium extension (for Ed25519/Ed448)
- Hash extension

Testing
-------

[](#testing)

Run the test suite:

```
vendor/bin/phpunit
```

Contributing
------------

[](#contributing)

Contributions are welcome! Please feel free to submit a Pull Request.

License
-------

[](#license)

MIT

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance63

Regular maintenance activity

Popularity8

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

352d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e354fdb316da535dfa8ba2e9193a473c403b6bc6fb9170778d1dc50e304c6e9d?d=identicon)[tourze](/maintainers/tourze)

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tourze-tls-crypto-asymmetric/health.svg)

```
[![Health](https://phpackages.com/badges/tourze-tls-crypto-asymmetric/health.svg)](https://phpackages.com/packages/tourze-tls-crypto-asymmetric)
```

###  Alternatives

[paragonie/paseto

Platform-Agnostic Security Tokens

3.4k1.3M18](/packages/paragonie-paseto)[paragonie/ciphersweet

Searchable field-level encryption library for relational databases

4641.2M21](/packages/paragonie-ciphersweet)[paragonie/certainty

Up-to-date, verifiable repository for Certificate Authorities

2642.4M20](/packages/paragonie-certainty)[singpolyma/openpgp-php

Pure-PHP implementation of the OpenPGP Message Format (RFC 4880)

184795.6k10](/packages/singpolyma-openpgp-php)[sarciszewski/php-future

Polyfill new (5.6+) features into old (5.4+) versions of PHP

411.5M7](/packages/sarciszewski-php-future)[payu/apple-pay

ApplePay Token Decoder

51983.2k](/packages/payu-apple-pay)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
