romabeckman/encryption

一个用于加密和解密带有签名的令牌的库。使用PHP进行JWT(JSON Web Token)的编码和解码。

1.0.3 2020-02-14 22:58 UTC

This package is auto-updated.

Last update: 2024-09-15 09:08:25 UTC


README

  • 需要php 7.1
  • composer require romabeckman/encryption

示例

  • 示例1:添加签名加密和解密内容
  • 示例2:添加签名加密和解密有效载荷
  • 示例3:JWT令牌

简单加密和解密文本

$encryption = new Encryption("__ENCRYPT_KEY__", "__SECURITY_KEY__");
$hash = Encryption::encrypt($Encryption, 'My text to example'); 
echo Encryption::decrypt($Encryption, $hash);
// must print 'My text to example'

您可能需要仅加密有效载荷而不使用RFC 7519 (JWT)方法。出于某种原因,您只需加密有效载荷并通过不安全的方式传输。示例

$encryption = new Encryption("__ENCRYPT_KEY__", "__SECURITY_KEY__"); 
$Payload = new Payload(
   ['name' => 'Firstname Lastname', 'email' => 'my_email@email.com'], // must encrypt
   strtotime('2 hours') // expiration time
);
// Optional attributes
$Payload
   ->setJti(1)
   ->setSub('My Teste')
   ->setCheckIssDomain(true) // Default false. If true, will set 'iss' like http://my_host.com and check when encoded
   ->setAud('Username');
$token = Payload::encode($Encryption, $Payload);
//$token = Payload::encode($Encryption, $Payload, true); // To get token for use in url

$decoded = Payload::decode($Encryption, $token);
//$decoded = Payload::decode($Encryption, $token, true); // To get token for use in url

按照RFC 7519 (JWT)方法生成JWT令牌。仅支持:HS256、HS384和HS512。示例

$Encryption = new Encryption('__ENCRYPT_KEY__', '__SECURITY_KEY__');
$Payload = new Payload(
        ['nome' => 'Firstname Lastname', 'email' => 'my_email@email.com'],
        strtotime('2 hours')
);
$jwt = Jwt::encode($Encryption, $Payload);
$decoded = Jwt::decode($Encryption, $jwt);