Jump to: navigation, search

Mysql SHA2

From w3cyberlearnings

Contents

MySQL SHA2 Function

This function calculates an SHA-2 checksum, and it is a family of hasing functions (SHA-223, SHA-256, SHA-384, SHA-512).

Syntax SHA2

  • str: string to be hashed
  • length: hash length to indicate the desired bit length of the result, and it must be in a value of 224, 256, 384, 512, or 0 (0 equal to 256).
SHA2(str,length);

Note

This function was added in MySQL 5.5.5. SHA2() can be considered cryptographically more secure than MD5() or SHA1().

Example 1

mysql> SELECT SHA2('message',0);
+------------------------------------------------------------------+
| SHA2('message',0)                                                |
+------------------------------------------------------------------+
| ab530a13e45914982b79f9b7e3fba994cfd1f3fb22f71cea1afbf02b460c6d1d |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT SHA2('message',256);
+------------------------------------------------------------------+
| SHA2('message',256)                                              |
+------------------------------------------------------------------+
| ab530a13e45914982b79f9b7e3fba994cfd1f3fb22f71cea1afbf02b460c6d1d |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>

Example 2

mysql> INSERT INTO userpassword(id, username,password)
    -> VALUES(null,'Lili',SHA2('mypassword1',256));
Query OK, 1 row affected (0.08 sec)

mysql> SELECT * FROM userpassword\G
*************************** 1. row ***************************
      id: 1
username: Lili
password: 09343625c6c123d3434932fe1ce08bae5ac00a8f95bd746e10491b0bafdd1817
1 row in set (0.00 sec)

mysql> SELECT id,username FROM userpassword WHERE
    -> password=SHA2('mypassword1',256);
+----+----------+
| id | username |
+----+----------+
|  1 | Lili     |
+----+----------+
1 row in set (0.03 sec)

Related Links


Navigation
Web
SQL
MISC
References