# 14.1. `hashlib` — 安全哈希与消息摘要

2.5 新版功能.

源码： Lib/hashlib.py

This module implements a common interface to many different secure hash and message digest algorithms. Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA’s MD5 algorithm (defined in Internet RFC 1321). The terms secure hash and message digest are interchangeable. Older algorithms were called message digests. The modern term is secure hash.

注解

如果你想找到 adler32 或 crc32 哈希函数，它们在 `zlib` 模块中。

警告

有些算法已知存在哈希碰撞弱点，请参考最后的“另请参阅”段。

There is one constructor method named for each type of hash. All return a hash object with the same simple interface. For example: use `sha1()` to create a SHA1 hash object. You can now feed this object with arbitrary strings using the `update()` method. At any point you can ask it for the digest of the concatenation of the strings fed to it so far using the `digest()` or `hexdigest()` methods.

Constructors for hash algorithms that are always present in this module are `md5()`, `sha1()`, `sha224()`, `sha256()`, `sha384()`, and `sha512()`. Additional algorithms may also be available depending upon the OpenSSL library that Python uses on your platform.

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

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

更简要的写法：

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

A generic `new()` constructor that takes the string name of the desired algorithm as its first parameter also exists to allow access to the above listed hashes as well as any other algorithms that your OpenSSL library may offer. The named constructors are much faster than `new()` and should be preferred.

使用 `new()` 并附带由 OpenSSL 所提供了算法:

    >>> h = hashlib.new('ripemd160')
    >>> h.update("Nobody inspects the spammish repetition")
    >>> h.hexdigest()
    'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'

This module provides the following constant attribute:

`hashlib.``algorithms`

  A tuple providing the names of the hash algorithms guaranteed to be supported by this module.

  2.7 新版功能.

`hashlib.``algorithms_guaranteed`

  A set containing the names of the hash algorithms guaranteed to be supported by this module on all platforms.

  2.7.9 新版功能.

`hashlib.``algorithms_available`

  一个集合，其中包含在所运行的 Python 解释器上可用的哈希算法的名称。 将这些名称传给 `new()` 时将可被识别。 `algorithms_guaranteed` 将总是它的一个子集。 同样的算法在此集合中可能以不同的名称出现多次（这是 OpenSSL 的原因）。

  2.7.9 新版功能.

下列值会以构造器所返回的哈希对象的常量属性的形式被提供:

`hash.``digest_size`

  以字节表示的结果哈希对象的大小。

`hash.``block_size`

  以字节表示的哈希算法的内部块大小。

哈希对象具有下列方法:

`hash.``update`(arg)

  Update the hash object with the string arg. Repeated calls are equivalent to a single call with the concatenation of all the arguments: `m.update(a); m.update(b)` is equivalent to `m.update(a+b)`.

  在 2.7 版更改: The Python GIL is released to allow other threads to run while hash updates on data larger than 2048 bytes is taking place when using hash algorithms supplied by OpenSSL.

`hash.``digest`()

  Return the digest of the strings passed to the `update()` method so far. This is a string of `digest_size` bytes which may contain non-ASCII characters, including null bytes.

`hash.``hexdigest`()

  Like `digest()` except the digest is returned as a string of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.

`hash.``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.

## 14.1.1. 密钥派生

密钥派生和密钥延展算法被设计用于安全密码哈希。 `sha1(password)` 这样的简单算法无法防御暴力攻击。 好的密码哈希函数必须可以微调、放慢步调，并且包含 加盐。

`hashlib.``pbkdf2_hmac`(name, password, salt, rounds, dklen=None)

  此函数提供 PKCS#5 基于密码的密钥派生函数 2。 它使用 HMAC 作为伪随机函数。

  The string name is the desired name of the hash digest algorithm for HMAC, e.g. ‘sha1’ or ‘sha256’. password and salt are interpreted as buffers of bytes. Applications and libraries should limit password to a sensible value (e.g. 1024). salt should be about 16 or more bytes from a proper source, e.g. `os.urandom()`.

  The number of rounds should be chosen based on the hash algorithm and computing power. As of 2013, at least 100,000 rounds of SHA-256 is suggested.

  dklen is the length of the derived key. If dklen is `None` then the digest size of the hash algorithm name is used, e.g. 64 for SHA-512.

      >>> import hashlib, binascii
      >>> dk = hashlib.pbkdf2_hmac('sha256', b'password', b'salt', 100000)
      >>> binascii.hexlify(dk)
      b'0394a2ede332c9a13eb82e9b24631604c31df978b4e2f0fbd2c549944f9d79a5'

  2.7.8 新版功能.

  注解

  随同 OpenSSL 提供了一个快速的 pbkdf2_hmac 实现。 Python 实现是使用 `hmac` 的内联版本。 它的速度大约要慢上三倍并且不会释放 GIL。

参见

模块 `hmac`

  A module to generate message authentication codes using hashes.

模块 `base64`

  Another way to encode binary hashes for non-binary environments.

http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf

  The FIPS 180-2 publication on Secure Hash Algorithms.

https://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms

  Wikipedia article with information on which algorithms have known issues and what that means regarding their use.