/Doc/library/md5.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 96 lines · 58 code · 38 blank · 0 comment · 0 complexity · ea93b30cfffa26eb4e6bf2b750dd3aba MD5 · raw file

  1. :mod:`md5` --- MD5 message digest algorithm
  2. ===========================================
  3. .. module:: md5
  4. :synopsis: RSA's MD5 message digest algorithm.
  5. :deprecated:
  6. .. deprecated:: 2.5
  7. Use the :mod:`hashlib` module instead.
  8. .. index::
  9. single: message digest, MD5
  10. single: checksum; MD5
  11. This module implements the interface to RSA's MD5 message digest algorithm (see
  12. also Internet :rfc:`1321`). Its use is quite straightforward: use :func:`new`
  13. to create an md5 object. You can now feed this object with arbitrary strings
  14. using the :meth:`update` method, and at any point you can ask it for the
  15. :dfn:`digest` (a strong kind of 128-bit checksum, a.k.a. "fingerprint") of the
  16. concatenation of the strings fed to it so far using the :meth:`digest` method.
  17. For example, to obtain the digest of the string ``'Nobody inspects the spammish
  18. repetition'``:
  19. >>> import md5
  20. >>> m = md5.new()
  21. >>> m.update("Nobody inspects")
  22. >>> m.update(" the spammish repetition")
  23. >>> m.digest()
  24. '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
  25. More condensed:
  26. >>> md5.new("Nobody inspects the spammish repetition").digest()
  27. '\xbbd\x9c\x83\xdd\x1e\xa5\xc9\xd9\xde\xc9\xa1\x8d\xf0\xff\xe9'
  28. The following values are provided as constants in the module and as attributes
  29. of the md5 objects returned by :func:`new`:
  30. .. data:: digest_size
  31. The size of the resulting digest in bytes. This is always ``16``.
  32. The md5 module provides the following functions:
  33. .. function:: new([arg])
  34. Return a new md5 object. If *arg* is present, the method call ``update(arg)``
  35. is made.
  36. .. function:: md5([arg])
  37. For backward compatibility reasons, this is an alternative name for the
  38. :func:`new` function.
  39. An md5 object has the following methods:
  40. .. method:: md5.update(arg)
  41. Update the md5 object with the string *arg*. Repeated calls are equivalent to a
  42. single call with the concatenation of all the arguments: ``m.update(a);
  43. m.update(b)`` is equivalent to ``m.update(a+b)``.
  44. .. method:: md5.digest()
  45. Return the digest of the strings passed to the :meth:`update` method so far.
  46. This is a 16-byte string which may contain non-ASCII characters, including null
  47. bytes.
  48. .. method:: md5.hexdigest()
  49. Like :meth:`digest` except the digest is returned as a string of length 32,
  50. containing only hexadecimal digits. This may be used to exchange the value
  51. safely in email or other non-binary environments.
  52. .. method:: md5.copy()
  53. Return a copy ("clone") of the md5 object. This can be used to efficiently
  54. compute the digests of strings that share a common initial substring.
  55. .. seealso::
  56. Module :mod:`sha`
  57. Similar module implementing the Secure Hash Algorithm (SHA). The SHA algorithm
  58. is considered a more secure hash.