在mac系统中,自带的有命令md5 可以帮我们计算文件或者字符串的md5值, shasum命令可以帮我们计算文件sha值。 其使用方法如下:

md5 命令

语法: md5 [-pqrtx] [-s string] [file ...]

ps:  在unix类系统的命令语法中,中括号[] 中的表示可选项,这个是默认的公用规范,必须要学会!!

计算字符串md5

➜  ~ md5 -s admin
MD5 ("admin") = 21232f297a57a5a743894a0e4a801fc3

计算文件md5

➜  ~ md5 yourfile.txt
MD5 (yourfile.txt) = 7b4b08855739c05400155d5a3445be78

md5命令选项
-s string
             Print a checksum of the given string.

 -p      Echo stdin to stdout and append the checksum to stdout.

 -q      Quiet mode - only the checksum is printed out.  Overrides the -r
         option.

 -r      Reverses the format of the output.  This helps with visual diffs.
         Does nothing when combined with the -ptx options.

 -t      Run a built-in time trial.

 -x      Run a built-in test script.
 

shasum命令

计算文件hash 支持的sha算法有 224, 256, 384, 512, 512224, 512256

➜  ~ shasum -a 256 yourfile.txt
028fc9f1c34dc30358d650ab61c757f1e087545690642e24ede8d947a146af4c  yourfile.txt

使用默认算法 sha224来计算文件sha

➜  ~ shasum  yourfile.txt
1b2ed9e3929e0461108cf2c224485ba75135844b  yourfile.txt


命令选项:
  -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.

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐