加密:使用md5哈希校验和验证文件完整性

阅读:157

Mukesh Chapagain 2016-06-06 00:00:00    Mukesh Chapagain笔记


校验和用于验证文件的数据完整性。它可以用于在设备之间或通过互联网传输数据时检测错误。校验和用作文件的数字指纹。诸如md5sumsha256sum的计算机程序计算和验证校验和。md5sum计算和验证128位MD5哈希和sha1sum计算和验证SHA-1哈希。

验证本地存储的文件

假设,您已经计算了您的文件的校验和和存储在另一个文件中的所有文件的校验和。在一段时间/天后,您需要检查这些文件是否被篡改/修改或损坏,然后您可以重新计算文件的校验和,并检查它是否与先前存储的校验和匹配。如果匹配,那么,您的数据不会更改或损坏。否则,您的文件已损坏。您可以验证每个单独文件的校验和。

验证下载的文件

有时,数据可能会在下载过程中丢失。在这种情况下,您可以在校验和的帮助下验证您下载的文件的完整性。为此,您需要先在您的服务器上存储文件的校验和。您可以从服务器下载包含所有其他文件的校验和的文件。然后,您可以计算下载文件的校验和,并验证它是否与先前存储的校验和匹配。如果匹配,那么,您的数据不会更改或损坏。否则,您的文件已损坏。您可以验证每个单独文件的校验和。

工作示例

在本文中,我将演示如何计算文件的校验和,将校验和存储在一个新文件中,然后使用md5sum验证文件的数据完整性/真实性我使用Ubuntu Linux作为我的操作系统。

1.创建文件和文件夹

我去我的主文件夹,并在那里创建一个名为“crypto”的新目录。所以,crypto文件夹将是我的工作目录为这篇文章的例子。


1

2

cd ~

mkdir crypto


我创建两个目录命名为“folder1”和“folder2”和5个文件。folder1和folder2每个都有一个文件。


1

2

mkdir folder1 folder2

touch file1.txt file2.txt file3.txt folder1/file4.txt folder2/file5.txt


2.打印所有文件

只需检查我们的工作目录中的所有文件和文件夹。

打印所有文件


1

find . -type f -print


输出:

./file1.txt 
./file2.txt 
./file3.txt 
./folder1/file4.txt 
./folder2/file5.txt

打印所有目录


1

find . -type d -print


输出:


./folder1 
./folder2

3.计算所有文件的md5哈希值并将其存储在名为“md5”的新文件中


1

md5sum * > md5


4.打印新文件“md5”的内容

这个文件有我们工作文件夹中的所有文件的md5哈希。


1

cat md5


输出:

d41d8cd98f00b204e9800998ecf8427e file1.txt 
d41d8cd98f00b204e9800998ecf8427e file2.txt 
d41d8cd98f00b204e9800998ecf8427e file3.txt

5.做出改变

让我们更新现有的文件之一。我们向file1.txt添加“一些文本”。


1

echo "some text" > file1.txt


6.验证md5哈希

现在,我们检查并验证我们工作目录中所有文件的完整性。


1

md5sum -c md5


输出:

file1.txt:FAILED 
file2.txt:OK 
file3.txt:OK 
md5sum:WARNING:1计算的校验和不匹配

我们可以看到它显示一个文件(file1.txt)被更改,因为我们已经编辑了该文件。

希望这可以帮助。谢谢。

分享这个:


Cryptography: Verify file integrity with md5 hash checksum

阅读:157

Mukesh Chapagain 2016-06-06 00:00:00


Checksum is used to verify the data integrity of files. It can be used to detect errors while transmitting data between devices or over the internet. The checksum functions as a digital fingerprint of a file. Computer programs like md5sum and sha256sum calculate and verify checksum. md5sum calculates and verifies 128-bit MD5 hashes and sha1sum calculates and verifies SHA-1 hashes.

Verifying locally stored files

Suppose, you had computed checksum of your files and stored checksum of all the files in another file. After some time/days, you need to check if those files are tampered / modified, or corrupted then you can recompute the checksum of your files and check if it matches with the previously stored checksum. If it matches then, your data are not altered or corrupted. Otherwise, your files are corrupted. You can verify the checksum of each individual file.

Verifying downloaded files

Sometime, data might get lost during download. In such case, you can verify the integrity of your downloaded files with the help of checksum. For this, you need to have previously stored checksum of your files on your server. You can download that file containing checksum of all the other files from your server. Then, you can compute the checksum of your downloaded files and verify if it matches with the previoulsy stored checksum. If it matches then, your data are not altered or corrupted. Otherwise, your files are corrupted. You can verify the checksum of each individual file.

Working Example

In this article, I will show how to compute checksum of files, store the checksum in a new file and then verify the data integrity / authenticity of the files using md5sum. I am using Ubuntu Linux as my operating system.

1. Create files and folders

I go to my home folder and create a new directory over there named ‘crypto’. So, crypto folder will be my working directory for this article example.


1

2

cd ~

mkdir crypto


I create two directories named ‘folder1’ and ‘folder2’ and 5 files. folder1 and folder2 have one file each inside them.


1

2

mkdir folder1 folder2

touch file1.txt file2.txt file3.txt folder1/file4.txt folder2/file5.txt


2. Print all files

Just checking all the files and folders present inside our working directory.

Print all files


1

find . -type f -print


output:

./file1.txt
./file2.txt
./file3.txt
./folder1/file4.txt
./folder2/file5.txt

Print all directories


1

find . -type d -print


output:

.
./folder1
./folder2

3. Compute md5 hash of all files and store it in a new file named ‘md5’


1

md5sum * > md5


4. Print content of the new file ‘md5’

This file has md5 hash of all the files present inside our working folder.


1

cat md5


output:

d41d8cd98f00b204e9800998ecf8427e file1.txt
d41d8cd98f00b204e9800998ecf8427e file2.txt
d41d8cd98f00b204e9800998ecf8427e file3.txt

5. Making changes

Let’s update one of the exisiting files. We add “some text” to file1.txt.


1

echo "some text" > file1.txt


6. Verify md5 hash

Now, we check and verify the integrity of all the files in our working directory.


1

md5sum -c md5


output:

file1.txt: FAILED
file2.txt: OK
file3.txt: OK
md5sum: WARNING: 1 computed checksum did NOT match

We can see that it shows one file (file1.txt) as altered because we had edited the file.

Hope this helps. Thanks.

Share this: