PHP sha1() 函數
實例
計算字串 "Hello" 的 SHA-1 散列:
<?php
$str = "Hello";
echo sha1($str);
?>
$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 | 可選。規定十六進制或二進位輸出格式:
|
技術細節
返回值: | 如果成功則返回已計算的 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>";
?>
$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;
}
?>
$str = "Hello";
echo sha1($str);
if (sha1($str) == "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0")
{
echo "<br>Hello world!";
exit;
}
?>
