PHP computes the MD5 hash of a string

Source: Internet
Author: User
Tags md5 digest md5 encryption md5 hash

Instance

Computes the MD5 hash of the string "Hello":

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

Definition and usage

The MD5 () function computes the MD5 hash of the string.

The MD5 () function uses RSA data security, including MD5 Digest to algorithm.

Explanation from RFC 1321-MD5 Digest to algorithm: MD5 Digest to the algorithm will be arbitrary length of information as input value, and convert it into a 128-bit length of "fingerprint Information" or "Digest to" value to represent this input value, and with the converted value as the result. The MD5 algorithm is designed primarily for digitally signed applications, where large files are compressed in a secure manner prior to encryption (where encryption is done by setting a private key under the public key of a cryptographic system, such as RSA).

To calculate the MD5 hash of a file, use the Md5_file () function.

Grammar

MD5 (STRING,RAW)

Parameter description

string is required. Specifies the string to be computed.

Raw is optional. Specify hexadecimal or binary output formats:

TRUE-Original 16 character binary format

FALSE-Default. 32-character hexadecimal number

Technical details

Return value: Returns the computed MD5 hash if successful, or FALSE if it fails.

PHP version: 4+

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

More examples

Example 1

Results of output MD5 ():

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

Example 2

Output the result of MD5 () and test it:

<?php$str = "Hello"; Echo MD5 ($STR); if (MD5 ($str) = = "8b1a9953c4611296a827abf8c47804d7") {echo] <br>hello world !"; Exit;}? >

In general, a member to provide registration of the site must collect the user's password, how to save the user password is a problem. We certainly cannot store passwords in plaintext in the database, because in this way, the average administrator can see the user's password, which is obviously a very dangerous thing for the user.

How to solve this problem, we can take such a strategy.

First introduce the use of the MD5 function in PHP:

<?php  $pswd 1=md5 ("cenusdesign");  echo $pswd 1;     The result of operation is: Fc60ec37d1c08d5b0fb67a8cd934d5ba  $pswd 2=md5 ("cenusdesign");  echo $pswd 2;     Operation Result: 067577d9fc109c80538c81d6f02bd293  ?>


Obviously, after MD5 encryption, the original "Cenusdesign" is transformed into a set of 32-bit strings, and even if the case of a letter changes, this set of strings will change dramatically.

Cenus Design recommends that when the user registers, the password is converted first through MD5, and then the encrypted database is converted. When the user logs on, the password is MD5 converted, and then compared with the set of MD5 encrypted strings in the database. This makes it possible to do a password-specific operation without knowing the user's exact password.

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.