ietf版chacha20 nonce为12字节,openssl_encrypt传递iv参数最低16字节,将nonce前面以\0补充到16字节。
<?php
$key = hash('sha256', "pass", true);
$nonce = random_bytes(12);
$msg = "message";
$cipher_str = openssl_encrypt($msg, 'chacha20', $key, OPENSSL_NO_PADDING, "\0\0\0\0".$nonce);
echo "cipher hex: " . bin2hex($cipher_str) . PHP_EOL;
$plain_str = openssl_decrypt($cipher_str, 'chacha20', $key, OPENSSL_NO_PADDING, "\0\0\0\0".$nonce);
echo "plain text: ".$plain_str.PHP_EOL;
标签:none