PHP sha1_file() Function
Example
Calculate the SHA-1 hash of the text file "test.txt":
The code above will output:
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
Definition and Usage
The sha1_file() function calculates the SHA-1 hash of a file.
The sha1_file() function uses the US Secure Hash Algorithm 1.
Explanation from RFC 3174 - 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 can generate or verify a message signature. Signing the message digest rather than the message improves process efficiency, since the message digest is usually much smaller than the message. The verifier of the digital signature must use the same hash algorithm as the creator of the digital signature.
Returns the calculated SHA-1 hash on success, or FALSE on failure.
Syntax
sha1_file(
file,raw
)
| Parameters | Description |
|---|---|
| file | Required. Specifies the file to calculate. |
| raw | Optional. A boolean specifying the hexadecimal or binary output format:
|
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, theraw parameter became optional. |
自 PHP 5.1 起,可以通过封装使用 sha1_file()。例如:** sha1_file("http://w3cschool.cc/..")** |
More examples
Example 1
Store the SHA-1 hash of "test.txt" in a file:
Check whether "test.txt" has been changed (i.e. whether the SHA-1 hash has been changed):
The code above will output:
The file is ok.
PHP String Reference Manual