下载和复制文件等操作可能会损毁文件,有时候文件也会被恶意篡改,为了保证文件的完整性,可以对其进行校验来以防万一。Hash又叫散列函数,是把任意长度的输入(又叫做预映射, pre-image),通过散列算法变换成固定长度的输出,该输出就是散列值。从原理上可以看出,校验是一个类似于映射的过程,理论上可以有无数个文件拥有一个相同的值,但是考虑到实际应用,这种巧合还是极其微小的。经常用有md5、sha1和sha256三个算法。

Windows系统

Windows系统其实是自带了一个叫做powershell的东西,不管是名字还是功能,powershell都很类似于Unix-like上的shell。作为一个操作系统,通过powershell校验hash值是必须有的功能。
直接在电脑上搜索powershell点击运行既可进入一个蓝色背景的操作界面,下面以检验Windows自带的ie浏览器为例。
MD5算法

Get-FileHash "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -Algorithm MD5| Format-List

md5
SHA1算法

Get-FileHash "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -Algorithm SHA1| Format-List

sha1
SH256算法

Get-FileHash "C:\Program Files (x86)\Internet Explorer\iexplore.exe" | Format-List

sha256
除此之外,也可以直接在注册表中写入这个功能。新建一个txt,将以下的命令复制进去,再将后缀改为reg,双击确认导入即可。

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\文件哈希校验]
"SubCommands"="MACTripleDES;MD5;RIPEMD160;SHA1;SHA256;SHA384;SHA512"
"MUIVerb"="文件哈希校验"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\MACTripleDES]
@="MACTripleDES"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\MACTripleDES\command]
@="PowerShell Get-FileHash -Algorithm MACTripleDES \\\"%1\\\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\MD5]
@="MD5"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\MD5\command]
@="PowerShell Get-FileHash -Algorithm MD5 \\\"%1\\\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\RIPEMD160]
@="RIPEMD160"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\RIPEMD160\command]
@="PowerShell Get-FileHash -Algorithm RIPEMD160 \\\"%1\\\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA1]
@="SHA1"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA1\command]
@="PowerShell Get-FileHash -Algorithm SHA1 \\\"%1\\\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA256]
@="SHA256"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA256\command]
@="PowerShell Get-FileHash -Algorithm SHA256 \\\"%1\\\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA384]
@="SHA384"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA384\command]
@="PowerShell Get-FileHash -Algorithm SHA384 \\\"%1\\\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA512]
@="SHA512"
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\CommandStore\shell\SHA512\command]
@="PowerShell Get-FileHash -Algorithm SHA512 \\\"%1\\\" | format-list;“按任意键退出...”;[Console]::Readkey() | Out-Null;exit"

效果如图:
reg

Linux系统

再Linux系统下的校验则相对简单很多,以home文件夹中的hello.py文件为例。

md5sum /home/hello.py  //计算md5值
sha1sum /home/hello.py  //计算sha1值
sha256sum /home/hello.py  //计算sha256值

linxu

标签: 教程, linux, windows

添加新评论