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

PHP crc32() Function The crc32() function in PHP  Calculates the crc32 polynomial of a string. Syntax crc32( string $str ) Parameter str(required)- This parameter represents The string to be calculated Return This function returns the crc32 checksum of string as an integer. Example 1 ...

1 minute read.

PHP htmlspecialchars() Function

PHP htmlspecialchars() Function The htmlspecialchars() function in PHP converts some special predefined characters to HTML entities. The special predefined characters are as follows: & (ampersand) OR &amp;" (double quote) OR &quot;' (single quote) OR &#039;<...

2 minutes read.

PHP strtr() Function

PHP strtr() Function The strtr() function in PHP translates or replaces certain characters in a string. Syntax strtr ( string $str , string $from , string $to ) or strtr ( string $str , array $replace_pairs ) Parameter str(required)- This parameter specifies the string to translate from(required)- This parameter represents the string being translated to the parameter ‘to’. to(required)- It specifies what...

1 minute read.

PHP foreach Loop

PHP foreach Loop with Example PHP foreach loop is used to loop through the associative arrays. Syntax: foreach($array as $key => $value) { //Statement } $array = associative array. $key = array key. &value =...

2 minutes read.

PHP quotemeta() Function

PHP quotemeta() Function The quotemeta() function in PHP quotes the meta characters and returns a string with a backslash character (\) before some predefine characters. The predefined characters are as follows: period ...

1 minute read.

PHP array_diff_assoc() Function

PHP array_diff_assoc() Function The array_diff_assoc() Function in PHP compares the difference of arrays and returns an array containing all the values from array1 that are not present in any of the other arrays. Syntax array_diff_assoc ( array $array1 , array $array2 [, array $... ] ) Parameter array1(required)- This parameter signifies...

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

PHP str_getcsv() Function The str_getcsv() function in PHP parses a CSV string into an array and returns an array containing the fields read. Syntax str_getcsv ( string $input [, string $delimiter [, string $enclosure [, string$escape ]]] ) Parameter input(required)- This parameter represents the string to parse. delimiter(optional)- This parameter signifies...

1 minute read.

PHP stristr() Function

PHP stristr() Function The stristr() function in PHP finds the first occurrence of a string and returns the matched substring. It is a case-insensitive function. Syntax stristr ( string $haystack , mixed $needle [, bool $before_needle ] ) Parameter haystack(required)- This parameter represents the input string. needle(required)- This parameter...

2 minutes read.

PHP str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

2 minutes read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

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

PHP strtok() Function The strtok() function in PHP splits a string into smaller strings or tokenize the specified string.  Syntax strtok ( string $str , string $token ) Parameter str(required)- This parameter represents the input string to split into smaller strings or tokens. token(required)- It signifies...

1 minute read.

PHP is_object() Function

The is_ object() function in PHP finds whether the specified variable is an object or not. It returns a Boolean value TRUE if the specified var is an object else it returns FALSE. Syntax is_object(mixed$var) var(required)- This parameter represents the...

1 minute read.

PHP is_integer() Function

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

1 minute read.

PHP MySQL Database Tutorial

MySQL is the most popular database system that is used within PHP. Let's first understand about MySQL. What is MySQL? MySQL is open-source Relational Database Management System (RDMS). The name of MySQL is...

13 minutes read.

PHP array_count_values() Function

PHP array_count_values() Function The array_count_values() function in PHP counts all the values present in the specified array and returns an array using the values of the array as keys and their frequency in the array as values. Syntax array_count_values ( array $array ) Parameter array(required)- This parameter represents the...

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.

WordPress Theme

Writing our WordPress Theme is relatively simple. Requirements: Before we begin, we need to know about the following set of requirements. We need to have full knowledge of WordPress setup, which is either...

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.