PHP built-in cryptographic functions

Source: Internet
Author: User
Tags crypt md5 digest md5 encryption printable characters rounds urlencode alphanumeric characters asymmetric encryption
In the development process of the website, we often need to encrypt some data (such as user password), this article mainly introduces several common cryptographic functions of PHP.

MD5 () encryption algorithm

Way: one-way encryption

Syntax: MD5 (string $str [, bool $raw _output = false])

$STR: Raw String

$raw _output: If the optional raw_output is set to true, then the MD5 digest will be returned in the original binary format of 16 bytes in length. Returns the hash value returned as a 32-character hexadecimal number

MD5 two times encryption: MD5 (MD5 ($string, True))

Crypt () encryption algorithm

Way: one-way encryption

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

$STR: PlainText required for encryption

$salt: Interference string when encrypting, it is more safe to encode

Note: If the $salt parameter is not added to the encryption, a disturbance string is randomly generated, otherwise the encryption secret will be flushed unchanged.

Algorithm constants:

[Crypt_salt_length]

The default encryption length. Uses standard DES encryption with a length of 2

[Crypt_std_des]

Hashes based on the standard DES algorithm use the two characters in the "./0-9a-za-z" character as the salt value. Using illegal characters in salt values will cause crypt () to fail.

[Crypt_ext_des]

Extended DES algorithm-based hashing. A string with a salt value of 9 characters, consisting of 1 underscores followed by 4-byte cycles and 4-byte salt values. They are encoded into printable characters, each character 6 bits, and the effective bit is the least preferred. 0 to 63 are encoded as "./0-9a-za-z". Using illegal characters in salt values will cause crypt () to fail.

[CRYPT_MD5]

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

[Crypt_blowfish]

The Blowfish algorithm uses the following salt values: "$2a$", a two-bit cost parameter, "$", and a string of 64 bits that are combined by the characters in "./0-9a-za-z". Using a character outside of this range in the Salt value causes crypt () to return an empty string. The two-bit cost parameter is the logarithm of the number of cycles with a base of 2, and its range is 04-31, and exceeding this range will cause crypt () to fail.

crypt_sha256

The SHA-256 algorithm uses a 16-character string salt value that begins with $5$ to hash. If the salt string begins with "rounds=<n>$", the numeric value of N is used to specify the number of executions of the hash loop, much like 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 that exceeds this range will be converted to the nearest value.

crypt_sha512

The SHA-512 algorithm uses a 16-character string salt value that begins with $6$ to hash. If the salt string begins with "rounds=<n>$", the numeric value of N is used to specify the number of executions of the hash loop, much like 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 that exceeds this range will be converted to the nearest value.

Cases:

if (CRYPT_MD5) {  echo "MD5 encryption:". CRYPT (' Something ', ' $1$somethin$ ');}

SHA1 () encryption algorithm

Way: one-way encryption

Syntax: String SHA1 (String $str [, bool $raw _output=false]) computes 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 with a 20-character length, otherwise the return value is a hexadecimal number of 40 string length.

URL Encoding encryption technology

Encoded URL string: UrlEncode (String $str)

Decode encoded URL string: URLDEOCDE (String $str)

Encoding specification: All non-alphanumeric characters except-_ in this string are replaced with a percent sign (%) followed by a two-digit hexadecimal number, and a space is encoded as a plus (+)

Encode the URL according to RFC1738: Rawurlencode (String $str): Returns the space encoded as%20 from the string

Decodes the encoded string: Rawurldecode (String $str): Returns a string in which the percent semicolon followed by a two-bit hexadecimal sequence bean is replaced with a literal character

The difference between UrlEncode and Rawurlencode is only the space.

BASE64 Coded encryption technology

Encode data using Base64: Base64_encode (String $data)

Decoding data encoded with MIME base64: Base64_decode (String $data [, $strict =false]); $strict: Returns False if the input data exceeds the Base64 's alphabet.

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

Information encryption Technology:

1 Single Hash Encryption:

Refers to the information of different input length of the hash calculation, to obtain a fixed length of output, the hash calculation process is one-way, can not calculate the fixed length of the output of the job output information.

2 Symmetric Hash encryption

Encryption and decryption keys are the same key or can be extrapolated to each other.

3 Asymmetric Hash Encryption

Asymmetric encryption and decryption keys are not the same key, one of which is publicly disclosed, known as the public key, and the other is known only by the owner.

The above is the whole content of this article, I hope that everyone's study has helped.


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.