PHP sha1()Functions

PHP String 参考手册PHP String Reference Manual

Examples

Calculate the SHA-1 hash of the string "Hello":

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

Run example »

Definition and Usage

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

The sha1() function uses the US Secure Hash Algorithm 1.

From RFC 3174 explanation - US Secure Hash Algorithm 1:SHA-1 produces a 160-bit output called a message digest. The message digest can be input to a signature algorithm that generates or verifies the message signature. Signing the message digest rather than the message improves the efficiency of the process because the message digest is usually much smaller than the message. The verifier of a digital signature must use the same hash algorithm as the creator of the digital signature.

Tip:To calculate the SHA-1 hash of a file, use thesha1_file()function.

Online SHA1 test tool:SHA online encryption tool


Syntax

sha1(string,raw)

Parameter Description
string Required. Specifies the string to be calculated.
raw Optional. Specifies the hexadecimal or binary output format:
  • TRUE - Raw 20-character binary format
  • FALSE - Default. 40-character hexadecimal number

Technical Details

Return value: Returns the calculated SHA-1 hash on success, or FALSE on failure.
PHP Version: 4.3.0+
Changelog: In PHP 5.0,rawthe parameter became optional.


More Examples

Example 1

Output the result of 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>";
?>

Run example »

Example 2

Output the result of sha1() and test it:

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

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

Run example »


PHP String 参考手册PHP String Reference Manual Other Extensions