PHP sha1_file()
Function
Example
Calculate the SHA-1 hash of the text file "test.txt":
The above code will output:
Definition and Usage
The sha1_file()
function calculates the SHA-1 hash of a file.
The sha1_file()
function uses the United States Secure Hash Algorithm 1.
Explanation from RFC 3174 - United States 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 message signatures. Signing the message digest rather than the message often improves the efficiency of the process, as the message digest is usually much smaller than the message. The digital signature verifier must use the same hash algorithm as the digital signature creator.
Returns the computed SHA-1 hash on success, or FALSE on failure.
Syntax
Parameter | Description |
---|---|
file | Required. Specifies the file to be calculated. |
raw | Optional. A boolean value that specifies hexadecimal or binary output format: TRUE - Raw 20-character binary format<br> FALSE - Default. 40-character hexadecimal number |
Technical Details
Return Value: | Returns the computed SHA-1 hash on success, or FALSE on failure. |
---|---|
PHP Version: | 4.3.0+ |
--- | --- |
Changelog: | In PHP 5.0, the raw parameter became optional. <br> <br>Since PHP 5.1, sha1_file() can be used with wrappers. For example: sha1_file("http://w3cschool.cc/..") |
--- | --- |
More Examples
Example 1
Store the SHA-1 hash of "test.txt" in a file:
Check if "test.txt" has been altered (i.e., if the SHA-1 hash has changed):
The above code will output: