最新的Web开发教程
 

PHP sha1() Function

<PHP字符串参考

计算字符串的SHA-1散列"Hello"

<?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_file()函数。


句法

sha1( string,raw )

参数 描述
string 需要。 要计算的字符串
raw 可选的。 指定十六进制或二进制输出格式:
  • - 最原始的20个字符的二进制格式
  • FALSE - 默认。 40个字符十六进制数

技术细节

返回值: 成功则返回计算的SHA-1散列,或FALSE的失败
PHP版本: 4.3.0+
更新日志: 原始参数在PHP 5.0起成为可选项

更多示例

实施例1

打印结果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>";
?>
运行示例»

实施例2

打印结果sha1()并对其进行测试:

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

if (sha1($str) == "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0")
  {
  echo "<br>Hello world!";
  exit;
  }
?>
运行示例»

<PHP字符串参考