What data type to use for hashed password field in MySQL?

MySQLMySQLi Database

The hashed password data type depends upon which hashing algorithm we are using. The hashing algorithm does not depends upon the input size because it produces a result of the same length. It gives the result in a series of hexadecimal digits, and we can reduce the hexadecimal digits by half with the help of UNHEX() function.

There are various algorithms and data types to store values.

  • MD5 − It can use char(32) or BINARY(16).

  • SHA-1 − It can use data type char(40) or BINARY(20).

Example of MD5

The following is an example −

mysql> select MD5('This is a hashed password');

Here is the output.

+----------------------------------+
| MD5('This is a hashed password') |
+----------------------------------+
| e9d4c42db40abbb4724a0047f7e91e67 |
+----------------------------------+
1 row in set (0.03 sec)

To know the length of the hashed password.

mysql>  SELECT CHARACTER_LENGTH(MD5('This is a hashed password'));
+----------------------------------------------------+
| CHARACTER_LENGTH(MD5('This is a hashed password')) |
+----------------------------------------------------+
|                                                 32 |
+----------------------------------------------------+
1 row in set (0.04 sec)

Example of SHA-1

mysql> select SHA1('This is a hashed password');

The following is the output.

+------------------------------------------+
| SHA1('This is a hashed password')        |
+------------------------------------------+
| 4e2e1a39dba84a0b5a91043bb0e4dbef23970837 |
+------------------------------------------+
1 row in set (0.00 sec)

We can know the length with the help of character_length() function.

mysql>  SELECT CHARACTER_LENGTH(SHA1('This is a hashed password'));

The following is the output.

+-----------------------------------------------------+
| CHARACTER_LENGTH(SHA1('This is a hashed password')) |
+-----------------------------------------------------+
|                                                  40 |
+-----------------------------------------------------+
1 row in set (0.00 sec)
raja
Published on 29-Nov-2018 06:35:39
Advertisements