0

I'm currently trying to create a mySQL database that holds hashes such as MD5 hashes. I'm using PHPmyAdmin version 3.3.9, and MySQL client version: 4.1.22

I already created a database named hashes. I'm new to mySQL so how can I add a table with data for a hash?

2
  • No I want the user to input something, then it hashes it and checks it against the hashes in the database. Commented Feb 3, 2011 at 19:41
  • MySQL 4.1.22? Did you rob a museum or something? Commented Feb 3, 2011 at 22:38

4 Answers 4

1

Hash column should be a CHAR(32) as that is the length of the hash:

CREATE TABLE `hashes` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    `hash` CHAR(32), 
    PRIMARY KEY (`id`)
);

mysql> describe hashes;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO   | PRI | NULL    | auto_increment |
| hash  | char(32) | YES  |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+

If you want to select from the table given user input:

-- Insert sample data:
mysql> INSERT INTO `hashes` VALUES (null, MD5('hello'));
Query OK, 1 row affected (0.00 sec)

-- Test retrieval:
mysql> SELECT * FROM `hashes` WHERE `hash` = MD5('hello');
+----+----------------------------------+
| id | hash                             |
+----+----------------------------------+
|  1 | 5d41402abc4b2a76b9719d911017c592 |
+----+----------------------------------+
1 row in set (0.00 sec)

You can add a key on hash for better performance.

Sign up to request clarification or add additional context in comments.

Comments

0

A hash can be stored as binary data or (more convenient) as text. The PHP MD5 function by default outputs a string of 32 characters, so you will need a table with a field that can hold a string of 32 character. That's all. :)

Comments

0

Take a look here: http://dev.mysql.com/doc/refman/5.1/en/create-table.html

You can do it using phpMyAdmin of course but it is better to understand the actual SQL.

Comments

0

If you open the database (click it's name in the left hand column), there will be a "Create new table on database dbname" section. Enter the name of the table you want (e.g. hashes), and the number of fields you want.

This is where we need a bit more information: if you just want to store hashes and hashes alone then you'll need two fields: a unique hash ID (of type integer, with auto_increment set to true) and a 32 character text field. I'm a little unsure of the utility of this, so if you post a bit more information we might be able to help you out with what you're trying to achieve a little better?

Edit: In that case, you'll need three fields: a hash id (a unique reference for each entry in the table), the hash, and the plaintext to which it correlates. Set the hash id to be an integer, with auto_increment set to true. Set the field for the hash to be varchar of length 32, and the plaintext field to 'text'.

6 Comments

Well I need two things. The MD5 hash and the plain text. So it checks the hash the user provided against hashes in the table and if there is a match it returns the plain text. Thanks for the help.
Okay, you're also going to need to store the plain text in the database (hashing is a one-way function: once plaintext has been hashed, there is no reliable or realistic way to retrieve the plaintext from the hash). I suggest adding a third field of type "text" which stores the plaintext associated with the hash. This can then be retrieved if the hash provided the user matches something.
I hope you don't mean the MD5 hash OF the plain text, AND the plain text itself (i.e., the hash and plain text should be unrelated, otherwise you're storing redundant data).
Sorry Jasie; you are completely correct. Storing the hash along with the plaintext would be pointless. AustinM, can you give us more of an idea of what you're trying to do, and maybe someone can suggest a better approach?
Well I'm working on an MD5 decoder. The database will have pairs of hash and plaintext. Then the user inputs a hash and it checks it against a hash in the DB to see if it exists. And if it does it will print the plaintext.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.