PHP computes the SHA-1 hash function of a string SHA1 ()

Source: Internet
Author: User

Instance

Computes the SHA-1 hash of the string "Hello":

<?php$str = "Hello"; Echo SHA1 ($STR);? >

Definition and usage

The SHA1 () function computes the SHA-1 hash of the string.

The SHA1 () 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.

Tip: If you want to calculate the SHA-1 hash of a file, use the Sha1_file () function.

Grammar

SHA1 (String,raw)

Parameter description

string is required. Specifies the string to be computed.

Raw is optional. Specify hexadecimal or binary output formats:

TRUE-Original 20 character binary format

FALSE-Default. 40-character hexadecimal number

Technical details

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

PHP version: 4.3.0+

Update log: In PHP 5.0, the raw parameter becomes optional.

More examples

Example 1

Results of output SHA1 ():

<?php $str = "Hello"; echo "The string:". $str. " <br> "; echo "true-raw character binary format:". SHA1 ($str, TRUE). " <br> "; echo "FALSE-40 character hex Number:". SHA1 ($STR). " <br> ";?>

Example 2

Output the result of SHA1 () and test it:

<?php$str = "Hello", Echo SHA1 ($STR), if (SHA1 ($str) = = "F7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0") {echo ' <br> Hello world! "; Exit;}? >
Related Article

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.