Sodium 是 PHP 中用于加密、解密和生成哈希的现代加密库。它提供了一组易于使用的加密工具,能够帮助开发者实现安全的数据存储和通信。以下是一些简单的用法示例:
<?php
// 生成随机密钥
$encryption_key = sodium_crypto_secretbox_keygen();
// 要加密的数据
$message = "Hello, Sodium!";
// 生成随机的 nonce
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
// 加密数据
$ciphertext = sodium_crypto_secretbox($message, $nonce, $encryption_key);
// 解密数据
$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $encryption_key);
echo "原始数据: $message\n";
echo "加密数据:$ciphertext";
echo "解密后的数据: $plaintext\n";
?>
$password = "my_secret_password";
$hash = sodium_crypto_generichash($password);
echo "原始密码: $password\n";
echo "Hash: $hash\n";