hash

(PHP 5 >= 5.1.2, PHP 7, PHP 8, PECL hash >= 1.1)

hash生成散列值(消息摘要)

说明

hash(
    string $algo,
    string $data,
    bool $binary = false,
    array $options = []
): string

参数

algo

要使用的散列算法,例如:“md5”、“sha256”、“haval160,4”等。在 hash_algos() 中查看支持的算法。

data

要进行散列运算的消息。

binary

设置为 true 输出原始二进制数据, 设置为 false 输出小写 16 进制字符串。

options

各种散列算法的一系列选项数组。目前 MurmurHash 算法仅支持 “seed” 参数。

返回值

如果 binary 设置为 true, 则返回原始二进制数据表示的信息摘要, 否则返回 16 进制小写字符串格式表示的信息摘要。

更新日志

版本 说明
8.1.0 新增 options 参数。
8.0.0 如果 algo 未知,现在 hash() 将抛出 ValueError 异常,之前返回 false

范例

示例 #1 hash() 示例

<?php
echo hash('ripemd160', 'The quick brown fox jumped over the lazy dog.');
?>

以上例程会输出:

ec457d0a974c48d5685a7efa03d137dc8bbde7e3

参见

  • hash_file() - 给指定文件的内容生成散列值
  • hash_hmac() - 使用 HMAC 方法生成带有密钥的散列值
  • hash_init() - 初始化增量散列运算上下文
  • md5() - 计算字符串的 MD5 散列值
  • sha1() - 计算字符串的 sha1 散列值

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top