spatie/crypto
Composer 安装命令:
composer require spatie/crypto
包简介
Encrypting and signing data using private/public keys
README 文档
README
This package allows you to easily generate a private/public key pairs, and encrypt/decrypt messages using those keys.
use Spatie\Crypto\Rsa\KeyPair; use Spatie\Crypto\Rsa\PrivateKey; use Spatie\Crypto\Rsa\PublicKey; // generating an RSA key pair [$privateKey, $publicKey] = (new KeyPair())->generate(); // when passing paths, the generated keys will be written those paths (new KeyPair())->generate($pathToPrivateKey, $pathToPublicKey); $data = 'my secret data'; $privateKey = PrivateKey::fromFile($pathToPrivateKey); $encryptedData = $privateKey->encrypt($data); // returns something unreadable $publicKey = PublicKey::fromFile($pathToPublicKey); $decryptedData = $publicKey->decrypt($encryptedData); // returns 'my secret data'
Most functions in this package are wrappers around openssl_* functions to improve DX.
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/crypto
Usage
You can generate a key pair using the generate function on the KeyPair class.
use Spatie\Crypto\Rsa\KeyPair; [$privateKey, $publicKey] = (new KeyPair())->generate();
You can write the keys to disk, by passing paths to the generate function.
// when passing paths, the generate keys will to those paths (new KeyPair())->generate($pathToPrivateKey, $pathToPublicKey)
You can protect the private key with a password by using the password method:
[$passwordProtectedPrivateKey, $publicKey] = (new KeyPair())->password('my-password')->generate();
When using a password to generating a private key, you will need that password when instantiating the PrivateKey class.
Loading keys
To load a key from a file use the fromFile static method.
Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey); Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey);
Alternatively, you can also create a key object using a string.
Spatie\Crypto\Rsa\PrivateKey::fromString($privateKeyString); Spatie\Crypto\Rsa\PublicKey::fromString($publicKeyString);
If the private key is password protected, you need to pass the password as the second argument.
Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey, $password); Spatie\Crypto\Rsa\PrivateKey::fromString($privateKeyString, $password);
If you do not specify the right password, a Spatie\Crypto\Exceptions\InvalidPrivateKey exception will be thrown.
Encrypting a message with a private key, decrypting with the public key
Here's how you can encrypt data using the private key, and how to decrypt it using the public key.
$data = 'my secret data'; $privateKey = Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey); $encryptedData = $privateKey->encrypt($data); // encrypted data contains something unreadable $publicKey = Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey); $decryptedData = $publicKey->decrypt($encryptedData); // decrypted data contains 'my secret data'
If decrypt cannot decrypt the given data (maybe a non-matching private key was used to encrypt the data, or maybe tampered with the data), an exception of class Spatie\Crypto\Exceptions\CouldNotDecryptData will be thrown.
Encrypting a message with a public key, decrypting with the private key
Here's how you can encrypt data using the public key, and how to decrypt it using the private key.
$data = 'my secret data'; $publicKey = Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey); $encryptedData = $publicKey->encrypt($data); // encrypted data contains something unreadable $privateKey = Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey); $decryptedData = $privateKey->decrypt($encryptedData); // decrypted data contains 'my secret data'
If decrypt cannot decrypt the given data (maybe a non-matching public key was used to encrypt the data, or maybe tampered with the data), an exception of class Spatie\Crypto\Exceptions\CouldNotDecryptData will be thrown.
Determining if the data can be decrypted
Both the PublicKey and PrivateKey class have a canDecrypt method to determine if given data can be decrypted.
Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->canDecrypt($data); // returns a boolean; Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey)->canDecrypt($data); // returns a boolean;
Signing and verifying data
The PrivateKey class has a method sign to generate a signature for the given data. The verify method on the PublicKey class can be used to verify if a signature is valid for the given data.
If verify returns true, you know for certain that the holder of the private key signed the message, and that it was not tampered with.
$signature = Spatie\Crypto\Rsa\PrivateKey::fromFile($pathToPrivateKey)->sign('my message'); // returns a string $publicKey = Spatie\Crypto\Rsa\PublicKey::fromFile($pathToPublicKey); $publicKey->verify('my message', $signature) // returns true; $publicKey->verify('my modified message', $signature) // returns false;
Alternatives
This package aims to be very lightweight and easy to use. If you need more features, consider using of one these alternatives:
A word on the usage of RSA
At the time of writing, RSA is secure enough for the use case we've built this package for.
To know more about why RSA might not be good enough for you, read this post on public-key encryption at Paragonie.com
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/crypto 适用场景与选型建议
spatie/crypto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 817.51k 次下载、GitHub Stars 达 482, 最近一次更新时间为 2020 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「crypto」 「spatie」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/crypto 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/crypto 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/crypto 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
High-level cryptographic primitives and security utilities for Maatify systems including password hashing, reversible encryption, HKDF key derivation, and key rotation.
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
统计信息
- 总下载量: 817.51k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 485
- 点击次数: 28
- 依赖项目数: 12
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-06