目录

PHP sha1() 函数

❮ PHP 字符串参考

示例

计算字符串 "Hello" 的 SHA-1 哈希值:

<?php
$str = "Hello";
echo sha1($str);
?>
亲自试一试 »

定义和用法

sha1() 函数计算字符串的 SHA-1 哈希值。

sha1() 函数使用美国安全哈希算法 1。

来自 RFC 3174 - 美国安全哈希算法 1:"SHA-1 produces a 160-bit output called a message digest. The message digest can then, for example, be input to a signature algorithm which generates or verifies the signature for the message. Signing the message digest rather than the message often improves the efficiency of the process because the message digest is usually much smaller in size than the message. The same hash algorithm must be used by the verifier of a digital signature as was used by the creator of the digital signature."

提示:要计算文件的 SHA-1 哈希值,请使用sha1_文件()功能。


语法

sha1( string,raw)

参数值

Parameter Description
string Required. The string to be calculated
raw Optional. Specify hex or binary output format:
  • TRUE - Raw 20 character binary format
  • FALSE - Default. 40 character hex number


技术细节

返回值: 成功时返回计算出的 SHA-1 哈希值,失败时返回 FALSE
PHP 版本: 4.3.0+

更多示例

示例

打印 sha1() 的结果:

<?php
$str = "Hello";
echo "The string: ".$str."<br>";
echo "TRUE - Raw 20 character binary format: ".sha1($str, TRUE)."<br>";
echo "FALSE - 40 character hex number: ".sha1($str)."<br>";
?>
亲自试一试 »

示例

打印 sha1() 的结果然后测试:

<?php
$str = "Hello";
echo sha1($str);

if (sha1($str) == "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0")
  {
  echo "<br>Hello world!";
  exit;
  }
?>
亲自试一试 »

❮ PHP 字符串参考