PHP crypt() Function

PHP crypt() Function

The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms.

Some constants of crypt() function are as follows:

  1. [CRYPT_STD_DES]
  2. [CRYPT_EXT_DES]
  3. [CRYPT_MD5]
  4. [CRYPT_BLOWFISH]
  5. [CRYPT_SHA_256]
  6. [CRYPT_SHA_512] etc.

Syntax

crypt ( string $str [, string $salt ] ) 

Parameter

str(required)- This parameter represents the string to be hashed

salt(optional)- This parameter represents the salt string to base the hashing on.

Return

This function returns the hashed string or a string that is shorter than 13 characters.

Example 1

   

Output

Standard DES: stqAdD7zlbByI

Example 2

<?php
 // 12 character salt starting with $1$ 
 if (CRYPT_MD5 == 1)
 {
 echo "MD5: ".crypt('tutorialandexample','$1$tutorialandexample$')."\n"; 
 } 
 else
 {
 echo "MD5 not supported.\n";
 }
 ?>     

Output

MD5: $1$tutorial$h33eIOE.o5xIc2MCML1M2/

Example 3

<?php
 // 16 character salt starting with $5$. The default number of rounds is 5000.  
 if (CRYPT_SHA256 == 1) {  
 echo "SHA-256: ".crypt('tutorialandexample','$565$rounds=98$tutorialandexamplestringforsalt$')."\n"; }  
 else{  
 echo "SHA-256 not supported.\n";   
 }  
 ?> 

Output

SHA-256: $5c8GnoWJydmI

Example 4

<?php
 // 16 character salt starting with $6$. The default number of rounds is 5000.
 if (CRYPT_SHA512 == 1) 
 {
 echo "SHA-512: ".crypt('something','$6$rounds=5000$anexamplestringforsalt$'); 
 }
 else 
 {
 echo "SHA-512 not supported.";
 }
 ?> 

Output

SHA-512: $6$rounds=5000$anexamplestringf$Oo0skOAdUFXkQxJpwzO05wgRHG0dhuaPBaOU/oNbGpCEKlf/7oVM5wn6AN0w2vwUgA0O24oLzGQpp1XKI6LLQ0

Example 5

<?php
 // 16 character salt starting with $5$. The default number of rounds is 5000.
 if (CRYPT_SHA256 == 1) 
 {
 echo "SHA-256: ".crypt('something','$5$rounds=5000$anexamplestringforsalt$')."\n"; }
 else
 {
 echo "SHA-256 not supported.\n";
 }  
 ?> 

Output

SHA-256: $5$rounds=5000$anexamplestringf$KIrctqsxo2wrPg5Ag/hs4jTi4PmoNKQUGWFXlVy9vu9

Related Topics

PHP convert_uudecode() Function

PHP convert_uudecode() Function The convert_uudecode() function in PHP decodes a uuencoded string. Syntax convert_uudecode ( string $data ) Parameter data- This parameter represents the uuencoded data. Return This function returns the decoded data as a string or FALSE on failure. Example 1 <?php // initializing the string $str...

1 minute read.

PHP is_double() Function

The PHP is_double() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

PHP strval() function

PHP strval() function The strval() function in PHP is used to get the string value for the specified variable. This function cannot be used on arrays or objects, if applied then would return only the...

1 minute read.

PHP strstr() Function

PHP strstr() Function The strstr() function in PHP finds the first occurrence of a string. It returns part of the parameter haystack string starting from and including the first occurrence of needle to the end of the haystack parameter. This function is case-sensitive.  Syntax strstr ( string $haystack , mixed $needle [, bool $before_needle] )  Parameter  haystack(required)-...

2 minutes read.

PHP htmlentities() Function

PHP htmlentities() Function The htmlentities() function in PHP converts all characters to HTML entities. Syntax htmlentities(string,flags,character-set,double_encode);   Parameter string(required)- This parameter specifies the string to convert. flags(optional)- This parameter specifies how to handle quotes, invalid encoding and the used document type. The...

1 minute read.

PHP strripos() Function

PHP strripos() Function The strripos() function in PHP finds the position of the last occurrence of a substring in the specified string. It is a case-insensitive and binary-safe function. Syntax strripos ( string $haystack , mixed $needle [, int $offset ] ) Parameter haystack(required)- This parameter signifies the...

1 minute read.

PHP arsort() function

PHP arsort() function The arsort() function in PHP sorts an array in the reverse order and maintains the index association Syntax arsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to modify the behavior...

2 minutes read.

PHP compact() function

PHP compact() function The compact() function in PHP is used to create an array containing variables and their values. It returns the output array whose keys are the variable names, and their corresponding values are...

1 minute read.

PHP strtoupper() Function

The strtoupper() function in PHP is used to convert a string to uppercase. This function is binary-safe. The related functions are as follows: strtolower()lcfirst()ucfirst()ucwords() Syntax strtoupper ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

1 minute read.

PHP reset() Function

PHP reset() Function The reset() function in PHP is used to set the internal pointer of an array to its first element and returns the value of the first array element. Syntax reset ( array &$array )  Parameter array(required)- This parameter represents...

1 minute read.

PHP is_bool() Function

The is_bool() Function in PHP is used to  find whether a variable is a Boolean or not. It returns a Boolean value true if the parameter var is a Boolean else it returns...

1 minute read.

PHP is_real() Function

The PHP is_real () function in PHP is used to find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter...

1 minute read.

PHP array_diff() Function

The array_diff () function in PHP compares array1 against other specified arrays and returns the values in array1 that are not present in any of the other arrays. Syntax array_diff(array$array1,array$array2[,array$...] ) Parameter array1(required)- This parameter signifies the input array to...

1 minute read.

PHP array_unique() Function

PHP array_unique() Function The array_unique() function in PHP removes the duplicate values from the specified array. Syntax array_unique (array $array [, int $sort_flags = SORT_STRING]) Parameter array(required)- This array represents the first input array. sort_flags (optional)- This parameter is used to modify the sorting behavior. The...

1 minute read.

PHP empty() Function

PHP empty() Function The empty() function in PHP determines whether the specified variable is empty or not. The specified variable is considered empty if it does not exist or if its value is equal to FALSE. This function...

1 minute read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP strcoll() Function

PHP strcoll() Function The strcoll() function in PHP compares two strings. This function is case-sensitive but not binary-safe. It uses the current locale for doing the comparisons. Syntax strcoll ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter...

1 minute read.

PHP next() Function

PHP next() Function The next() function in PHP advances or moves the internal array pointer one place forward and returns the next array value. Syntax next ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns the next array value...

1 minute read.

PHP array_map() Function

PHP array_map() Function The array_map() function  in PHP is used to return an array containing the results of applying the user-defined callback function over the elements of one or more arrays. Syntax array_map ( callable $callback , array $array1 [, array $... ] )  Parameter callable(required)- This parameter...

1 minute read.

PHP strcmp() Function

PHP strcmp() Function The strcmp() function in PHP compares two strings. It is a binary-safe case-sensitive string function. This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each...

1 minute read.