大网数据广告活动
PHP 语言与运行手册

crypt_gensalt · Compile a string for use as the salt argument to crypt

· 52CE 我爱测

crypt_gensalt — Compile a string for use as the salt argument to crypt

crypt_gensalt

(PECL xpass >= 1.1.0)

crypt_gensalt — Compile a string for use as the salt argument to crypt

说明

function crypt_gensalt(string $prefix = null, int $count = 0): ?string

Compile a string for use as the salt argument to crypt().

参数

prefix

Hashing method. One of the CRYPT_PREFIX_* constant. If null, the best available hashing method will be selected.

count

Controls the processing cost of the hash; the valid range and exact meaning of count depend on the hashing method, but larger numbers correspond to more costly hashes in terms of CPU time and possibly memory usage. If count is 0, a low default cost will be selected.

返回值

Returns a string with the setting, or null in case of an error.

示例

示例 #1 A crypt_gensalt() example

<?php
// Generate the salt
$salt = crypt_gensalt(CRYPT_PREFIX_BLOWFISH);
// Hash the password
$hash = crypt("secret", $salt);
// Check the hash
$test = hash_equals(crypt("secret", $hash), $hash);
var_dump($salt, $hash, $test);
?>

以上示例会输出:

string(29) "$2y$05$GcPykP.Am8C1.dGamdpwW."
string(60) "$2y$05$GcPykP.Am8C1.dGamdpwW.1RR.7uicWvJPZfJfCEizZHqVWwuaJLm"
bool(true)

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。