Summary of encryption functions in PHP

Source: Internet
Author: User
Tags crc32 crypt md5 hash
In general, the functions that can implement encryption include: (1) MD5 () (2) sha1 () (3) CRC32 () (4) crypt () (5) uniqid ()

First, explain the MD5 () method:

Definition and usage

MD5 () function calculates the MD5 hash of a string.

The MD5 () function uses RSA Data Security, including the MD5 packet excerpt algorithm.

If the calculation succeeds, the calculated MD5 hash is returned. If the calculation fails, false is returned.

Syntax
md5(string,raw)
Parameters Description
String

Required. Specifies the string to be calculated.

Raw

Raw

Optional. The hexadecimal or binary output format is required:

  • True-original 16-character binary format
  • False-default. 32-character hexadecimal number

Note: this parameter is added in PHP 5.0.

Example
<?php$str = "Hello";echo md5($str);?>

Output:

8b1a9953c4611296a827abf8c47804d7

Second, the sha1 () function:

Definition and usage

Sha1 () function calculates the SHA-1 hash of the string.

The sha1 () function uses the US Secure Hash Algorithm 1.

If the operation succeeds, the calculated SHA-1 hash is returned. If the operation fails, false is returned.

Syntax
sha1(string,raw)
Parameters Description
String Required. Specifies the string to be calculated.
Charlist

Optional. The hexadecimal or binary output format is required:

  • True-original 20-character binary format
  • False-default. 40-character hexadecimal number

Note: this parameter is added in PHP 5.0.

         

The second is the CRC32 () function:

Definition and usage

The CRC32 () function computes the CRC32 polynomial of a string.

This function can be used to verify data integrity.

Syntax

CRC32 (string)

String is required. Specifies the string to be calculated.

Description

Generate the 32-bit Cyclic Redundancy checksum polynomial of the string parameter. This is usually used to check whether the transmitted data is complete.

Tips and comments

Tip: Because PHP integers are signed, many CRC32 verification codes return negative integers, so you need to use sprintf () or printf () to obtain the string that represents the CRC32 verification code.

Example 1

In this example, the result of CRC32 () is output without the "% u" format character (note that the result is the same ):

<?php$str = crc32("Hello world!");echo 'Without %u: '.$str."<br />";echo 'With %u: ';printf("%u",$str);?>

Output:

Without %u: 461707669With %u: 461707669
Example 2

In this example, we will output the CRC32 () results without the "% u" format character (note that the results are different ):

<?php$str = crc32("Hello world.");echo 'Without %u: '.$str."<br />";echo 'With %u: ';printf("%u",$str);?> 

Output:

Without %u: -1959132156With %u: 2335835140

Then the uniqid () function:

Definition and usage

The uniqid () function generates a unique ID based on the current time in microseconds.

Syntax

Uniqid (prefix, more_entropy)

Prefix is optional. It is the prefix specified by ID. This parameter is useful if two scripts generate IDs in the same subtle way.

More_entropy is optional. Specify more entropy at the end of the returned value.

Description

IfPrefixIf the parameter is null, the returned string contains 13 characters in length. IfMore_entropyIf the parameter is set to true, it is a string of 23 characters.

IfMore_entropyIf the parameter is set to true, an extra entropy is added at the end of the returned value (the program is generated by using the combination of linear and remainder values), which improves the uniqueness of the result.

Return Value

Returns a unique identifier in the form of a string.

Tips and comments

Note: because the system time is used, the IDS generated through this function are not optimal. To generate an absolutely unique ID, use the MD5 () function (please refer to the string function reference ).

Example
<?phpecho uniqid();?>

The output is similar:

4415297e3af8c

Then there is the crypt () function:

Definition and usage

The crypt () function returns a string encrypted with DES, blowfish, or MD5.

In different operating systems, the behavior of this function is different. Some operating systems support more than one algorithm type. During installation, PHP checks what algorithms are available and used.

Syntax

Crypt (STR, salt)

STR is required. Specifies the string to be encoded.

Salt

Optional. It is used to increase the number of characters to be encoded to make the encoding more secure.

If the salt parameter is not provided, a random value is generated each time the function is called.

Tips and comments

Tip: the decryption function does not exist. The crypt () function uses a unidirectional algorithm.

Example

In this example, We will test different algorithms:

<?phpif (CRYPT_STD_DES == 1){echo "Standard DES: ".crypt("hello world")."\n<br />";}else{echo "Standard DES not supported.\n<br />";}if (CRYPT_EXT_DES == 1){echo "Extended DES: ".crypt("hello world")."\n<br />";}else{echo "Extended DES not supported.\n<br />";}if (CRYPT_MD5 == 1){echo "MD5: ".crypt("hello world")."\n<br />";}else{echo "MD5 not supported.\n<br />";}if (CRYPT_BLOWFISH == 1){echo "Blowfish: ".crypt("hello world");}else{echo "Blowfish DES not supported.";}?>

The output is similar (depending on the operating system ):

Standard DES: $1$r35.Y52.$iyiFuvM.zFGsscpU0aZ4e. Extended DES not supported. MD5: $1$BN1.0I2.$8oBI/4mufxK6Tq89M12mk/ Blowfish DES not supported.
Description

The exact algorithm depends on the salt parameter format and length.

The following are some constants used with the crypt () function. During installation, PHP sets these constants:

  • [Crypt_salt_length]
  • [Crypt_std_des]
  • [Crypt_ext_des]
  • [Crypt_md5]
  • [Crypt_blowfish]

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.