Latest web development tutorials

PHP sha1 () function

PHP String Reference PHP String Reference

Examples

Calculation string "Hello" in the SHA-1 hash:

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

Running instance »

Definition and Usage

sha1 () function calculates a string SHA-1 hash.

sha1 () function uses the Secure Hash Algorithm 1.

Explanation from RFC 3174 - The US Secure Hash Algorithm1:SHA-1produces a message digest called 160-bit output.Message Digest can be input to generate a packet or a signature verification algorithm.Signing on the message digest rather than the message is signed, this can increase the efficiency of the process, because the message digest size is usually much smaller than the message.Who must verify the digital signature as a digital signature creator, like using the same hashing algorithm.

Tip: To calculate the SHA-1 hash files, use sha1_file () function.


grammar

sha1( string,raw )

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

technical details

return value: If successful, it returns the computed SHA-1 hash, if it fails it returns FALSE.
PHP version: 4.3.0+
Update log: In PHP 5.0 in, raw parameter becomes optional.


More examples

Example 1

Output sha1 () result:

<?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>";
?>

Running instance »

Example 2

Results output sha1 () and test it in:

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

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

Running instance »


PHP String Reference PHP String Reference