LoginSignup
9
3

More than 5 years have passed since last update.

コマンドライン上でMD5のハッシュ値を取得

Posted at

前置き

ファイルのハッシュ値を取得したくなるときがある。主にファイルの転送を行い、それらの同一性を担保したいときなど)

そういうときは、md5sumコマンドでハッシュ値を求めて比較をする。

$ echo "abcABC123" > foo.txt
$ md5sum foo.txt
c92cd7e4a8f52284bf956ac63b72bb13  foo.txt

MD5ではなくSHA-256が良い場合はこう。

$ sha256sum foo.txt
6916802971d6cbd36dcc535bbad309ecc7c099b330ba0b4ed3a980157fc4da35  foo.txt

本題

ファイルではなく、任意の文字列のハッシュ値を取得したい場合はどうするか。

echoを使うのが簡単だ。

// echoは末尾に改行コードを挿入するので、"-n"が必要
$ echo -n "abcABC123" | md5sum
480aeb42d7b1e3937fe8db12a1ffe6d8  -

さきほどの例と結果が違うのは、コメントにも書いたが、改行コードの有無による。

$ echo "abcABC123" > foo.txt
$ md5sum foo.txt
c92cd7e4a8f52284bf956ac63b72bb13  foo.txt

// "-n"を付けない
$ echo "abcABC123" | md5sum -
c92cd7e4a8f52284bf956ac63b72bb13  -

いっしょになった。

9
3
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
9
3