PHP sha1() 函数

PHP 教程 · 2019-01-29 20:56:56

实例

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

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

定义和用法

sha1() 函数计算字符串的 SHA-1 散列。

sha1() 函数使用美国 Secure Hash 算法 1。

来自 RFC 3174 的解释 - 美国 Secure Hash 算法 1:SHA-1 产生一个名为报文摘要的 160 位的输出。报文摘要可以被输入到一个可生成或验证报文签名的签名算法。对报文摘要进行签名,而不是对报文进行签名,这样可以提高进程效率,因为报文摘要的大小通常比报文要小很多。数字签名的验证者必须像数字签名的创建者一样,使用相同的散列算法。

提示:如需计算文件的 SHA-1 散列,请使用 sha1_file() 函数。

语法


sha1(string,raw)


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

技术细节

返回值: 如果成功则返回已计算的 SHA-1 散列,如果失败则返回 FALSE。
PHP 版本: 4.3.0+
更新日志: 在 PHP 5.0 中,raw 参数变成可选的。

更多实例

实例 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 教程 文章: https://codercto.com/courses/l/5.html

查看所有标签

Weaving the Web

Weaving the Web

Tim Berners-Lee / Harper Paperbacks / 2000-11-01 / USD 15.00

Named one of the greatest minds of the 20th century by Time , Tim Berners-Lee is responsible for one of that century's most important advancements: the world wide web. Now, this low-profile genius-wh......一起来看看 《Weaving the Web》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具