PHP built-in encryption function details, php built-in function details

Source: Internet
Author: User
Tags crypt md5 hash rounds sha1 hash alphanumeric characters asymmetric encryption

PHP built-in encryption function details, php built-in function details

Md5 () Encryption Algorithm

Method: One-way encryption

Syntax: md5 (string $ str [, bool $ raw_output = false])

$ Str: original string

$ Raw_output: if the optional raw_output is set to true, the md5 message digest is returned in the original binary format of 16 bytes. returns the hash value in the hexadecimal format of 32 characters.

Md5 secondary encryption: md5 (md5 ($ string, true ))

Crypt () Encryption Algorithm

Method: One-way encryption

Syntax: string crypt (string $ str [, string $ salt]), returns a three-column string based on the standard unix des algorithm or other alternative algorithms available on the system

$ Str: plaintext to be encrypted

$ Salt: The interference string during encryption, which is more secure than encoding.

Note: If the $ salt parameter is not added during encryption, an interference string will be randomly generated. Otherwise, the encrypted secret will not be refreshed.

Algorithm constant:

[CRYPT_SALT_LENGTH]

The default encryption length. Use standard DES encryption, with a length of 2

[CRYPT_STD_DES]

The hash based on the standard DES algorithm uses the two characters in the "./0-9A-Za-z" character as the salt value. Using invalid characters in the salt value causes crypt () to fail.

[CRYPT_EXT_DES]

Extended hash Based on the DES algorithm. The salt value is a 9-character string consisting of a 4-byte Loop followed by an underscore and a 4-byte salt value. They are encoded into printable characters, each of which has 6 characters, with the least valid characters given priority. 0 to 63 are encoded as "./0-9A-Za-z ". Using invalid characters in the salt value causes crypt () to fail.

[CRYPT_MD5]

The MD5 hash uses a string salt value starting with $1 $.

[CRYPT_BLOWFISH]

The Blowfish algorithm uses the following salt value: "$ 2a $", a two-character cost parameter, "$", and a 64-bit string consisting of characters in "./0-9A-Za-z. Using a character out of this range in the salt value causes crypt () to return an empty string. The two cost parameters are the base 2 logarithm of the number of loops, and the value range is 04-31. exceeding this range will cause crypt () to fail.

CRYPT_SHA256

The SHA-256 algorithm hashes the salt value of a 16-character string starting with $5 $. If the salt value string starts with "rounds = <N >$", the numeric value of N is used to specify the number of times hash loops are executed, which is similar to the cost parameter of the Blowfish algorithm. The default number of cycles is 5000, the minimum is 1000, and the maximum is 999,999,999. N out of this range will be converted to the nearest value.

CRYPT_SHA512

The SHA-512 algorithm hashes the salt value of a 16-character string starting with $6 $. If the salt value string starts with "rounds = <N >$", the numeric value of N is used to specify the number of times hash loops are executed, which is similar to the cost parameter of the Blowfish algorithm. The default number of cycles is 5000, the minimum is 1000, and the maximum is 999,999,999. N out of this range will be converted to the nearest value.

Example:

If (CRYPT_MD5) {echo "MD5 encryption:". crypt ('something', '$1 $ somethin $ ');}

Sha1 () Encryption Algorithm

Method: One-way encryption

Syntax: string sha1 (string $ str [, bool $ raw_output = false]) calculates the sha1 hash value of a string.

$ Str: Encrypted string

$ Raw_output: if the optional raw_output parameter is set to TRUE, The sha1 digest is returned in the original format of 20 characters; otherwise, the return value is a hexadecimal number of 40 characters.

URL encoding encryption technology

Encoded URL string: urlencode (string $ str)

Decodes the encoded URL string: urldeocde (string $ str)

Encoding specification: Except -_. all other non-alphanumeric characters will be replaced with a semicolon (%) followed by two hexadecimal numbers, and spaces will be encoded as the plus sign (+)

Encode the URL according to RFC1738: rawurlencode (string $ str): return the slave string and encode the space as % 20

Decodes the encoded string: rawurldecode (string $ str): returns a string of semicolons (;) % followed by two hexadecimal sequence beans. the string is replaced with the original literal character.

The difference between urlencode and rawurlencode is space.

Base64 encryption technology

Encode data using base64: base64_encode (string $ data)

Decodes data encoded with MIME base64: base64_decode (string $ data [, $ strict = false]); $ strict: If the input data exceeds the base64 alphabet, returns false.

Base64 encoded images can also be directly displayed in the label:

Information Encryption technology:

1. Hash encryption:

Hash calculation is performed based on information of different input lengths to get output with a fixed length. This hash calculation process is unidirectional, the Fixed Length output cannot be calculated to output information.

2. symmetric hash Encryption

The encryption key and decryption key are the same or can be calculated from each other.

3 asymmetric hash Encryption

The asymmetric encryption and decryption keys are not the same. One of them is public, called a public key, and the other is only known to the owner.

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.