「MySQL」SHA関数で文字列を暗号化するサンプル

2022年1月5日

環境
Windows10 64bit
MySQL 8.0.27

書式
SHA(文字列 )
この暗号化方式は一方向の暗号化であり、復号して元の文字列に戻すことはできません。

使用例1
SHA関数を使用して、文字列を暗号化します

mysql> SELECT SHA( 'arkgame' ) as result;
+------------------------------------------+
| result                                   |
+------------------------------------------+
| f7cb9fd654b84482c166ca18c5a7e9cffba4a9d1 |
+------------------------------------------+
1 row in set (0.00 sec)

暗号化される文字列の長さを確認します

mysql> select length(sha('arkgame')) as result;
+--------+
| result |
+--------+
|     40 |
+--------+
1 row in set (0.00 sec)

使用例2
文字列が空白の場合

mysql> select sha('') as result;
+------------------------------------------+
| result                                   |
+------------------------------------------+
| da39a3ee5e6b4b0d3255bfef95601890afd80709 |
+------------------------------------------+
1 row in set (0.00 sec)

暗号化される文字列の長さを確認します

mysql> select length(sha('')) as result;
+--------+
| result |
+--------+
|     40 |
+--------+
1 row in set (0.00 sec)

 

MySQL

Posted by arkgame