【函数功能】
该函数计算指定字符串的 SHA-1 散列。
【函数语法】
sha1($str, $raw_output = null)
【参数说明】
$str:必需,要计算的字符串。
$raw_output:可选,十六进制或二进制输出格式:
TRUE - 原始 16 字符二进制格式
FALSE - 默认。32 字符十六进制数
【演示程序】
<?php
/**
* sha1($str, $raw_output = null)
* **/
$str = "hello i am PHP!";
echo sha1($str);
echo '<br>';
$string = sha1($str,true);
echo $string;
echo '<br>';
//16位加密后的md5值有不可见的字符,所以我们采用下面的方法将他们的ASCII码值打印出来看一下
while(strlen($string)>0){
echo ord($string).'-';
$string = substr($string, 1,strlen($string)-1);
}
?>
【输出结果】
4db645ae5acf43ad114980a8fa62c84f6a54ccd0
M?E?Z?C?I???b?OjT??
19277-182-69-174-90-207-67-173-17-73-128-168-250-98-200-79-106-84-204-208-
【特别注意】
1.函数使用美国 Secure Hash 算法1。
2.若指定字符串为空则返回false。
3.采用16位格式时直接打印会有乱码。
4.PHP 版本:4.3.0+。
5.更新日志:在 PHP 5.0 中,$raw_output参数变为可选的。
6.来自 RFC 3174 的解释 - 美国 Secure Hash 算法 1:SHA-1 产生一个名为报文摘要的 160 位的输出。报文摘要可以被输入到一个可生成或验证报文签名的签名算法。对报文摘要进行签名,而不是对报文进行签名,这样可以提高进程效率,因为报文摘要的大小通常比报文要小很多。数字签名的验证者必须像数字签名的创建者一样,使用相同的散列算法。
7.如需计算文件的 SHA-1 散列,请使用 sha1_file() 函数。
【原版定义】
/**
* Calculate the sha1 hash of a string
* @link http://www.php.net/manual/en/function.sha1.php
* @param str string <p>
* The input string.
* </p>
* @param raw_output bool[optional] <p>
* If the optional raw_output is set to true,
* then the sha1 digest is instead returned in raw binary format with a
* length of 20, otherwise the returned value is a 40-character
* hexadecimal number.
* </p>
* @return string the sha1 hash as a string.
*/
转载请注明出处:php1234.cn ,原文地址:http://www.php1234.cn/a/functions/2016/1103/115.html