このエントリーをはてなブックマークに追加


ファイルのハッシュ値を求める・certutil

コマンドプロンプトで、 certutilコマンドで-hashfileオプションを使えばファイルのハッシュ値を求めることができます。
このcertutilコマンドは、Base64のエンコードやデコードなどもできる便利なコマンドです。

以下に certutil コマンドを使ってファイルのハッシュ値を求めた実行例を記します。
尚、本資料は Windows10 ver.1809 で動作確認を行いました。


関連記事

certutilコマンドについて

certutilの-hashfileオプションについて以下のコマンドで調べてみます。

C:\Users\sakura>certutil /?

動詞:
<省略>
 -decodehex        -- 16 進エンコード ファイルをデコードします
 -decode           -- Base 64 エンコード ファイルをデコードします
 -encode           -- ファイルを Base 64 にエンコードします
<省略>
 -hashfile         -- ファイルに暗号化ハッシュを生成し表示します
<省略>

上記の通り、-hashfileオプションを使えば、ファイルのハッシュ値が取得できるのが確認できます。

オプション -hashfileの使い方を調べてみます。

C:\Users\sakura>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 -?           -- すべての動詞のヘルプ テキストをすべて表示します

上記により、使い方の構文は以下のようにようになります。

CertUtil [オプション] -hashfile InFile [HashAlgorithm]

実際に使用し、ハッシュ値を求める

以下のようにファイル(hello.txt)を作成し、サポートしているアルゴリズムのハッシュ値を求めてみます。

上記のヘルプで出力されたアルゴリズムを試してみます。
コマンドは以下の通りです。

certUtil -hashfile hello.txt MD2
certUtil -hashfile hello.txt MD4
certUtil -hashfile hello.txt MD5
certUtil -hashfile hello.txt SHA1
certUtil -hashfile hello.txt SHA256
certUtil -hashfile hello.txt SHA384
certUtil -hashfile hello.txt SHA512

実行結果

hello.txtというテキストファイルを作成し、ハッシュ値を計算しています。

D:\work>echo hello world > hello.txt
D:\work>type hello.txt
hello world
D:\work>certUtil -hashfile hello.txt MD2
MD2 ハッシュ (対象 hello.txt):
293be77984faea7ecb5beaa93a80b150
CertUtil: -hashfile コマンドは正常に完了しました。
D:\work>certUtil -hashfile hello.txt  MD4
MD4 ハッシュ (対象 hello.txt):
136798e830bc993b548c588793c67349
CertUtil: -hashfile コマンドは正常に完了しました。
D:\work>certUtil -hashfile hello.txt  MD5
MD5 ハッシュ (対象 hello.txt):
d1b9c5009a6ddd7dacb45eddb78fa23a
CertUtil: -hashfile コマンドは正常に完了しました。
D:\work>certUtil -hashfile hello.txt  SHA1
SHA1 ハッシュ (対象 hello.txt):
6288696b16c2ec9c48592475beb5e33dddf507bf
CertUtil: -hashfile コマンドは正常に完了しました。
D:\work>certUtil -hashfile hello.txt  SHA256
SHA256 ハッシュ (対象 hello.txt):
0d74c782a0fd750336d9703eb9995985255bb5dd383c28d1828a6379a5418e4e
CertUtil: -hashfile コマンドは正常に完了しました。
D:\work>certUtil -hashfile hello.txt  SHA384
SHA384 ハッシュ (対象 hello.txt):
79b2dbd854939271dcbf2079f0b78b954e25d78fba65545ad5ade9e4ce28f0b2e4b71437536445b5b05cef2c72a7d70a
CertUtil: -hashfile コマンドは正常に完了しました。
D:\work>certUtil -hashfile hello.txt  SHA512
SHA512 ハッシュ (対象 hello.txt):
8db14cdb2b9231b09e188547b97998faf2822b860eb4fbf6ab747c09ef9bd9f986604f4ddd163674a32c766343ac6ca1934dde63f318f01f94e61a287430411e
CertUtil: -hashfile コマンドは正常に完了しました。

以上、コマンドプロンプトでファイルのハッシュ値を求めることができるCertUtilコマンドの紹介でした。



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019-03-31 (日) 18:30:55