Introduction: PHP Hash Generator

About: Just your average idiot who likes electricity, programming, and doing things the hardest way possible.
Hashing allows you to turn readable text into a hexadecimal code. Hashing algorithms work one way only; cannot be reversed. The most commonly hashed things are passwords. The reason you would want to hash something is because, once hashed, is not human readable. The only way to figure out the original text that was hashed is to take some text and hash that, comparing the output to the known hash (other words, brute force).

But the reason I want to do this instructable is to let you see these hashes. Basically, we are going to create a page that allows you to enter text and choose a hashing method. The page will then show you the original text and the hash it generates. I have now uploaded text files with the code we will use. Lets begin.

Step 1: Preface - Requirements

All you need to be able to do this is a PHP enabled web server with some hash engines. Or you can just use a program called "EasyPHP". The former can cost money, depending on if you have an old computer lying around and blank CDs too, or if you are renting space on a server somewhere. The former is free and easy to use. I am using the former.

You also need a web browser to view the files and a text editor to make the files. I am on a Windows machine, so if you need to, adjust this to your OS. Now we go on to step two.

Step 2: Determine Your Available Hash Engines.

Go to your web directory, "www". If you get your own server or are renting space, it won't be too hard to find. In Ubuntu server, it will be /var/www). In EasyPHP, it will be "C:\Program Files\EasyPHP-12.1\www". Or in EasyPHP, right-click the "e" icon in the task tray and click "explore". Start a new file there called, "phpinfo.php" and insert the following code: <?php phpinfo() ?>

Save it and navigate to that page in your browser. You will see a ton of stuff. Look for the word, in bold, "hash". It will show a list separated list of hash functions you can use. You will need this list later. Either open a new tab for the rest of this instructable, or copy the list to a new text document. I would do the latter because of on of our next steps.

Step 3: Building Index.php

For simplicity's sake, we will make the entire hash process stay on one page. We will call this page, "index.php". If you want to create a "style.css", do it, but I am going to skip that. Our index.php page will contain the following:
1. A form used to insert text, allow hashing of empty text, selection of hash method, and a submit button
2. Some text
3. Our output

While this may not seem like much, it will take up a ton of space (a staggering 2.87 KB). Because the code is so long, I will spread it across multiple steps. But lets go ahead and create some text. Insert the following into the blank page, edit as desired:



Hash generator


This page will take input and create a hash of your choosing.

 

 

Step 4: Making the Form: Text and Checkbox

Our form will appear small on the web browser, but large in the file. So lets get the small stuff out of the way first. We will now create a text area and a check box. Insert the following code and tweak as desired, but remember any changes you make. If you change a variable and try to use the original later, the code will not work.

<form action="index.php" method="post">
Text/string: <input type="text" name="message" /> Check the following box to allow the hashing of empty text (you do not type anything): <input type="checkbox" name="blank" value="blank" /><br />

Step 5: Form: Drop Down List

Holy Credit Bureau of Indochina, this part is large. * long sigh* Lets get it over with. The dropdown list begins with '<select name="method">'. To actually add options to the list, use '<option value="hash function">what the user reads</option>'

An example would look like this:
<option value="md2">MD2</option>
<option value="md5">The MD. of 5</option>

Just remember to end your list using '</select>'. The options you can use is in the space separated list you saw earlier. I am just going to copy my list. Edit it as needed:

Hash Method: <select name="method">
<option value="md2">MD2</option>
<option value="md4">MD4</option>
<option value="md5">MD5</option>
<option value="sha1">SHA1</option>
<option value="sha256">SHA256</option>
<option value="sha384">SHA384</option>
<option value="sha224">SHA224</option>
<option value="sha512">SHA512</option>
<option value="snefru256">SNEFRU256</option>
<option value="fnv132">FNV132</option>
<option value="fnv164">FNV164</option>
<option value="joaat">JOAAT</option>
<option value="sha512">SHA512</option>
<option value="ripemd128">RIPEMD128</option>
<option value="ripemd160">RIPEMD160</option>
<option value="ripemd256">RIPEMD256</option>
<option value="ripemd320">RIPEMD320</option>
<option value="whirlpool">WHIRLPOOL</option>
<option value="tiger128,3">TIGER128,3</option>
<option value="tiger160,3">TIGER160,3</option>
<option value="tiger192,3">TIGER192,3</option>
<option value="tiger128,4">TIGER128,4</option>
<option value="tiger160,4">TIGER160,4</option>
<option value="tiger192,4">TIGER192,4</option>
<option value="snefru">SNEFRU</option>
<option value="gost">GOST</option>
<option value="adler32">ADLER32</option>
<option value="crc32">CRC32</option>
<option value="crc32b">CRC32B</option>
<option value="haval128,3">HAVAL128,3</option>
<option value="haval160,3">HAVAL160,3</option>
<option value="haval192,3">HAVAL192,3</option>
<option value="haval224,3">HAVAL224,3</option>
<option value="haval256,3">HAVAL256,3</option>
<option value="haval128,4">HAVAL128,4</option>
<option value="haval160,4">HAVAL160,4</option>
<option value="haval192,4">HAVAL192,4</option>
<option value="haval224,4">HAVAL224,4</option>
<option value="haval256,4">HAVAL256,4</option>
<option value="haval128,5">HAVAL128,5</option>
<option value="haval160,5">HAVAL160,5</option>
<option value="haval192,5">HAVAL192,5</option>
<option value="haval224,5">HAVAL224,5</option>
<option value="haval256,5">HAVAL256,5</option>
</select><br />

Step 6: Finishing the Form: the Submit Button

After you have your list built, you need to add a submit button and finish the form and the paragraph.
<input type="submit" value="Hash it to pieces." /></form><p />

Save it and look at it. It should look something like the picture, minus the part where I am using a stylesheet. If it looks like that, it is time to make the PHP code that makes it all work.

Step 7: Final Touches: the PHP Code

What we will do is create a script that does the following:
1. Check if the variable, "message," has been set
2. Check if the variable, "message," is not empty or if the check box has been checked
3. Tell us the original text and the hashed output
4. If "message" is blank and the check box has not be checked, or if "message" has not been set, tell us there was nothing to hash.

I am just going to copy my code over.

<br />
<br />
<?php
if (isset($_POST["text"]) and (!empty($_POST["text"]) or !empty($_POST["blank"])))
{$_HASH = hash($_POST["method"],$_POST["text"]);
echo "Original text: " . $_POST["text"] . "<br />" . $_POST["method"] . " hash: " . $_HASH;}
if (!isset($_POST["text"]) or (empty($_POST["text"]) and empty($_POST["blank"])))
{echo "No input to hash, sorry.";}
?>

Step 8: See Your Code Work, If It Works

The final step, save your modified "index.php" and load it in the browser. Just start playing with it to see if it all works. The pictures I attached here shows mine working well.

Step 9: Source Code

This is just so you can see the entire code at once. I have put the code, and edited accordingly, to a web server. I'm not sure about <i>what</i> changes I made, so it will look different than what you have. But there are no major differences; this version has meta tags, a style.css link, a homepage link, and I turned the text yellow because the stylesheet made the background black. To get an idea of how it should look and how it should behave, I have the edited version up and live at http://www.muknot.com/hash_generator.php
The code I used in this instructable is appended to this step as a txt file.

<head><title>Hash Generator</title><link rel="stylesheet" type="text/css" href="./style.css" />
<meta name="description" value="Hash generator" />
<meta name="keywords" value="MD1, MD5, SHA256, whirlpool" /></head><html>
<h1>Hash Generator</h1>
<p id="1">This page will take a string of text you enter and generate a hash of your choice.<p />
<p id="form"><form action="./hash.php" method="post">
String/text here: <input type="text" name="text" /> Check to allow the hashing of blank inputs (leave the text field blank): <input type="checkbox" name="blank" value="blank"><br />
Hash Method: <select name="method">
<option value="md2">MD2</option>
<option value="md4">MD4</option>
<option value="md5">MD5</option>
<option value="sha1">SHA1</option>
<option value="sha256">SHA256</option>
<option value="sha384">SHA384</option>
<option value="sha512">SHA512</option>
<option value="ripemd128">RIPEMD128</option>
<option value="ripemd160">RIPEMD160</option>
<option value="ripemd256">RIPEMD256</option>
<option value="ripemd320">RIPEMD320</option>
<option value="whirlpool">WHIRLPOOL</option>
<option value="tiger128,3">TIGER128,3</option>
<option value="tiger160,3">TIGER160,3</option>
<option value="tiger192,3">TIGER 192,3</option>
<option value="tiger128,4">TIGER128,4</option>
<option value="tiger160,4">TIGER160,4</option>
<option value="tiger192,4">TIGER192,4</option>
<option value="snefru">SNEFRU</option>
<option value="gost">GOST</option>
<option value="adler32">ADLER32</option>
<option value="crc32">CRC32</option>
<option value="crc32b">CRC32B</option>
<option value="haval128,3">HAVAL128,3</option>
<option value="haval160,3">HAVAL160,3</option>
<option value="haval192,3">HAVAL192,3</option>
<option value="haval224,3">HAVAL224,3</option>
<option value="haval256,3">HAVAL256,3</option>
<option value="haval128,4">HAVAL128,4</option>
<option value="haval160,4">HAVAL160,4</option>
<option value="haval192,4">HAVAL192,4</option>
<option value="haval224,4">HAVAL224,4</option>
<option value="haval256,4">HAVAL256,4</option>
<option value="haval128,5">HAVAL128,5</option>
<option value="haval160,5">HAVAL160,5</option>
<option value="haval192,5">HAVAL192,5</option>
<option value="haval224,5">HAVAL224,5</option>
<option value="haval256,5">HAVAL256,5</option>
</select><br />
<input type="submit" value="Hash it in!" /></form></ p>
<br />
<br />
<?php
if (isset($_POST["text"]) and (!empty($_POST["text"]) or !empty($_POST["blank"])))
{$_HASH = hash($_POST["method"],$_POST["text"]);
echo "<font color='yellow'>Original text: " . $_POST["text"] . "<br />" . $_POST["method"] . " hash: " . $_HASH;}
if (!isset($_POST["text"]) or (empty($_POST["text"]) and empty($_POST["blank"])))
{echo "<font color='yellow'>No input to hash, sorry.";}
?><br /><br />
<p id="links"><a href="./index.php">Home, yo</a><br /><p />

Attachments