PHP computed file SHA-1 hash function sha1_file ()

Source: Internet
Author: User

Instance

Computes the SHA-1 hash of the text file "Test.txt":

<?php$filename = "Test.txt"; $sha 1file = Sha1_file ($filename); Echo $sha 1file;? >

The above code will output:

aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d

Definition and usage

The Sha1_file () function computes the SHA-1 hash of the file.

The Sha1_file () function uses the United States Secure Hash algorithm 1.

Explanation from RFC 3174-the US Secure Hash algorithm 1:sha-1 produces a 160-bit output called the digest. The digest of the paper can be entered into a signature algorithm that can generate or verify the message signature. The signature of the digest, rather than the signature of the message, can improve the efficiency of the process, since the size of the digest is usually much smaller than the message. The authenticator of a digital signature must use the same hashing algorithm as the creator of the digital signature.

Returns the computed SHA-1 hash if successful, or FALSE if it fails.

Grammar

Sha1_file (File,raw)
parameters description
file required. Specify the documents to be calculated.
raw optional. A Boolean value that specifies the hexadecimal or binary output format:
  • TRUE-Original 20 character binary format

  • FALSE-Default. 40-character hexadecimal number

technical details

return value:
php version: 4.3.0+
update log:
from PHP 5.1, you can use Sha1_file () by encapsulation. For example: Sha1_file ("http://w3cschool.cc/.")

More examples

Example 1

Store the SHA-1 hash of "test.txt" in the file:

<?php$sha1file = Sha1_file ("test.txt"); File_put_contents ("Sha1file.txt", $sha 1file);? >

Detects if "Test.txt" has been changed (that is, if the SHA-1 hash has been changed):

<?php$sha1file = file_get_contents ("Sha1file.txt"), if (Sha1_file ("test.txt") = = $sha 1file) {echo "The file is OK."} Else{echo "The file has been changed.";}? >

The above code will output:

The file is OK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.