Halite 介绍
Halite
是PHP项目中一个简单的libsodium封装包。Halite提供高级的加密接口,依靠其所有潜在的密码操作libsodium。
Halite 基本 API:
-
Encryption
-
Anonymous
-
Authenticated
-
asymmetric\Crypto::seal(HiddenString, EncryptionPublicKey): string
-
asymmetric\Crypto::unseal(string, EncryptionSecretKey): HiddenString
-
asymmetric\Crypto::encrypt(HiddenString, EncryptionSecretKey, EncryptionPublicKey): string
-
asymmetric\Crypto::decrypt(string, EncryptionSecretKey, EncryptionPublicKey): HiddenString
-
Symmetric\Crypto::encrypt(HiddenString, EncryptionKey): string
-
Symmetric\Crypto::decrypt(string, EncryptionKey): HiddenString
-
Symmetric
-
-
Authentication
-
asymmetric\Crypto::sign(string, SignatureSecretKey): string
-
asymmetric\Crypto::verify(string, SignaturePublicKey, string): bool
-
Symmetric\Crypto::authenticate(string, AuthenticationKey): string
-
Symmetric\Crypto::verify(string, AuthenticationKey, string): bool
-
Symmetric
-
示例1:加密解密消息
<?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 官网
https://paragonie.com/project/halite
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。