- How do I check the SHA1 hash of a file?
- 6 Answers 6
- Python
- Test run:
- How to Use the md5sum Command in Linux
- The md5sum Command with Examples
- Read in Binary Mode
- Read in Text Mode
- Create a BSD-Style Checksum
- Validate md5 Checksum with a File
- Validate Multiple Files
- Display Only Modified Files
- Generate Status Only
- Check Improperly Formatted Checksum Lines
- Skip Reporting Status for Missing Files
- Show Help and Version Information
How do I check the SHA1 hash of a file?
adding this note to whom it may concern: SHA-1 is now compromised, proven by a Google-CWI joint research. TL;DR Don’t use it anywhere carrying any value.
Just to add, while it is compromised, meaning it is not to be used for anything that needs to be cryptographically secure, it’s still a good way to checksum things if security is not a factor
6 Answers 6
Print or check SHA1 (160-bit) checksums. With no FILE, or when FILE is -, read standard input.
If you want to send the file together with its sha1sum output redirect the output to a file:
Send both files and the other party can do a.
It should show OK if the sha1 is correct.
Great! But how do you run sha1sum -c when
or shasum — the default SHA is (if I am correct) SHA1. Also you set it with -a, —algorithm option: shasum -a 1
@PiotrFindeisen — The output of sha1sum is
Without creating local file:
$ sha1sum filename 8dd10000eb1b768800000e1d2fe1c3100005d2dc *filename
For checking, go to the directory that contains filename and run this command:
echo "8dd10000eb1b768800000e1d2fe1c3100005d2dc *filename" | sha1sum -c -
Thanks, though I don’t think you should have the * in there. Here’s a concrete example: echo ‘b78bb50bdac5ec8c108f34104f788e214ac23635 raspbian.zip’ | sha1sum -c — That will check a file name raspbian.zip in the current directory.
Just in case anyone gets here and is on a mac, there are two spaces between the filename and the output of shasum as of OSX 10.13.3. When I used just one, I got a incorrect formatting error for shasum -c
Navigate to the terminal and key in:
The check argument generates the sha1 hash of filename and compares it with the value stored in filename.sha1 . If it matches OK is displayed and the exit code for the command is 0
@AlexanderMills confirming the sha1 hash tells you that the file you downloaded matches with what the server says it’s offering. So it confirms that you indeed received what you wanted to, instead of a malicious person sending you the wrong file.
For those who are on mac and don’t have coreutils/sha1sum installed.
$ openssl sha1 `mktemp` SHA1(/tmp/tmp.jkyfOWma3t)= da39a3ee5e6b4b0d3255bfef95601890afd80709
this is askubunutu, so being on a Mac would be off-topic 😉 but this should work on ubuntu too, so +1
What are you people talking about? Yes, I get the concept behind sha1sum, but the info above is confusing to say the best. First, Ubuntu does not seem to have sha1sum files — just strings on a web page such as this for 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
To check the integrity of a downloaded .iso, one opens the terminal program, does «cd Downloads», then sha1sum . After a bit, the terminal will produce a hash such as
efcbbc70b10173cea203df30396d0848ba8fa0d8 ubuntu-mate-16.04-beta1-desktop-i386.iso
Next, we have to go to the web page and compare the strings to verify that the verification works. Not nearly as easy as it could be.
Python
Python has excellent hashlib library, that allows calculating multiple hashsums, including sha1 . Here’s a simple script that can do the job:
#!/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Вычисление хеш суммы linux = 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()
Test run:
$ ./hash_sums.py /etc/passwd >>> /etc/passwd md5sum ce5f247e016ba2bb92049fc86158376a sha1sum b8abadf4618b09bd3eebb6064fc2df5b90e5ae03 sha224sum 044579d46e0d969a860602216ea4764465e5618ed714109cf782ff50 sha256sum 53f2ff8997625c958f77aef034f9c96d9fcfc8bcb4bb8a96fd8ac89a5ed5adf6 sha384sum b84b6cafa178147614e6b7903e1b7a342e09d95e3101e55c6a3b5b093a22190f2d367c69b1ee12b1ec59726337a40e9c sha512sum edacca8237d3be5095f392c9d347dce3a5249c79d09f9b99a055b796edd74541b4529c499ff0e4f25e817b702c206073bfe5b0fccae6773680c79ea1e0efa9e2
$ ./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
How to Use the md5sum Command in Linux
When you download a file from the internet, it is a good safety practice to check whether you received the original version. Comparing checksums you received from the file creator with the ones you obtain by checking the file yourself is a reliable way to confirm your download’s integrity.
The md5sum command in Linux helps create, read, and check file checksums.
In this tutorial, you will learn how to use the md5sum command to validate the files you receive.
The md5sum Command with Examples
When used on a file without any options, the md5sum command displays the file’s hash value alongside the filename. The syntax is:
After obtaining the hash value, compare it with the MD5 value provided by the file creator.
Note: While md5sum is a reliable method for testing whether the file you received has been compromised, it is useful only if you know that the website you downloaded it from is secure. If hackers gain access to the website, they can change both the file and its checksum, making it appear as if the file you are downloading is safe.
Read in Binary Mode
To read the file in binary mode, use the -b option ( —binary ):
The * character before the file name means that md5sum read it in binary mode.
Read in Text Mode
Use the -t option ( —text ) to read the file in text mode:
Text mode is the default mode for reading files with md5sum .
Create a BSD-Style Checksum
Using the —tag option outputs the hash value in the BSD-style format:
Validate md5 Checksum with a File
To check a file by comparing its hash value with the value provided in a hash file, use the -c option.
1. As an example, create a hash file containing the md5sum output:
md5sum [filename] > [file-containing-hashes]
2. Use the following syntax to compare the hash value from the file you created against the current hash value of the .txt file:
md5sum -c [file-containing-hashes]
3. If you change the contents of the file and repeat the check, a warning message is displayed:
Validate Multiple Files
Use the same md5sum -c procedure to check the integrity of multiple files:
md5sum [filename1] [filename2] [filename3] > [file-containing-hashes]
In the following example, the contents of example2.txt have changed, resulting in a warning message from md5sum :
Display Only Modified Files
The —quiet option displays only the files whose hash value has changed. It skips the output of validated files.
md5sum --quiet -c [file-containing-hashes]
Generate Status Only
The md5sum command with the —status option does not produce any output but returns 0 if there are no changes and 1 if it detects changes. This argument is useful for scripting, where there is no need for standard output.
The example script below illustrates the use of the —status option:
#!/bin/bash md5sum --status -c hashfile Status=$? echo "File check status is: $Status" exit $Status
When the script executes, it shows status 1 , meaning that md5sum detected the change made earlier in example2.txt .
Check Improperly Formatted Checksum Lines
Add the —strict option to exit non-zero for improperly formatted hash values:
md5sum --strict -c [file-containing-hashes]
The example shows the output of md5sum —strict when you put invalid characters in the first line of the file containing hashes:
To display which line has an invalid hash, use -w ( —warn ):
md5sum -w -c [file-containing-hashes]
The example above shows the -w option displaying that the improperly formatted MD5 checksum line is line 1 of the file.
Skip Reporting Status for Missing Files
By default, md5sum shows warnings about the files it cannot find on the system. To override this behavior, use the —ignore-missing option:
md5sum --ignore-missing -c [file-containing-hashes]
In the example below, example1.txt was deleted before running the md5sum command. The output ignores the deleted file:
Show Help and Version Information
To get the official help for the md5sum command, type:
To check md5sum version, type:
Note: You should also check out our overview of the diff command to learn how to compare two files line by line.
After completing this tutorial, you should know how to properly use the md5sum command to create, print, or check MD5 checksums.
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
The echo command prints out a text string you provide as the output message. This tutorial covers the echo.
The ls command (short for list) lists information about directories and any type of files in the working.
A list of all the important Linux commands in one place. Find the command you need, whenever you need it or.
Creating a file in Linux might seem straightforward, but there are some surprising and clever techniques. In.