こんにちは、伊藤です。
パスワードハッシュを使用したシステムを開発する際、プログラムが生成したパスワードハッシュが正しいかどうかテストすることがあります。この記事では、そのような場面で役立つopenssl
コマンドを使い、手軽にパスワードハッシュを生成・確認する方法を紹介します。
検証環境
検証で使用したOSとOpenSSLのバージョンは以下のとおりです。
OSRocky Linux OS 9.2
OpenSSLのバージョン
$ openssl version
OpenSSL 3.0.7 1 Nov 2022 (Library: OpenSSL 3.0.7 1 Nov 2022)
パスワードハッシュの出力
使用できるパスワードハッシュアルゴリズムを確認します。openssl
コマンドをそのまま入力すると、helpが表示されます。
$ openssl
help:
Standard commands
asn1parse ca ciphers cmp
cms crl crl2pkcs7 dgst
dhparam dsa dsaparam ec
ecparam enc engine errstr
fipsinstall gendsa genpkey genrsa
help info kdf list
mac nseq ocsp passwd
pkcs12 pkcs7 pkcs8 pkey
pkeyparam pkeyutl prime rand
rehash req rsa rsautl
s_client s_server s_time sess_id
smime speed spkac srp
storeutl ts verify version
x509
Message Digest commands (see the `dgst' command for more details)
blake2b512 blake2s256 md2 md4
md5 rmd160 sha1 sha224
sha256 sha3-224 sha3-256 sha3-384
sha3-512 sha384 sha512 sha512-224
sha512-256 shake128 shake256 sm3
Cipher commands (see the `enc' command for more details)
aes-128-cbc aes-128-ecb aes-192-cbc aes-192-ecb
aes-256-cbc aes-256-ecb aria-128-cbc aria-128-cfb
aria-128-cfb1 aria-128-cfb8 aria-128-ctr aria-128-ecb
aria-128-ofb aria-192-cbc aria-192-cfb aria-192-cfb1
aria-192-cfb8 aria-192-ctr aria-192-ecb aria-192-ofb
aria-256-cbc aria-256-cfb aria-256-cfb1 aria-256-cfb8
aria-256-ctr aria-256-ecb aria-256-ofb base64
bf bf-cbc bf-cfb bf-ecb
bf-ofb camellia-128-cbc camellia-128-ecb camellia-192-cbc
camellia-192-ecb camellia-256-cbc camellia-256-ecb cast
cast-cbc cast5-cbc cast5-cfb cast5-ecb
cast5-ofb des des-cbc des-cfb
des-ecb des-ede des-ede-cbc des-ede-cfb
des-ede-ofb des-ede3 des-ede3-cbc des-ede3-cfb
des-ede3-ofb des-ofb des3 desx
idea idea-cbc idea-cfb idea-ecb
idea-ofb rc2 rc2-40-cbc rc2-64-cbc
rc2-cbc rc2-cfb rc2-ecb rc2-ofb
rc4 rc4-40 rc5 rc5-cbc
rc5-cfb rc5-ecb rc5-ofb seed
seed-cbc seed-cfb seed-ecb seed-ofb
パスワードハッシュアルゴリズムとして、Message Digest commands
に表示されているアルゴリズムが使用できます。
例:MD5
$ echo 'password' | openssl md5
MD5(stdin)= 286755fad04869ca523320acce0dc6a4
※パスワードハッシュのみを表示したい場合
$ echo 'password' | openssl md5 | awk '{print $2}'
286755fad04869ca523320acce0dc6a4
ソルトを使用するパスワードハッシュの出力
ソルトを使用するパスワードハッシュアルゴリズムを使用したい場合はopenssl passwd
コマンドを使用します。openssl passwd
で使用できるパスワードハッシュアルゴリズムを確認します。Cryptographic options
の部分が使用できるパスワードハッシュアルゴリズムです。
$ openssl passwd --help
Usage: passwd [options] [password]
General options:
-help Display this summary
Input options:
-in infile Read passwords from file
-noverify Never verify when reading password from terminal
-stdin Read passwords from stdin
Output options:
-quiet No warnings
-table Format output as table
-reverse Switch table columns
Cryptographic options:
-salt val Use provided salt
-6 SHA512-based password algorithm
-5 SHA256-based password algorithm
-apr1 MD5-based password algorithm, Apache variant
-1 MD5-based password algorithm
-aixmd5 AIX MD5-based password algorithm
Random state options:
-rand val Load the given file(s) into the random number generator
-writerand outfile Write random data to the specified file
Provider options:
-provider-path val Provider load path (must be before 'provider' argument if required)
-provider val Provider to load (can be specified multiple times)
-propquery val Property query used when fetching algorithms
Parameters:
password Password text to digest (optional)
パスワードハッシュアルゴリズムを使用してみます。
例:MD5Crypt
$ openssl passwd -1 -salt 'salt' 'password'
$1$salt$qJH7.N4xYta3aEG/dfqo/0
まとめ
今回はopenssl
コマンドを使い、パスワードハッシュを生成する方法を紹介しました。
開発中のテストや検証で、ぜひ活用してみてください。