Hashing PHP

Hashing is the process of creating an index for a value using the value itself. The index is called a hash. Sometimes hashes are unique to values, but not always. Hashes can be used to make fast lookups, a method that PHP uses for keeping track of variables. Other times hashes are used like encryption. If the hashes of two strings match, you can assume the two strings match, as long as hash values are unique. In this way you can check passwords without ever decrypting the original password.

Some of the functions in this section are built into PHP. The others are part of Sascha Shumann's Mhash library. This library presents a universal interface to many hashing algorithms.

string md5(string text)
The md5 function produces a hash as described by RFC 1321. The function takes a string of any length and returns a 32-character identifier. It is theorized that the algorithm for the md5 function will produce unique identifiers for all strings.

md5

string md5(string text)

string md5(string text)

string metaphone(string word)
Use metaphone to produce a string that describes how a word sounds when spoken. This function is similar to soundex; however, it knows about how groups of letters are pronounced in English. Therefore it is more accurate. Compare this function to soundex and similar_text. The metaphone algorithm, invented by Lawrence Philips, was first described in Computer Language magazine.

string mhash(integer hash, string data)
Use mhash to get a hash for a string. Hashing algorithms available at the time of writing.Refer to the Mhash documentation for more information about each algorithm.

<?
print(mhash(MHASH_GOST, "Who is John Galt?"));
?>Mhash-Algorithms

integer mhash_count()
The mhash_count function returns the highest-numbered hash identifier. All hash algorithms are numbered from zero, so you can use this function and mhash_get_hash_name to get a complete list.

<?
print("<TABLE BORDER="1">n");
print("<TR>n");
print("<TH>Algorithm</TH>n");
print("<TH>Block Size</TH>n");
print("</TR>n");
for($i=0; $i <= mhash_count(); $i++)
{
print("<TR>n");
print("<TD>MHASH_" . mhash_get_hash_name($i) .
"</TD>n");
print("<TD>" . mhash_get_block_size($i) .
"</TD>n");
print("</TR>n");
}
print("</TABLE>n");
?>

integer mhash_get_block_size(integer hash)
The mhash_get_block_size function returns the block size used for a hash algorithm.

string mhash_get_hash_name(integer hash)
The mhash_get_hash_name function returns the name for a particular hash identifier.

int similar_text(string left, string right, reference percentage)
The similar_text function compares two strings and returns the number of characters they have in common. If present, the variable specified for the percentage argument will receive the percentage similarity. Compare this function to metaphone and soundex.

The algorithm used for similar_text is taken from a book by Ian Oliver called Programming Classics: Implementing the World's Best Algorithms. It's published by Prentice Hall, and you can find out more about it on the Prentice Hall PTR

similar_text

Similar text

string soundex(string text)
The soundex function returns an identifier based on how a word sounds when spoken. Similar-sounding words will have similar or identical soundex codes. The soundex code is four characters and starts with a letter. Compare this function to the similar_text and the metaphone functions. The soundex algorithm was described by Donald Knuth in Volume 3 of The Art of Computer Programming.

<?
print(soundex("lion"));
print("<BR>");
print(soundex("lying"));
?>

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics