PHP has a number of hashing algorithms available for creating hash values of files and strings. A hash is a non-reversible fixed length string which has a number of applications, such as storing a password in a database in a way that can not be read (in this example when someone logs in the hash is applied to their submitted plain text password and compared with the hashed value in the database). This post looks at how to show the available hashes in PHP and how to use the hash() function to generate a hash value.
To get the list of all the available hash algorithms in PHP use the hash_algos() function, for example:
This will give you a zero based index array containing all the hash algorithms available. The output from the command above is shown below:
As you can see there are a lot. The most commonly used ones are MD5 and SHA1 and these have their own functions as well as the generic hash and hash_file functions.
To calculate the hash on a string using md5, for example, you can do this:
Both of the above examples will output the same hash which is as follows:
Picking one of the other hash algorithms at random, we could use e.g. haval256,3 like so:
You can also create a hash of a file using the hash_file, md5_file and sha1_file functions, e.g.:
0 comments:
Post a Comment