🍏

LinuxでSHA-512のパスワードハッシュを作成する

2025/01/18に公開

はじめに

Opensslを使ってSHA-512のパスワードハッシュを作成する方法を記載する。Openssl v1系だと対応していないため、最新のバージョンにアップデートした後にハッシュを作成する。

手順

  • 実行環境
# cat /etc/system-release
Amazon Linux release 2 (Karoo)
# openssl version
OpenSSL 1.0.2k-fips  26 Jan 2017

“-6”はSHA-512で平文を暗号化するためのオプション。opensslのバージョンが古いためにオプションをサポートしていない。これを最新のバージョンにアップデートする。

# openssl passwd -6
Usage: passwd [options] [passwords]
where options are
-crypt             standard Unix password algorithm (default)
-1                 MD5-based password algorithm
-apr1              MD5-based password algorithm, Apache variant
-salt string       use provided salt
-in file           read passwords from file
-stdin             read passwords from stdin
-noverify          never verify when reading password from terminal
-quiet             no warnings
-table             format output as table
-reverse           switch table columns

最新のソースをダウンロードして、インストール。
makeの処理に5分くらいかかる。最近はコンパイルのコマンドが./configureではなく ./configになっている。へぇぇ。

# cd /usr/local/src/
# sudo wget https://www.openssl.org/source/openssl-3.4.0.tar.gz
# ./config
Configuring OpenSSL version 3.4.0 for target linux-x86_64
Using os-specific seed configuration
Created configdata.pm
Running configdata.pm
Created Makefile.in
Created Makefile
Created include/openssl/configuration.h

**********************************************************************
***                                                                ***
***   OpenSSL has been successfully configured                     ***
***                                                                ***
***   If you encounter a problem while building, please open an    ***
***   issue on GitHub <https://github.com/openssl/openssl/issues>  ***
***   and include the output from the following command:           ***
***                                                                ***
***       perl configdata.pm --dump                                ***
***                                                                ***
***   (If you are new to OpenSSL, you might want to consult the    ***
***   'Troubleshooting' section in the INSTALL.md file first)      ***
***                                                                ***
**********************************************************************

# make
# make install

バージョン確認するも、共有ライブラリのlibssl.so.3 が見つからないことがわかる。
/src/openssl-3.4.0/libssl.so.3 はソースを展開したファイルなので、インストールされた /usr/local/lib64/libssl.so.3 に対して、パスを通す。

# openssl version
openssl: error while loading shared libraries: libssl.so.3: cannot open shared object file: No such file or directory

# which openssl
/usr/local/bin/openssl
# ldd /usr/local/bin/openssl
        linux-vdso.so.1 (0x00007fffac95e000)
        libssl.so.3 => not found
        libcrypto.so.3 => not found
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f65ebce1000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f65ebac3000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f65eb716000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f65ebee5000)

# find / | grep libssl.so.3
/usr/local/lib64/libssl.so.3
/usr/local/src/openssl-3.4.0/libssl.so.3

共有ライブラリの環境変数にパスを指定する。シェル起動時に実行されるbashrcに環境変数パスを指定することでログインする度に読み込んでくれる。

# echo $LD_LIBRARY_PATH

# vim ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/lib64 ← 最後の行に追加する。
# source ~/.bashrc
# echo $LD_LIBRARY_PATH
/usr/local/lib64
# ldd /usr/local/bin/openssl
        linux-vdso.so.1 (0x00007ffd643e8000)
        libssl.so.3 => /usr/local/lib64/libssl.so.3 (0x00007f8991d3b000)
        libcrypto.so.3 => /usr/local/lib64/libcrypto.so.3 (0x00007f8991654000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f8991450000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f8991232000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f8990e85000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f8992037000)

必要なライブラリがそろったので、以下コマンドで発行。

# openssl version
OpenSSL 3.4.0 22 Oct 2024 (Library: OpenSSL 3.4.0 22 Oct 2024)
# openssl passwd -6
Password:
Verifying - Password:
$6$SSzR7n7QPjy7px44$dV9ZQKiB3lOgdlIEv7d8JPgI1vIGaVvbX5bK0pFRVWFj0WCBr1TCC4RkSXGikml6A4igsg78C1afne4EziA6c.

OpenSSL 3.4.0 のオプション一覧は以下のとおり。

# 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)

Discussion