加密解密
字符串加密解密支持。
引入相关类
public function testBaseUse()
{
$encryption = new Encryption('encode-key');
$this->assertInstanceof(IEncryption::class, $encryption);
$sourceMessage = '123456';
$encodeMessage = $encryption->encrypt($sourceMessage);
$this->assertFalse($sourceMessage === $encodeMessage);
$this->assertSame(
$encryption->decrypt($encodeMessage),
$sourceMessage
);
$this->assertSame(
$encryption->decrypt($encodeMessage.'foo'),
''
);
$this->assertSame(
'encode-key',
$this->getTestProperty($encryption, 'key')
);
}
加密解密 AES-128-CBC
public function testUse128()
{
$encryption = new Encryption('encode-key', 'AES-128-CBC');
$this->assertInstanceof(IEncryption::class, $encryption);
$sourceMessage = '123456';
$encodeMessage = $encryption->encrypt($sourceMessage);
$this->assertFalse($sourceMessage === $encodeMessage);
$this->assertSame(
$encryption->decrypt($encodeMessage),
$sourceMessage
);
$this->assertSame(
$encryption->decrypt($encodeMessage.'foo'),
''
);
$this->assertSame(
'encode-key',
$this->getTestProperty($encryption, 'key')
);
}
加密解密支持过期时间
public function testDecryptButExpired()
{
$encryption = new Encryption('encode-key');
$this->assertInstanceof(IEncryption::class, $encryption);
$data = $encryption->encrypt('123456', 1);
$this->assertSame('123456', $encryption->decrypt($data));
sleep(2);
$this->assertSame('', $encryption->decrypt($data));
}
加密解密支持 RSA 校验
public function testWithPublicAndPrimaryKey()
{
$encryption = new Encryption(
'encode-key', 'AES-256-CBC',
file_get_contents(__DIR__.'/assert/rsa_private_key.pem'),
file_get_contents(__DIR__.'/assert/rsa_public_key.pem')
);
$this->assertInstanceof(IEncryption::class, $encryption);
$sourceMessage = '123456';
$encodeMessage = $encryption->encrypt($sourceMessage);
$this->assertFalse($sourceMessage === $encodeMessage);
$this->assertSame(
$encryption->decrypt($encodeMessage),
$sourceMessage
);
}