PHP String Hashing Functions
Here are some PHP functions which used to create a hash of the string. Below are the list of those functions.
md5()md5_file()sha1()sha1_file()crypt()
Let’s get into each one.
md5()
This function create a md5 hash of the string.
This function takes two arguments.
str (required)
This is the string for which we want to get the MD5 hash.
raw_output (optional)
If this parameter is set to TRUE then this function will return raw binary format with the length of 16.
Return Value:
Returns the hash as a 32-character hexadecimal number.
Note: MD5 Stands for Message Digest 5.
Usage:
<?php echo md5('123456'); ?>
md5_file()
This function will create a MD5 hash of the given file. It will take same arguments as md5(), but firts argument should be file name.
This function return a file hash on success, FALSE otherwise.
Usage:
<?php $file = 'demo.txt'; echo md5_file($file); ?>
sha1()
This function calculate the sha1 hash of the given string. This will calculate hash using US Secure Hash Algorithm 1.
This function will take two parameter.
str (required)
This is the string for which we need a sha1 hash.
row_output (optional)
If this parameter set to TRUE then this function will return raw binary format with length of 20, otherwise it will return 40 chracter hexadecimal number.
Usage:
<?php echo sha1('123456'); ?>
sha1_file()
This function will generate a sha1 hash of the given file in first argument. Second argument will behave same as in sha1().
Usage:
<?php $file = 'demo.txt'; echo sha1_file($file); ?>
crypt()
This is the method for one way hashing of the string. This functions will take two arguments.
This function will return standard Unix DES-based algorithm or alternative algorithms that may be available on the system
str (required)
This is the string for which we want the hash.
salt (optional)
This arguments can be from below methods.
- CRYPT_STD_DES
- CRYPT_EXT_DES
- CRYPT_MD5
- CRYPT_BLOWFISH
- CRYPT_SHA256
- CRYPT_SHA512
Note: There is no decrypt function, because crypt is the one way hashing algorithm.
More details about crypt() can be found here.
More From Avinash
Avinash Recommends
- Data encryption and hashing in PHP (refulzphp)
- PHP Coding Tips (refulzphp)
