PHP sha1_file () function
Examples
Calculation text file "test.txt" the SHA-1 hash:
$filename = "test.txt";
$sha1file = sha1_file($filename);
echo $sha1file;
?>
The code above will output:
Definition and Usage
sha1_file () function calculates the SHA-1 hash files.
sha1_file () 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.
If successful, it returns the computed SHA-1 hash, if it fails it returns FALSE.
grammar
参数 | 描述 |
---|---|
file | 必需。规定要计算的文件。 |
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. Since PHP 5.1 onwards, you can use the package sha1_file (). For example: sha1_file ( "http://w3cschool.cc/ ..") |
More examples
Example 1
Stored in the file "test.txt" the SHA-1 hash:
$sha1file = sha1_file("test.txt");
file_put_contents("sha1file.txt",$sha1file);
?>
Detecting whether "test.txt" has been changed (that is, whether SHA-1 hash has been changed):
$sha1file = file_get_contents("sha1file.txt");
if (sha1_file("test.txt") == $sha1file)
{
echo "The file is ok.";
}
else
{
echo "The file has been changed.";
}
?>
The code above will output:
PHP String Reference