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 nl2br() Function

PHP nl2br() Function The nl2br() function in PHP inserts HTML line breaks before all newlines in a string. Syntax nl2br ( string $string [, bool $is_xhtml] ) Parameter sting(required)- This parameter specifies the input string. is_xhtml(optional)- This parameter states whether to use XHTML compatible line breaks...

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 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 break

What is Break in PHP? Inside the loop, the break statement is used to control the loop in preprocessor hypertext. PHP utilizes the break keyword to implement the break statement, which...

3 minutes read.

PHP vsprintf() Function

PHP vsprintf() Function The vsprintf() function in PHP returns a formatted string. This function accepts an array of arguments. Syntax vsprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to format the variables in it. The conversion specification of this parameter...

3 minutes read.

PHP print() Function

PHP print() Function The print() function in PHP is used to output one or more string. Syntax print ( string $arg ) Parameter arg(required)- This parameter represents the input data. It can take one or more arguments. Return This function returns an integer value 1. Example...

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.

PHP sscanf() Function

PHP sscanf() Function The sscanf() function in PHP parses input from a string according to a given format. Syntax sscanf ( string $str , string $format [,mixed &$arg1,$agr2,$arg++... ] ) Parameter str(required)- This parameter specifies the string to read. format(required)- This parameter specifies the format to use. The conversion specification...

3 minutes read.

PHP is_numeric() Function

The is_numeric() function in PHP finds whether the specified variable is a number or a numeric string. It returns a Boolean value TRUE if the specified var is a number or a numeric string else it returns FALSE otherwise. Syntax is_numeric ( mixed...

1 minute read.

PHP array_unshift() Function

PHP array_unshift() Function The array_unshift () function in PHP prepends one or more elements to the beginning or front of an array. It returns the new number of elements in the array. Syntax array_unshift ( array &$array [, mixed $... ] ) Parameter array(required)- This array represents...

1 minute read.

PHP if..Else..Elseif

PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement if statement ..else statement ..elseif....else statement switch statement PHP if Statement PHP...

2 minutes read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute read.

PHP strcasecmp() Function

PHP strcasecmp() Function The strcasecmp() function in PHP compares two strings. It is a binary-safe case-insensitive string function. Syntax strcasecmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than( <...

1 minute read.

PHP array_pad() Function

PHP array_pad() Function The array_pad() function in PHP pads the array to the given length and value and returns a copy of the array padded to size and value specified by size and value parameter. The padding takes place as...

1 minute read.

PHP array_uintersect() Function

PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...

2 minutes read.

PHP natcasesort() Function

PHP natcasesort() Function The natcasesort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natcasesort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean...

1 minute read.

PHP Tutorial

PHP Tutorial PHP Introduction PHP is a server-side scripting language which is used for web development. It stands for Hypertext Preprocessor. It is faster than another programming language. It supports multiple databases like MySQL, SQL, PostgreSQL and...

3 minutes read.

PHP array_uintersect_assoc() Function

PHP array_uintersect_assoc() Function The array_uintersect_uassoc() function in PHP calculates the intersection of arrays by comparing the data and indexes by separate callback functions. It returns an array containing all the values of the first input...

2 minutes read.

PHP Date and Time

DATE: The PHP date() function formats a date or time. The timestamp is used to format a more readable date and time into a PHP date () function. Syntax: date(format,timestamp) Simple Date: Characters that...

1 minute read.

PHP array_fill() function

PHP array_fill() function The array_fill() function in PHP fills an user-defined array with num entries of the value that of specified values parameter, where the keys start at the start_index parameter. Syntax array_fill ( int $start_index , int $num , mixed $value ) Parameter start_index(required)- This parameter represents the first index of the...

1 minute read.