略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: sodium_crypto_secretbox_keygen

2024-05-05

sodium_crypto_secretbox_keygen

(PHP 7 >= 7.2.0, PHP 8)

sodium_crypto_secretbox_keygenGenerate random key for sodium_crypto_secretbox

说明

sodium_crypto_secretbox_keygen(): string

Generate a key for use with sodium_crypto_secretbox() and sodium_crypto_secretbox_open().

参数

此函数没有参数。

返回值

Returns the generated string of cryptographically secure random bytes.

范例

示例 #1 sodium_crypto_secretbox_keygen() example

<?php
$key 
sodium_crypto_secretbox_keygen();
var_dumpsodium_bin2hex$key ) );
?>

以上例程的输出类似于:

string(64) "88bd1dc51ec81984f3ddc5a8f59a3d95b647e2da3e879f1b9ceb0abd89e7286c"

示例 #2 Comparing sodium_crypto_secretbox_keygen() with random_bytes()

<?php
$key 
sodium_crypto_secretbox_keygen();
$bytes random_bytesSODIUM_CRYPTO_SECRETBOX_KEYBYTES );
var_dumpmb_strlen$key'8bit' ) === mb_strlen$bytes'8bit' ) );
?>

以上例程会输出:

bool(true)

参见

add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/function.sodium-crypto-secretbox-keygen.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3