PHPackages                             tourze/tls-crypto - 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

ActiveLibrary[Security](/categories/security)

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

TLS加密

0.0.1(1y ago)06011MITPHPPHP ^8.1CI passing

Since May 19Pushed 1y ago1 watchersCompare

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

READMEChangelog (1)Dependencies (5)Versions (2)Used By (1)

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

[](#tls-crypto)

此包实现了TLS协议所需的加密算法和功能，包括：

功能特性
----

[](#功能特性)

- 对称加密算法：AES-GCM、AES-CBC、ChaCha20-Poly1305、3DES等
- 非对称加密算法：RSA、ECDSA、EdDSA、DSA等
- 密钥交换算法：ECDHE、DHE、RSA等
- 哈希函数：SHA-256、SHA-384、SHA-512、MD5等
- 消息认证码：HMAC、AEAD、GMAC等
- 随机数生成器：CSPRNG
- 密钥导出函数：HKDF
- 曲线实现：P-256、P-384、X25519、X448等

使用要求
----

[](#使用要求)

- PHP 8.1+
- ext-ctype
- psr/log

安装
--

[](#安装)

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

基本用法
----

[](#基本用法)

### 随机数生成

[](#随机数生成)

```
use Tourze\TLSCrypto\CryptoFactory;

// 创建随机数生成器
$random = CryptoFactory::createRandom();

// 生成16字节的随机数
$randomBytes = $random->getRandomBytes(16);

// 生成1-100范围内的随机整数
$randomInt = $random->getRandomInt(1, 100);
```

### 哈希函数

[](#哈希函数)

```
use Tourze\TLSCrypto\CryptoFactory;

// 创建SHA-256哈希函数
$hash = CryptoFactory::createHash('sha256');

// 计算哈希值
$data = 'Hello, World!';
$hashValue = $hash->hash($data);

// 使用增量哈希
$context = $hash->createContext();
$hash->updateContext($context, 'Hello, ');
$hash->updateContext($context, 'World!');
$hashValue = $hash->finalizeContext($context);
```

### HMAC消息认证码

[](#hmac消息认证码)

```
use Tourze\TLSCrypto\CryptoFactory;

// 创建基于SHA-256的HMAC
$hmac = CryptoFactory::createMac('hmac-sha256');

// 计算HMAC
$data = 'Message to authenticate';
$key = $random->getRandomBytes(32);
$mac = $hmac->compute($data, $key);

// 验证HMAC
$isValid = $hmac->verify($data, $mac, $key);
```

### AES-GCM加密

[](#aes-gcm加密)

```
use Tourze\TLSCrypto\CryptoFactory;

// 创建AES-256-GCM加密算法
$cipher = CryptoFactory::createCipher('aes-256-gcm');

// 生成随机密钥和IV
$random = CryptoFactory::createRandom();
$key = $random->getRandomBytes($cipher->getKeyLength());
$iv = $random->getRandomBytes($cipher->getIVLength());

// 加密数据
$plaintext = 'Secret message';
$aad = 'Additional authenticated data';
$tag = null;
$ciphertext = $cipher->encrypt($plaintext, $key, $iv, $aad, $tag);

// 解密数据
$decrypted = $cipher->decrypt($ciphertext, $key, $iv, $aad, $tag);
```

### HKDF密钥导出

[](#hkdf密钥导出)

```
use Tourze\TLSCrypto\CryptoFactory;

// 创建基于SHA-256的HKDF
$kdf = CryptoFactory::createKdf('hkdf-sha256');

// 导出密钥
$secret = 'Master secret';
$salt = 'Salt value';
$info = 'Key expansion';
$length = 32; // 导出32字节的密钥材料
$derivedKey = $kdf->derive($secret, $salt, $info, $length);
```

许可证
---

[](#许可证)

本项目采用MIT许可证，详情请查看[LICENSE](LICENSE)文件。

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance46

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity36

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

411d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/13899502?v=4)[tourze](/maintainers/tourze)[@tourze](https://github.com/tourze)

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[laravel/socialite

Laravel wrapper around OAuth 1 &amp; OAuth 2 libraries.

5.7k108.5M885](/packages/laravel-socialite)[paragonie/ciphersweet

Searchable field-level encryption library for relational databases

4671.4M24](/packages/paragonie-ciphersweet)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751291.4k43](/packages/civicrm-civicrm-core)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.6M574](/packages/shopware-core)[shopware/administration

Administration frontend for the Shopware Core

414.3M116](/packages/shopware-administration)[soneso/stellar-php-sdk

Stellar PHP SDK for the Stellar Network

4154.3k4](/packages/soneso-stellar-php-sdk)

PHPackages © 2026

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