0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PostgreSQL でハッシュ値を生成する

Posted at

標準でインストールしたPostgreSQLでは、md5, sha256, sha384, sha512のハッシュアルゴリズムのSQL関数が提供されています。なぜか、sha1がありません。

関数 説明 戻り値
md5(バイナリ文字列) バイナリ文字列のMD5ハッシュ計算し、16進数で結果を返します。 テキスト型
sha256(バイナリ文字列) SHA256 ハッシュ値を返します。 バイナリ文字列
sha384(バイナリ文字列) SHA384 ハッシュ値を返します。 バイナリ文字列
sha512(バイナリ文字列) SHA512 ハッシュ値を返します。 バイナリ文字列

MD5のハッシュ値を返します

postgres=# select md5('Hello World');
               md5                
----------------------------------
 b10a8db164e0754105b7a99be72e3fe5
(1 row)

SHA256のハッシュ値を返します

postgres=# select sha256('Hello World');
                               sha256                               
--------------------------------------------------------------------
 \xa591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
(1 row)

postgres=# select encode(sha256('Hello World'), 'hex');
                              encode                              
------------------------------------------------------------------
 a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
(1 row)

SHA384, SHA512のハッシュ値を返します

postgres=# select encode(sha384('Hello World'), 'hex');
                                              encode                                              
--------------------------------------------------------------------------------------------------
 99514329186b2f6ae4a1329e7ee6c610a729636335174ac6b740f9028396fcc803d0e93863a7c3d90f86beee782f4f3f
(1 row)

postgres=# select encode(sha512('Hello World'), 'hex');
                                                              encode                                                              
----------------------------------------------------------------------------------------------------------------------------------
 2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b
(1 row)

pgcryptoモジュール

pgcryptoモジュール には、汎用ハッシュ関数が提供されていて、md5、sha1、sha224、sha256、sha384、およびsha512のハッシュを生成できます。

pgcryptoモジュールのインストール

pgcrypto モジュールを以下のコマンドでインストールします

postgres=# CREATE EXTENSION PGCRYPTO;
CREATE EXTENSION

汎用ハッシュ関数

関数 説明 戻り値
digest(data text, type text) バイナリ文字列のMD5ハッシュ計算し、16進数で結果を返します。 バイナリ文字列型

type で指定できるアルゴリズムは、md5、sha1、sha224、sha256、sha384、およびsha512 です。

digest関数を使用して、SHA1のハッシュ値を生成します。

postgres=# select encode(digest('Hello World', 'sha1'), 'hex');
                  encode                  
------------------------------------------
 0a4d55a8d778e5022fab701977c5d840bbc486d0
(1 row)

digest関数を使用して、SHA256のハッシュ値を生成します。

postgres=# select encode(digest('Hello World', 'sha256'), 'hex');
                              encode                              
------------------------------------------------------------------
 a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
(1 row)

参照情報

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?