python3 MD5提取

计算MD5值

import hashlib
h=hashlib.md5()

h.update("hello".encode('utf-8'))

print(h.digest())
print(h.hexdigest())

print(hashlib.__doc__)
print(h.__doc__)

输出:

b']A@*\xbcK*v\xb9q\x9d\x91\x10\x17\xc5\x92'
5d41402abc4b2a76b9719d911017c592
hashlib module - A common interface to many hash functions.

new(name, data=b'', **kwargs) - returns a new hash object implementing the
                                given hash function; initializing the hash
                                using the given binary data.

Named constructor functions are also available, these are faster
than using new(name):

md5(), sha1(), sha224(), sha256(), sha384(), sha512(), blake2b(), blake2s(),
sha3_224, sha3_256, sha3_384, sha3_512, shake_128, and shake_256.

More algorithms may be available on your platform but the above are guaranteed
to exist.  See the algorithms_guaranteed and algorithms_available attributes
to find out what algorithm names can be passed to new().

NOTE: If you want the adler32 or crc32 hash functions they are available in
the zlib module.

Choose your hash function wisely.  Some have known collision weaknesses.
sha384 and sha512 will be slow on 32 bit platforms.

Hash objects have these methods:
 - update(arg): Update the hash object with the bytes in arg. Repeated calls
                are equivalent to a single call with the concatenation of all
                the arguments.
 - digest():    Return the digest of the bytes passed to the update() method
                so far.
 - hexdigest(): Like digest() except the digest is returned as a unicode
                object of double length, containing only hexadecimal digits.
 - copy():      Return a copy (clone) of the hash object. This can be used to
                efficiently compute the digests of strings that share a common
                initial substring.

For example, to obtain the digest of the string 'Nobody inspects the
spammish repetition':

    >>> import hashlib
    >>> m = hashlib.md5()
    >>> m.update(b"Nobody inspects")
    >>> m.update(b" the spammish repetition")
    >>> m.digest()
    b'\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'

More condensed:

    >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
    'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'


A hash represents the object used to calculate a checksum of a
string of information.

Methods:

update() -- updates the current digest with an additional string
digest() -- return the current digest value
hexdigest() -- return the current digest as a string of hexadecimal digits
copy() -- return a copy of the current hash object

Attributes:

name -- the hash algorithm being used by this object
digest_size -- number of bytes in this hashes output
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MD5是message-digest algorithm 5(信息-摘要算法)的缩写,被广泛用于加密和解密技术上,它可以说是文件的“数字指纹”。任何一个文件,无论是可执行程序、图像文件、临时文件或者其他任何类型的文件,也不管它体积多大,都有且只有一个独一无二的MD5信息值,并且如果这个文件被修改过,它的MD5值也将随之改变。因此,我们可以通过对比同一文件的MD5值,来校验这个文件是否被“篡改”过。 MD5到底有什么用 当我们下载了如图1所示之类的文件后,如果想知道下载的这个文件和网站的原始文件是否一模一样,就可以给自己下载的文件做个MD5校验。如果得到的MD5值和网站公布的相同,可确认所下载的文件是完整的。如有不同,说明你下载的文件是不完整的:要么就是在网络下载的过程中出现错误,要么就是此文件已被别人修改。为防止他人更改该文件时放入病毒,最好不要使用。 当我们用E-mail给好友发送文件时,可以将要发送文件的MD5值告诉对方,这样好友收到该文件以后即可对其进行校验,来确定文件是否安全。 再比如:在刚安装好系统后可以给系统文件做个MD5校验,过了一段时间后如果你怀疑某些文件被人换掉,那么就可以给那些被怀疑的文件做个MD5校验,若和从前得到的MD5校验码不一样,那么就可以肯定是有问题的。 如何读取和校验MD5信息 了解了MD5信息以后,下面我们来看一看如何读取并校验文件的MD5信息。这需要一款检测MD5值的专门小软件,这是一款绿色软件,解压缩后运行其中的MD5.EXE文件即可。软件的使用非常简单,点击“Open”按钮,选择并打开想要进行校验的文件,稍等片刻后,在MD5一栏中便会显示该文件的MD5值,将该数值同网站公布的数值进行比较即可确定文件是否完整了。点击“Save”按钮可以将读取的MD5保存为一个.MD5文件,用记事本打开该文件,可以将MD5值复制出来。 为了验证文件修改后的MD5值是否发生变化,笔者用一个文本文件进行了测试。如图2所示,第一个文件为进行测试的原始文件,第二个文件为进行修改后的文件(不过只是在打开原始文件的基础上加入了一个空格),第三个文件为原始文件的复制文件。从图中可以看出,尽管改动不大,但是两个文件的MD5值却大相径庭,而复制得到的文件则不会发生变化
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值