sha1_file()
function calculates the SHA-1 hash of the file.
sha1_file()
function uses the American Secure Hash algorithm 1.
Interpretation from RFC 3174 - US Secure Hash Algorithm 1: SHA-1 produces a 160-bit output called message digest. The message digest can be input into a signature algorithm that can generate or verify the message signature. Signing the message digest instead of signing the message can improve process efficiency, because the size of the message digest is usually much smaller than that of the message. The verifier of a digital signature must use the same hashing algorithm as the creator of a digital signature.
Returns the calculated SHA-1 hash if successful, and returns FALSE if failed.
Calculate the SHA-1 hash of the text file "test.txt":
<?php $filename = "test.txt" ; $sha1file = sha1_file ( $filename ) ; echo $sha1file ; ?>
Output of the above code:
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
Storing the SHA-1 hash of "test.txt" in the file:
<?php $sha1file = sha1_file ( "test.txt" ) ; file_put_contents ( "sha1file.txt" , $sha1file ) ; ?>
Test whether "test.txt" has been modified (i.e. whether SHA-1 has been modified):
<?php $sha1file = file_get_contents ( "sha1file.txt" ) ; if ( sha1_file ( "test.txt" ) == $sha1file ) { echo "The file is ok." ; } else { echo "The file has been changed." ; } ?>
Output of the above code:
The file is ok.
sha1_file ( file , raw )
parameter | describe |
---|---|
file | Required. Specify the documents to be calculated. |
raw |
Optional. Boolean value, specifying hexadecimal or binary output format:
|