ハッシュ値生成の色々な方法 コマンド編
Windows
CertUtil
Windowsの標準コマンドでは、CertUtil コマンドで ファイルのハッシュ値を生成することが出来ます。使い方は、以下の通りです。
CertUtil -hashfile 対象ファイル名 ハッシュアルゴリズム
コマンドのヘルプで確認すると、以下の様に出力されました。
> CertUtil -hashfile -?
使用法:
CertUtil [オプション] -hashfile InFile [HashAlgorithm]
ファイルに暗号化ハッシュを生成し表示します
オプション:
-Unicode -- リダイレクトされた出力を Unicode として書き込む
-gmt -- 時刻を GMT で表示します
-seconds -- 時間を秒とミリ秒で表示します
-v -- メッセージを詳細に表示します
-privatekey -- パスワードと秘密キーのデータを表示します
-pin PIN -- スマート カードの PIN
-sid WELL_KNOWN_SID_TYPE -- 数値 SID
22 -- ローカル システム
23 -- ローカル サービス
24 -- ネットワーク サービス
ハッシュ アルゴリズム: MD2 MD4 MD5 SHA1 SHA256 SHA384 SHA512
CertUtil -? -- 動詞の一覧 (コマンドの一覧) を表示します
CertUtil -hashfile -? -- "hashfile" 動詞のヘルプ テキストを表示します
CertUtil -v -? -- すべての動詞のヘルプ テキストをすべて表示します
SHA1のハッシュ値を生成しました。
> CertUtil -hashfile HelloWorld.txt sha1
SHA1 ハッシュ (対象 HelloWorld.txt):
0a4d55a8d778e5022fab701977c5d840bbc486d0
CertUtil: -hashfile コマンドは正常に完了しました。
SHA256のハッシュ値を生成しました。
> CertUtil -hashfile HelloWorld.txt sha256
SHA256 ハッシュ (対象 HelloWorld.txt):
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
CertUtil: -hashfile コマンドは正常に完了しました。
SHA512のハッシュ値を生成しました。
> CertUtil -hashfile HelloWorld.txt sha512
SHA512 ハッシュ (対象 HelloWorld.txt):
2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b
CertUtil: -hashfile コマンドは正常に完了しました。
Windows版 OpenSSL
Windows版のOpenSSLのコマンド を使って、ハッシュ値を生成します。
ハッシュ値を取得するときのコマンドは、以下の通りです。
> openssl ハッシュタイプ 対象ファイル名
コマンドのヘルプで確認すると、以下の様に出力されました。
> openssl dgst --help
Usage: dgst [options] [file...]
General options:
-help Display this summary
-list List digests
-engine val Use engine e, possibly a hardware device
-engine_impl Also use engine given by -engine for digest operations
-passin val Input file pass phrase source
Output options:
-c Print the digest with separating colons
-r Print the digest in coreutils format
-out outfile Output to filename rather than stdout
-keyform format Key file format (ENGINE, other values ignored)
-hex Print as hex dump
-binary Print in binary form
-xoflen +int Output length for XOF algorithms
-d Print debug info
-debug Print debug info
Signing options:
-sign val Sign digest using private key
-verify val Verify a signature using public key
-prverify val Verify a signature using private key
-sigopt val Signature parameter in n:v form
-signature infile File with signature to verify
-hmac val Create hashed MAC with key
-mac val Create MAC (not necessarily HMAC)
-macopt val MAC algorithm parameters in n:v form or key
-* Any supported digest
-fips-fingerprint Compute HMAC with the key used in OpenSSL-FIPS fingerprint
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:
file Files to digest (optional; default is stdin)
SHA1のハッシュ値を生成しました。
> openssl sha1 HelloWorld.txt
SHA1(HelloWorld.txt)= 0a4d55a8d778e5022fab701977c5d840bbc486d0
OpenSSLの場合、入力を標準入力からも行えます。
> type HellowWorld.txt | openssl sha1
SHA1(stdin)= 0a4d55a8d778e5022fab701977c5d840bbc486d0
> type HelloWorld.txt | openssl sha256
SHA256(stdin)= a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
> type HellowWorld.txt | openssl sha512
SHA512(stdin)= 2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b
Windows版 LibreSSL
LibreSSLは、コマンドが OpenSSLと同じで opensslです。
> "C:\Program Files (x86)\LibreSSL\bin\openssl.exe" sha1 HelloWorld.txt
SHA1(HelloWorld.txt)= 0a4d55a8d778e5022fab701977c5d840bbc486d0
> type HelloWorld.txt | "C:\Program Files (x86)\LibreSSL\bin\openssl.exe" sha1
(stdin)= 0a4d55a8d778e5022fab701977c5d840bbc486d0
> type HelloWorld.txt | "C:\Program Files (x86)\LibreSSL\bin\openssl.exe" sha256
(stdin)= a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
> type HelloWorld.txt | "C:\Program Files (x86)\LibreSSL\bin\openssl.exe" sha512
(stdin)= 2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b
Linux Ubuntu
Linux系はOpensslを使用してハッシュ値を作成するか、GNU Core Utilitiesに含まれているsha1sum, sha256sumを使用してハッシュ値を作成します。
OpenSSLのコマンドの指定方法は、Windows系のOpenSSLのコマンドの指定方法と同じです。
sha1sum, sha256sum, sha512sum
sha1sum コマンドの指定方法は、以下のとおりです。
$ sha1sum 対象ファイル名
sha1sumの実施例
$ sha1sum HelloWorld.txt
0a4d55a8d778e5022fab701977c5d840bbc486d0 HelloWorld.txt
入力が標準入力でも行えます。
$ echo -n "Hello World" | sha1sum
0a4d55a8d778e5022fab701977c5d840bbc486d0
sah1sumのヘルプ
$ sha1sum --help
Usage: sha1sum [OPTION]... [FILE]...
Print or check SHA1 (160-bit) checksums.
ファイルの指定がない場合や FILE が - の場合, 標準入力から読み込みを行います。
-b, --binary read in binary mode
-c, --check FILE から SHA1 チェックサムを読み込み、照合する
--tag create a BSD-style checksum
-t, --text テキストモードで読み込む (デフォルト)
-z, --zero end each output line with NUL, not newline,
and disable file name escaping
The following five options are useful only when verifying checksums:
--ignore-missing don't fail or report status for missing files
--quiet don't print OK for each successfully verified file
--status don't output anything, status code shows success
--strict exit non-zero for improperly formatted checksum lines
-w, --warn warn about improperly formatted checksum lines
--help この使い方を表示して終了する
--version バージョン情報を表示して終了する
The sums are computed as described in FIPS-180-1. When checking, the input
should be a former output of this program. The default mode is to print a
line with checksum, a space, a character indicating input mode ('*' for binary,
' ' for text or where binary is insignificant), and name for each FILE.
Note: There is no difference between binary mode and text mode on GNU systems.
GNU coreutils のオンラインヘルプ: <https://www.gnu.org/software/coreutils/>
翻訳に関するバグは <https://translationproject.org/team/ja.html> に連絡してください。
詳細な文書 <https://www.gnu.org/software/coreutils/sha1sum>
(ローカルでは info '(coreutils) sha1sum invocation' で参照可能)。
sha256sumの実施例です
$ sha256sum HelloWorld.txt
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e HelloWorld.txt
sha512sumの実施例です
$ sha512sum HelloWorld.txt
2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b HelloWorld.txt
OpenSSL
OpenSSLでの実施例です
$ openssl sha1 HelloWorld.txt
SHA1(HelloWorld.txt)= 0a4d55a8d778e5022fab701977c5d840bbc486d0
$ cat HelloWorld.txt | openssl sha1
SHA1(stdin)= 0a4d55a8d778e5022fab701977c5d840bbc486d0
$ cat HelloWorld.txt | openssl dgst -sha1
SHA1(stdin)= 0a4d55a8d778e5022fab701977c5d840bbc486d0
$ cat HelloWorld.txt | openssl sha256
SHA256(stdin)= a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e