Skip to content

文字列からワンライナーでハッシュ値を求める

シェル上で、文字列からワンライナーでハッシュ値を求めるにはopensslshasumなどを用いる方法があります。opensslを使うのであれば例えば以下のように実行します。

echo -n "KEYWORD" | openssl sha256

シェルスクリプトとして実行し、アルゴリズム毎のハッシュ値を求める場合は以下のように実行します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/bash

KEYWORD="ABC"

echo -n ${KEYWORD} | openssl md5
echo -n ${KEYWORD} | openssl sha1
echo -n ${KEYWORD} | openssl sha224
echo -n ${KEYWORD} | openssl sha256
echo -n ${KEYWORD} | openssl sha384
echo -n ${KEYWORD} | openssl sha512
echo -n ${KEYWORD} | openssl sha3-224
echo -n ${KEYWORD} | openssl sha3-256
echo -n ${KEYWORD} | openssl sha3-384
echo -n ${KEYWORD} | openssl sha3-512

実行例は以下です。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
% ./hash.sh
MD5(stdin)= 902fbdd2b1df0c4f70b4a5d23525e932
SHA1(stdin)= 3c01bdbb26f358bab27f267924aa2c9a03fcfdb8
SHA2-224(stdin)= 107c5072b799c4771f328304cfe1ebb375eb6ea7f35a3aa753836fad
SHA2-256(stdin)= b5d4045c3f466fa91fe2cc6abe79232a1a57cdf104f7a26e716e0a1e2789df78
SHA2-384(stdin)= 1e02dc92a41db610c9bcdc9b5935d1fb9be5639116f6c67e97bc1a3ac649753baba7ba021c813e1fe20c0480213ad371
SHA2-512(stdin)= 397118fdac8d83ad98813c50759c85b8c47565d8268bf10da483153b747a74743a58a90e85aa9f705ce6984ffc128db567489817e4092d050d8a1cc596ddc119
SHA3-224(stdin)= 51e6db7cba212f1490b290e44c588e3a028c8334055c877910c3ebe6
SHA3-256(stdin)= 7fb50120d9d1bc7504b4b7f1888d42ed98c0b47ab60a20bd4a2da7b2c1360efa
SHA3-384(stdin)= 38078331baaa86dbe9b38224a0780e9661daa35b42066a804efd5215b2487b9728a19ae4940ddbcbda39b697f13ebebb
SHA3-512(stdin)= 077aa33882b1aaf06da41c7ed3b6a40d7128dee23505ca2689c47637111c4701645fabc5ee1b9dcd039231d2d086bff9819ce2da8647432a73966494dd1a77ad

参考

openssl

% openssl --help
help:

Standard commands
asn1parse         ca                ciphers           cmp
cms               configutl         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           skeyutl           smime             speed
spkac             srp               storeutl          ts
verify            version           x509

Message Digest commands (see the `dgst' command for more details)
blake2b512        blake2s256        md4               md5
mdc2              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            seed              seed-cbc
seed-cfb          seed-ecb          seed-ofb          sm4-cbc
sm4-cfb           sm4-ctr           sm4-ecb           sm4-ofb

shasum

% shasum --help
Usage: shasum [OPTION]... [FILE]...
Print or check SHA checksums.
With no FILE, or when FILE is -, read standard input.

  -a, --algorithm   1 (default), 224, 256, 384, 512, 512224, 512256
  -b, --binary      read in binary mode
  -c, --check       read SHA sums from the FILEs and check them
      --tag         create a BSD-style checksum
  -t, --text        read in text mode (default)
  -U, --UNIVERSAL   read in Universal Newlines mode
                        produces same digest on Windows/Unix/Mac
  -0, --01          read in BITS mode
                        ASCII '0' interpreted as 0-bit,
                        ASCII '1' interpreted as 1-bit,
                        all other characters ignored

The following five options are useful only when verifying checksums:
      --ignore-missing  don't fail or report status for missing files
  -q, --quiet           don't print OK for each successfully verified file
  -s, --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

  -h, --help        display this help and exit
  -v, --version     output version information and exit

When verifying SHA-512/224 or SHA-512/256 checksums, indicate the
algorithm explicitly using the -a option, e.g.

  shasum -a 512224 -c checksumfile

The sums are computed as described in FIPS PUB 180-4.  When checking,
the input should be a former output of this program.  The default
mode is to print a line with checksum, a character indicating type
(`*' for binary, ` ' for text, `U' for UNIVERSAL, `^' for BITS),
and name for each FILE.  The line starts with a `\' character if the
FILE name contains either newlines or backslashes, which are then
replaced by the two-character sequences `\n' and `\\' respectively.

Report shasum bugs to mshelor@cpan.org