PHP sha1 () function
Examples
Calculation string "Hello" in the SHA-1 hash:
$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
参数 | 描述 |
---|---|
string | 必需。规定要计算的字符串。 |
raw | 可选。规定十六进制或二进制输出格式:
|
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:
$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:
$str = "Hello";
echo sha1($str);
if (sha1($str) == "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0")
{
echo "<br>Hello world!";
exit;
}
?>
Running instance »
PHP String Reference