Answers:
打印或检查SHA1(160位)校验和。如果没有FILE,或者当FILE为-时,请读取标准输入。
sha1sum {file}
如果要将文件及其sha1sum输出一起发送,请将输出重定向到文件:
sha1sum {file} > {file}.sha1
发送文件和对方都可以做...
sha1sum -c {file}.sha1
它应该显示OK
,如果sha1
是正确的。
sha1sum -c
当{file}.sha1
仅包含散列而不包含文件名(经常从Internet的各个角落下载)时,如何运行?我想出了for f in *.sha1; do echo "$(cat $f) ${f/.sha1/}"; done | sha1sum -c
(请注意双空格),但这必须简单得多。
shasum
-默认SHA是(如果我正确的话)SHA1。您也可以使用以下-a, --algorithm
选项进行设置:shasum -a 1
<hash> <full file path>
因此有足够的信息供sha1sum -c知道要验证的文件
sha1sum <file_path> <the_expected_hash>
吗?
不创建本地文件:
$ sha1sum filename
8dd10000eb1b768800000e1d2fe1c3100005d2dc *filename
要进行检查,请转到包含filename
并运行以下命令的目录:
echo "8dd10000eb1b768800000e1d2fe1c3100005d2dc *filename" | sha1sum -c -
echo 'b78bb50bdac5ec8c108f34104f788e214ac23635 raspbian.zip' | sha1sum -c -
将检查当前目录中的文件名raspbian.zip。
对于在Mac上且未安装coreutils / sha1sum的用户。
openssl sha1 <file>
例:
$ openssl sha1 `mktemp`
SHA1(/tmp/tmp.jkyfOWma3t)= da39a3ee5e6b4b0d3255bfef95601890afd80709
你们在说什么?是的,我得到了sha1sum背后的概念,但是上面的信息令人难以置信。首先,Ubuntu似乎没有sha1sum文件-诸如Mate 16.04 Beta 1这样的网页上的字符串:
bfba577970d573e0ba5095fbb72787de97f88b4b *ubuntu-mate-16.04-beta1-desktop-amd64.iso
efcbbc70b10173cea203df30396d0848ba8fa0d8 *ubuntu-mate-16.04-beta1-desktop-i386.iso
8563fec4d66bce851b0800f5ac746f38e4041a6a *ubuntu-mate-16.04-beta1-desktop-powerpc.iso
要检查下载的.iso的完整性,请打开终端程序,依次执行“ cd Downloads”和sha1sum。稍后,终端将产生一个哈希,例如
efcbbc70b10173cea203df30396d0848ba8fa0d8 ubuntu-mate-16.04-beta1-desktop-i386.iso
接下来,我们必须转到网页并比较字符串以验证验证是否有效。几乎没有那么容易。
戴夫
Python具有出色的hashlib
库,该库允许计算多个哈希值,包括sha1
。这是可以完成此工作的简单脚本:
#!/usr/bin/env python3
import sys
import hashlib
import os
from collections import OrderedDict as od
def get_hashsums(file_path):
hash_sums = od()
hash_sums['md5sum'] = hashlib.md5()
hash_sums['sha1sum'] = hashlib.sha1()
hash_sums['sha224sum'] = hashlib.sha224()
hash_sums['sha256sum'] = hashlib.sha256()
hash_sums['sha384sum'] = hashlib.sha384()
hash_sums['sha512sum'] = hashlib.sha512()
with open(file_path, 'rb') as fd:
data_chunk = fd.read(1024)
while data_chunk:
for hashsum in hash_sums.keys():
hash_sums[hashsum].update(data_chunk)
data_chunk = fd.read(1024)
results = od()
for key,value in hash_sums.items():
results[key] = value.hexdigest()
return results
def main():
for path in sys.argv[1:]:
print(">>> ",path)
for key,value in get_hashsums(path).items():
print(key,value)
if __name__ == '__main__': main()
一个文件:
$ ./hash_sums.py /etc/passwd
>>> /etc/passwd
md5sum ce5f247e016ba2bb92049fc86158376a
sha1sum b8abadf4618b09bd3eebb6064fc2df5b90e5ae03
sha224sum 044579d46e0d969a860602216ea4764465e5618ed714109cf782ff50
sha256sum 53f2ff8997625c958f77aef034f9c96d9fcfc8bcb4bb8a96fd8ac89a5ed5adf6
sha384sum b84b6cafa178147614e6b7903e1b7a342e09d95e3101e55c6a3b5b093a22190f2d367c69b1ee12b1ec59726337a40e9c
sha512sum edacca8237d3be5095f392c9d347dce3a5249c79d09f9b99a055b796edd74541b4529c499ff0e4f25e817b702c206073bfe5b0fccae6773680c79ea1e0efa9e2
使用glob:
$ ./hash_sums.py *
>>> 3-4-placement.py
md5sum a81dd2a6eb122176204cacd92d76d08c
sha1sum 4972f8cf08701cdfc6308def05d3ec2eedfcdd9a
sha224sum 32c4dad60f59584ba39ce73c1a1c96d4da36ee6fe3fd291145692b2b
sha256sum 7460ebf8736b0d6e7be8a1025743d0498871c7013cf5ad4463366fd95fe7576a
sha384sum 77de28b4b185d9a5d7d49aef0aad432d37145b914557dc6ecf3e6cdcbd6cd4a1999d717c027489ac99751f066050199b
sha512sum 7bf6a8059601c72e1278e321f225fef82f12a7bad73e1e8c5c43c1fcbdc2243934bf7d1ad07534bdbf10e2dd9ea9265e1debcbdf6603bec24ede665d2f651cf5
>>> SHA256SUMS
md5sum 3c37318d45676c1db2598aa817b37679
sha1sum 8338b8ae5f749551d131dc28aebb80a2b125d651
sha224sum e89ad392f10a77b0940792a03470f3a23df2f2df9c8b6a91a1c496e0
sha256sum dd4a53f7da270f3b6ab691841ca911a231e20af53d7fabc5a39059b9dbdb036d
sha384sum aadfb55856730eb5b7e4192111bfb8fc4c022396a5809cb37fdd8e8b6ac8dbb3b7e462266cde2b34f90d7015fb42fe2b
sha512sum baba0ed86ad781daffb5905512459e353b7ca7da7b04cf67a26cfb320906041da2d4bc73673926aab7c98869f25bc2fd6ea0116c21c40c07188e6dcbbba3dbcf
>>> answer.md
md5sum b6111facdba5978e6cf3a200706ba6fd
sha1sum 5c2eb00c4c0c4799d4457d3a84eea283a3a0d249
sha224sum 7172f877ab579e1ee845c723e0d42ff3acb96859cc43a56aebc39f59
sha256sum dc6ac7365f680e98b9f1279d2e22ed21b9b82c988b99b0facd5e8a98ff50ddd6
sha384sum 5082b3b62d677c5b1b8938f871da16c595be16a540bd76bc9c25ea6572dc9020f236237ec310cfa78fdaa1d0c0a51b0f
sha512sum 81f21316ab6c5a4038cdcde528766e620988a0ccf53bf2e9932399f4534c070468949a5a43ea68629c07e622404574e46bb20cf60c51da4e2a7a44a1df71d920