我们可以在包含用户密码列的 MySQL 表中将 MD5 转换为 SHA256 吗?

mysqlmysqli database更新于 2025/11/2 21:32:17

使用 SHA2() 将 MD5 密码转换为 SHA256。它计算 SHA-2 系列哈希函数(即 SHA-224、SHA-256、SHA-384 和 SHA-512)。

首先我们创建一个表 −

mysql> create table DemoTable818(UserPassword text);
Query OK, 0 rows affected (0.51 sec)

使用 insert 插入命令在表中插入一些记录 −

mysql> insert into DemoTable818 values(MD5('John_123'));
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable818 values(MD5('999Carol@22'));
Query OK, 1 row affected (0.14 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable818;

这将产生以下输出 −

+----------------------------------+
| UserPassword                     |
+----------------------------------+
| 47c7d0987db4e59e2264ce9fefce4977 |
| 950aa70edd5b686a807b3bfffdf2248c |
+----------------------------------+
2 rows in set (0.00 sec)

以下是将 MD5 转换为 SHA256 的查询 −

mysql> update DemoTable818 set UserPassword=SHA2(UserPassword,256);
Query OK, 2 rows affected (0.19 sec)
Rows matched: 2 Changed: 2 Warnings: 0

让我们再次检查表记录 −

mysql> select *from DemoTable818;

这将产生以下输出 −

+------------------------------------------------------------------+
| UserPassword                                                     |
+------------------------------------------------------------------+
| 8b68c46294a9ccb2449324c24fe774f95b7c14e4b56fc51c7f8e6c5b01c7020f |
| 9cc80741546051ae3de7d31246327968c98af3c65125376acb7b49a0760d42a3 |
+------------------------------------------------------------------+
2 rows in set (0.00 sec)


相关文章


有用资源