Example usage of PHP cryptographic function-sha1 () function encryption

Source: Internet
Author: User
Tags crypt md5 encryption sha1 encryption
PHP Cryptographic function-sha1 () function Encryption

First of all, let's introduce what is SHA1?

SHA is the full name: Secure Hash Algorithm (secure hashing algorithm) is mainly applied to digital signature (digital Signature standard DSS) in the definition of the signature algorithm (digitally Signature Algorithm DSA). For messages that are less than 2^64 bits in length, SHA1 produces a 160-bit message digest. When a message is received, this message digest can be used to verify the integrity of the data. As the data is likely to change during transmission, a different message digest is generated. PHP provides the SHA1 () function using the SHA algorithm!

In the previous two articles, "PHP cryptographic function-crypt () function Encryption" and "PHP cryptographic function-md5 () function Encryption", I believe you have a certain understanding of encryption, in this chapter we will continue to introduce the MD5 () function similar to the SHA1 () function algorithm.

The syntax format for the SHA1 () function is as follows:

String SHA1 (String $str [, bool $raw _output = false])
Parameters Describe
String Necessary. Specifies the string to be computed.
Raw Optional. Specify hexadecimal or binary output format: TRUE-original 20 character binary format false-default. 40-character hexadecimal number

The function returns a 40-bit hexadecimal number, and if the parameter raw_output is true, then a 20-bit binary number is returned, and the default is Raw_output is false.

Here we want to highlight the following:

Sha after 1 is the Arabic numerals (123456) in the 1, not the letter L (L), many people will be regarded as the letter, but it is the Arabic numerals, here we must pay attention to, do not make a mistake!

The following is an example of the SHA1 () function, as follows:

<?phpheader ("content-type:text/html; Charset=utf-8 "); $str =" Chinese web "; echo" string: ". $str." <br> "; echo" TRUE-original 20 character binary format: ". SHA1 ($str, TRUE)." <br> "; echo" FALSE-40 character hexadecimal number: ". SHA1 ($STR)." <br> ";? >

The output is:

The following instance is the result of output SHA1 () and tests it:

<?phpheader ("content-type:text/html; Charset=utf-8 "), $str =" Chinese web ", Echo SHA1 ($STR), if (SHA1 ($str) = =" B1d5e6240057f21930892531def6597f135252ca ") {    echo "<br>i Love Chinese web!";    Exit;}? >

The output is:

The following example is a comparison of MD5 and SHA cryptographic operations, with the following code :

<?phpheader ("content-type:text/html; Charset=utf-8 "); $str =" Chinese web "; echo" MD5 encryption Result: ". MD5 ($STR)." <br> "; echo" <br> "echo" SHA1 encryption Result: ". SHA1 ($STR)." <br> ";? >

The output is:

The main three crypt (), MD5 () and SHA1 () of data encryption functions in PHP are covered, and the following article introduces the PHP Crypto library: Two extensions for MCrypt and Mhash!


"Recommended"

1. Related topics: "PHP cryptographic Functions"

2.PHP cryptographic function-crypt () function Encryption usage instance

3.PHP cryptographic function-md5 () function Encryption instance usage

4.PHP Encryption Technology Video tutorial

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.