PHP core 整合加密库 Libsodium

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Halite 是PHP项目中一个简单的libsodium封装包。Halite提供高级的加密接口,依靠其所有潜在的密码操作libsodium。Halite 基本 API:EncryptionAnonymousAuthenticatedAsymmetric\Crypto::seal(HiddenString, EncryptionPublicKey): stringAsymmetric\Crypto::unseal(string, EncryptionSecretKey): HiddenStringAsymmetric\Crypto::encrypt(HiddenString, EncryptionSecretKey, EncryptionPublicKey): stringAsymmetric\Crypto::decrypt(string, EncryptionSecretKey, EncryptionPublicKey): HiddenStringSymmetric\Crypto::encrypt(HiddenString, EncryptionKey): stringSymmetric\Crypto::decrypt(string, EncryptionKey): HiddenStringSymmetricAsymmetricAuthenticationAsymmetric\Crypto::sign(string, SignatureSecretKey): stringAsymmetric\Crypto::verify(string, SignaturePublicKey, string): boolSymmetric\Crypto::authenticate(string, AuthenticationKey): stringSymmetric\Crypto::verify(string, AuthenticationKey, string): boolSymmetricAsymmetric示例1:加密解密消息首先,生成一个key<?php use ParagonIE\Halite\KeyFactory; $encKey = KeyFactory::generateEncryptionKey(); KeyFactory::save($encKey, '/path/outside/webroot/encryption.key');然后,加密解密消息<?php use ParagonIE\Halite\HiddenString; use ParagonIE\Halite\KeyFactory; use ParagonIE\Halite\Symmetric\Crypto as Symmetric; $encryptionKey = KeyFactory::loadEncryptionKey('/path/outside/webroot/encryption.key'); $message = new HiddenString('This is a confidential message for your eyes only.'); $ciphertext = Symmetric::encrypt($message, $encryptionKey); $decrypted = Symmetric::decrypt($ciphertext, $encryptionKey); var_dump($decrypted === $message); // bool(true)示例2:用password-derived key 加密消息<?php use ParagonIE\Halite\HiddenString; use ParagonIE\Halite\KeyFactory; use ParagonIE\Halite\Symmetric\Crypto as Symmetric; $passwd = new HiddenString('correct horse battery staple'); // Use random_bytes(16); to generate the salt: $salt = "\xdd\x7b\x1e\x38\x75\x9f\x72\x86\x0a\xe9\xc8\x58\xf6\x16\x0d\x3b"; $encryptionKey = KeyFactory::deriveEncryptionKey($passwd, $salt); $message = new HiddenString('This is a confidential message for your eyes only.'); $ciphertext = Symmetric::encrypt($message, $encryptionKey); echo $ciphertext, "\n"; 标签:Halite
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值