2007-08-15 11:28:22 -03:00
|
|
|
:mod:`hashlib` --- Secure hashes and message digests
|
|
|
|
====================================================
|
|
|
|
|
|
|
|
.. module:: hashlib
|
|
|
|
:synopsis: Secure hash and message digest algorithms.
|
2016-06-11 16:02:54 -03:00
|
|
|
|
Merged revisions 68450,68480-68481,68493,68495,68501,68512,68514-68515,68534-68536,68552,68563,68570-68572,68575,68582,68596,68623-68624,68628 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r68450 | jeffrey.yasskin | 2009-01-09 10:47:07 -0600 (Fri, 09 Jan 2009) | 3 lines
Fix issue 4884, preventing a crash in the socket code when python is compiled
with llvm-gcc and run with a glibc <2.10.
........
r68480 | vinay.sajip | 2009-01-10 07:38:04 -0600 (Sat, 10 Jan 2009) | 1 line
Minor documentation changes cross-referencing NullHandler to the documentation on configuring logging in a library.
........
r68481 | vinay.sajip | 2009-01-10 07:42:04 -0600 (Sat, 10 Jan 2009) | 1 line
Corrected an incorrect self-reference.
........
r68493 | benjamin.peterson | 2009-01-10 11:18:55 -0600 (Sat, 10 Jan 2009) | 1 line
rewrite verbose conditionals
........
r68495 | benjamin.peterson | 2009-01-10 11:36:44 -0600 (Sat, 10 Jan 2009) | 1 line
tp_iter only exists with Py_TPFLAGS_HAVE_ITER #4901
........
r68501 | vinay.sajip | 2009-01-10 13:22:57 -0600 (Sat, 10 Jan 2009) | 1 line
Corrected minor typo and added .currentmodule directives to fix missing cross-references.
........
r68512 | benjamin.peterson | 2009-01-10 16:42:10 -0600 (Sat, 10 Jan 2009) | 1 line
make tests fail if they can't be imported
........
r68514 | benjamin.peterson | 2009-01-10 17:41:59 -0600 (Sat, 10 Jan 2009) | 1 line
move seealso to a more appropiate place
........
r68515 | benjamin.peterson | 2009-01-10 17:49:08 -0600 (Sat, 10 Jan 2009) | 1 line
macos 9 isn't supported
........
r68534 | gregory.p.smith | 2009-01-11 11:53:33 -0600 (Sun, 11 Jan 2009) | 2 lines
correct email address
........
r68535 | gregory.p.smith | 2009-01-11 11:57:54 -0600 (Sun, 11 Jan 2009) | 9 lines
Update the documentation for binascii and zlib crc32/adler32 functions
to better describe the signed vs unsigned return value behavior on
different platforms and versions of python. Mention the workaround to
make them all return the same thing by using & 0xffffffff.
Fixes issue4903.
Also needs to be merged into release26-maint, release30-maint, & py3k.
........
r68536 | benjamin.peterson | 2009-01-11 13:48:15 -0600 (Sun, 11 Jan 2009) | 1 line
add email addresses
........
r68552 | vinay.sajip | 2009-01-12 14:36:18 -0600 (Mon, 12 Jan 2009) | 1 line
Minor changes/corrections in markup.
........
r68563 | benjamin.peterson | 2009-01-12 19:49:10 -0600 (Mon, 12 Jan 2009) | 1 line
small logic correction
........
r68570 | raymond.hettinger | 2009-01-13 03:08:32 -0600 (Tue, 13 Jan 2009) | 5 lines
Issue 4922: Incorrect comments for MutableSet.add() and MutableSet.discard().
Needs to be backported to 2.6 and forward ported to 3.0 and 3.1.
........
r68571 | armin.ronacher | 2009-01-13 05:52:23 -0600 (Tue, 13 Jan 2009) | 3 lines
ast.literal_eval can properly evaluate complex numbers now. This fixes issue4907.
........
r68572 | andrew.kuchling | 2009-01-13 07:40:54 -0600 (Tue, 13 Jan 2009) | 1 line
Note that first coord. is left alone
........
r68575 | thomas.heller | 2009-01-13 11:32:28 -0600 (Tue, 13 Jan 2009) | 1 line
Fix refcount leak in error cases. Bug found by coverity.
........
r68582 | georg.brandl | 2009-01-13 16:14:01 -0600 (Tue, 13 Jan 2009) | 2 lines
Use assertRaises.
........
r68596 | amaury.forgeotdarc | 2009-01-13 17:39:22 -0600 (Tue, 13 Jan 2009) | 3 lines
#1162154: inspect.getmembers() now skips attributes that raise AttributeError,
e.g. a __slots__ attribute which has not been set.
........
r68623 | vinay.sajip | 2009-01-15 16:48:13 -0600 (Thu, 15 Jan 2009) | 1 line
Made minor changes/corrections in markup. Added a couple of section headings.
........
r68624 | vinay.sajip | 2009-01-15 17:04:47 -0600 (Thu, 15 Jan 2009) | 1 line
Minor changes/corrections in markup.
........
r68628 | benjamin.peterson | 2009-01-15 20:55:24 -0600 (Thu, 15 Jan 2009) | 1 line
compare with == not is #4946
........
2009-01-15 23:54:08 -04:00
|
|
|
.. moduleauthor:: Gregory P. Smith <greg@krypto.org>
|
|
|
|
.. sectionauthor:: Gregory P. Smith <greg@krypto.org>
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
**Source code:** :source:`Lib/hashlib.py`
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: message digest, MD5
|
|
|
|
single: secure hash algorithm, SHA1, SHA224, SHA256, SHA384, SHA512
|
|
|
|
|
2016-08-10 03:05:19 -03:00
|
|
|
.. testsetup::
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
|
2011-01-27 16:38:46 -04:00
|
|
|
--------------
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
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
|
2007-09-06 11:09:10 -03:00
|
|
|
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.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61538 | steven.bethard | 2008-03-18 20:03:50 +0100 (Di, 18 Mär 2008) | 1 line
cell_compare needs to return -2 instead of NULL.
........
r61539 | steven.bethard | 2008-03-18 20:04:32 +0100 (Di, 18 Mär 2008) | 1 line
_have_soundcard() is a bad check for winsound.Beep, since you can have a soundcard but have the beep driver disabled. This revision basically disables the beep tests by wrapping them in a try/except. The Right Way To Do It is to come up with a _have_enabled_beep_driver() and use that.
........
r61540 | gregory.p.smith | 2008-03-18 20:05:32 +0100 (Di, 18 Mär 2008) | 8 lines
Fix chown on 64-bit linux. It needed to take a long (64-bit on 64bit linux) as
uid and gid input to accept values >=2**31 as valid while still accepting
negative numbers to pass -1 to chown for "no change".
Fixes issue1747858.
This should be backported to release25-maint.
........
r61556 | steven.bethard | 2008-03-18 20:59:14 +0100 (Di, 18 Mär 2008) | 1 line
Fix test_atexit so that it still passes when -3 is supplied. (It was catching the warning messages on stdio from using the reload() function.)
........
r61559 | neal.norwitz | 2008-03-18 21:30:38 +0100 (Di, 18 Mär 2008) | 1 line
Import the test properly. This is especially important for py3k.
........
r61560 | gregory.p.smith | 2008-03-18 21:40:01 +0100 (Di, 18 Mär 2008) | 2 lines
news entry for the chown fix
........
r61563 | brett.cannon | 2008-03-18 22:12:42 +0100 (Di, 18 Mär 2008) | 2 lines
Ignore BIG5HKSCS-2004.TXT which is downloaded as part of a test.
........
r61565 | steven.bethard | 2008-03-18 22:30:13 +0100 (Di, 18 Mär 2008) | 1 line
Have regrtest skip test_py3kwarn when the -3 flag is missing.
........
r61571 | gregory.p.smith | 2008-03-18 23:27:41 +0100 (Di, 18 Mär 2008) | 4 lines
Add a test to make sure zlib.crc32 and binascii.crc32 return the same thing.
Fix a buglet in binascii.crc32, the second optional argument could previously
have a signedness mismatch with the C variable its going into.
........
r61575 | raymond.hettinger | 2008-03-19 00:22:29 +0100 (Mi, 19 Mär 2008) | 1 line
Speed-up isinstance() for one easy case.
........
r61576 | raymond.hettinger | 2008-03-19 00:33:08 +0100 (Mi, 19 Mär 2008) | 1 line
Issue: 2354: Add 3K warning for the cmp argument to list.sort() and sorted().
........
r61580 | andrew.kuchling | 2008-03-19 02:05:35 +0100 (Mi, 19 Mär 2008) | 1 line
Add Jeff Rush
........
r61581 | gregory.p.smith | 2008-03-19 02:38:35 +0100 (Mi, 19 Mär 2008) | 3 lines
Mention that crc32 and adler32 are available in a different module (zlib).
Some people look for them in hashlib.
........
r61582 | gregory.p.smith | 2008-03-19 02:46:10 +0100 (Mi, 19 Mär 2008) | 3 lines
Use zlib's crc32 routine instead of binascii when available. zlib's is faster
when compiled properly optimized and about the same speed otherwise.
........
r61586 | david.wolever | 2008-03-19 03:26:57 +0100 (Mi, 19 Mär 2008) | 1 line
Added my name to ACKS
........
r61591 | gregory.p.smith | 2008-03-19 04:14:41 +0100 (Mi, 19 Mär 2008) | 5 lines
Fix the struct module DeprecationWarnings that zipfile was triggering by
removing all use of signed struct values.
test_zipfile and test_zipfile64 pass. no more warnings.
........
r61593 | raymond.hettinger | 2008-03-19 04:56:59 +0100 (Mi, 19 Mär 2008) | 1 line
Fix compiler warning.
........
r61595 | martin.v.loewis | 2008-03-19 05:39:13 +0100 (Mi, 19 Mär 2008) | 2 lines
Issue #2400: Allow relative imports to "import *".
........
r61605 | martin.v.loewis | 2008-03-19 07:00:28 +0100 (Mi, 19 Mär 2008) | 2 lines
Import relimport using a relative import.
........
r61606 | trent.nelson | 2008-03-19 07:28:24 +0100 (Mi, 19 Mär 2008) | 1 line
Issue2290: Support x64 Windows builds that live in pcbuild/amd64. Without it, sysutils._python_build() returns the wrong directory, which causes the test_get_config_h_filename method in Lib/distutils/tests/test_sysconfig.py to fail.
........
r61613 | trent.nelson | 2008-03-19 08:45:19 +0100 (Mi, 19 Mär 2008) | 3 lines
Refine the Visual Studio 2008 build solution in order to improve how we deal with external components, as well as fixing outstanding issues with Windows x64 build support. Introduce two new .vcproj files, _bsddb44.vcproj and sqlite3.vcproj, which replace the previous pre-link event scripts for _bsddb and _sqlite3 respectively. The new project files inherit from our property files as if they were any other Python module. This has numerous benefits. First, the components get built with exactly the same compiler flags and settings as the rest of Python. Second, it makes it much easier to debug problems in the external components when they're part of the build system. Third, they'll benefit from profile guided optimisation in the release builds, just like the rest of Python core.
I've also introduced a slightly new pattern for managing externals in subversion. New components get checked in as <name>-<version>.x, where <version> matches the exact vendor version string. After the initial import of the external component, the .x is tagged as .0 (i.e. tcl-8.4.18.x -> tcl-8.4.18.0). Some components may not need any tweaking, whereas there are others that might (tcl/tk fall into this bucket). In that case, the relevant modifications are made to the .x branch, which will be subsequently tagged as .1 (and then n+1 going forward) when they build successfully and all tests pass. Buildbots will be converted to rely on these explicit tags only, which makes it easy for us to switch them over to a new version as and when required. (Simple change to external(-amd64).bat: if we've bumped tcl to 8.4.18.1, change the .bat to rmdir 8.4.18.0 if it exists and check out a new .1 copy.)
........
r61614 | trent.nelson | 2008-03-19 08:56:39 +0100 (Mi, 19 Mär 2008) | 1 line
Remove extraneous apostrophe and semi-colon from AdditionalIncludeDirectories.
........
r61615 | georg.brandl | 2008-03-19 08:56:40 +0100 (Mi, 19 Mär 2008) | 2 lines
Remove footnote from versionchanged as it upsets LaTeX.
........
r61616 | georg.brandl | 2008-03-19 08:57:57 +0100 (Mi, 19 Mär 2008) | 2 lines
Another one.
........
r61618 | trent.nelson | 2008-03-19 09:06:03 +0100 (Mi, 19 Mär 2008) | 1 line
Fix the tcl-8.4.18.1 path and make sure we cd into the right directory when building tcl/tk.
........
r61621 | trent.nelson | 2008-03-19 10:23:08 +0100 (Mi, 19 Mär 2008) | 1 line
Lets have another try at getting the Windows buildbots in a consistent state before rebuilding using the new process.
........
r61622 | eric.smith | 2008-03-19 13:09:55 +0100 (Mi, 19 Mär 2008) | 2 lines
Use test.test_support.captured_stdout instead of a custom contextmanager.
Thanks Nick Coghlan.
........
r61623 | eric.smith | 2008-03-19 13:15:10 +0100 (Mi, 19 Mär 2008) | 1 line
Trivial typo.
........
r61625 | thomas.heller | 2008-03-19 17:10:57 +0100 (Mi, 19 Mär 2008) | 2 lines
Checkout sqlite-source when it is not there.
........
r61627 | brett.cannon | 2008-03-19 17:50:13 +0100 (Mi, 19 Mär 2008) | 5 lines
test_nis would fail if test.test_support.verbose was true but NIS was not set
up on the machine.
Closes issue2411. Thanks Michael Bishop.
........
r61631 | brett.cannon | 2008-03-19 18:37:43 +0100 (Mi, 19 Mär 2008) | 2 lines
Use sys.py3kwarning instead of trying to trigger a Py3k-related warning.
........
r61632 | raymond.hettinger | 2008-03-19 18:45:19 +0100 (Mi, 19 Mär 2008) | 1 line
Issue 2354: Fix-up compare warning. Patch contributed by Jeff Balogh.
........
r61633 | raymond.hettinger | 2008-03-19 18:58:59 +0100 (Mi, 19 Mär 2008) | 1 line
The filter() function does support a None argument in Py3.0.
........
r61634 | raymond.hettinger | 2008-03-19 19:01:58 +0100 (Mi, 19 Mär 2008) | 1 line
Remove itertools warnings I had added before the 2-to-3 handled the migration.
........
2008-03-19 18:50:51 -03:00
|
|
|
.. note::
|
2013-10-06 13:26:36 -03:00
|
|
|
|
|
|
|
If you want the adler32 or crc32 hash functions, they are available in
|
Merged revisions 61538-61540,61556,61559-61560,61563,61565,61571,61575-61576,61580-61582,61586,61591,61593,61595,61605-61606,61613-61616,61618,61621-61623,61625,61627,61631-61634 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r61538 | steven.bethard | 2008-03-18 20:03:50 +0100 (Di, 18 Mär 2008) | 1 line
cell_compare needs to return -2 instead of NULL.
........
r61539 | steven.bethard | 2008-03-18 20:04:32 +0100 (Di, 18 Mär 2008) | 1 line
_have_soundcard() is a bad check for winsound.Beep, since you can have a soundcard but have the beep driver disabled. This revision basically disables the beep tests by wrapping them in a try/except. The Right Way To Do It is to come up with a _have_enabled_beep_driver() and use that.
........
r61540 | gregory.p.smith | 2008-03-18 20:05:32 +0100 (Di, 18 Mär 2008) | 8 lines
Fix chown on 64-bit linux. It needed to take a long (64-bit on 64bit linux) as
uid and gid input to accept values >=2**31 as valid while still accepting
negative numbers to pass -1 to chown for "no change".
Fixes issue1747858.
This should be backported to release25-maint.
........
r61556 | steven.bethard | 2008-03-18 20:59:14 +0100 (Di, 18 Mär 2008) | 1 line
Fix test_atexit so that it still passes when -3 is supplied. (It was catching the warning messages on stdio from using the reload() function.)
........
r61559 | neal.norwitz | 2008-03-18 21:30:38 +0100 (Di, 18 Mär 2008) | 1 line
Import the test properly. This is especially important for py3k.
........
r61560 | gregory.p.smith | 2008-03-18 21:40:01 +0100 (Di, 18 Mär 2008) | 2 lines
news entry for the chown fix
........
r61563 | brett.cannon | 2008-03-18 22:12:42 +0100 (Di, 18 Mär 2008) | 2 lines
Ignore BIG5HKSCS-2004.TXT which is downloaded as part of a test.
........
r61565 | steven.bethard | 2008-03-18 22:30:13 +0100 (Di, 18 Mär 2008) | 1 line
Have regrtest skip test_py3kwarn when the -3 flag is missing.
........
r61571 | gregory.p.smith | 2008-03-18 23:27:41 +0100 (Di, 18 Mär 2008) | 4 lines
Add a test to make sure zlib.crc32 and binascii.crc32 return the same thing.
Fix a buglet in binascii.crc32, the second optional argument could previously
have a signedness mismatch with the C variable its going into.
........
r61575 | raymond.hettinger | 2008-03-19 00:22:29 +0100 (Mi, 19 Mär 2008) | 1 line
Speed-up isinstance() for one easy case.
........
r61576 | raymond.hettinger | 2008-03-19 00:33:08 +0100 (Mi, 19 Mär 2008) | 1 line
Issue: 2354: Add 3K warning for the cmp argument to list.sort() and sorted().
........
r61580 | andrew.kuchling | 2008-03-19 02:05:35 +0100 (Mi, 19 Mär 2008) | 1 line
Add Jeff Rush
........
r61581 | gregory.p.smith | 2008-03-19 02:38:35 +0100 (Mi, 19 Mär 2008) | 3 lines
Mention that crc32 and adler32 are available in a different module (zlib).
Some people look for them in hashlib.
........
r61582 | gregory.p.smith | 2008-03-19 02:46:10 +0100 (Mi, 19 Mär 2008) | 3 lines
Use zlib's crc32 routine instead of binascii when available. zlib's is faster
when compiled properly optimized and about the same speed otherwise.
........
r61586 | david.wolever | 2008-03-19 03:26:57 +0100 (Mi, 19 Mär 2008) | 1 line
Added my name to ACKS
........
r61591 | gregory.p.smith | 2008-03-19 04:14:41 +0100 (Mi, 19 Mär 2008) | 5 lines
Fix the struct module DeprecationWarnings that zipfile was triggering by
removing all use of signed struct values.
test_zipfile and test_zipfile64 pass. no more warnings.
........
r61593 | raymond.hettinger | 2008-03-19 04:56:59 +0100 (Mi, 19 Mär 2008) | 1 line
Fix compiler warning.
........
r61595 | martin.v.loewis | 2008-03-19 05:39:13 +0100 (Mi, 19 Mär 2008) | 2 lines
Issue #2400: Allow relative imports to "import *".
........
r61605 | martin.v.loewis | 2008-03-19 07:00:28 +0100 (Mi, 19 Mär 2008) | 2 lines
Import relimport using a relative import.
........
r61606 | trent.nelson | 2008-03-19 07:28:24 +0100 (Mi, 19 Mär 2008) | 1 line
Issue2290: Support x64 Windows builds that live in pcbuild/amd64. Without it, sysutils._python_build() returns the wrong directory, which causes the test_get_config_h_filename method in Lib/distutils/tests/test_sysconfig.py to fail.
........
r61613 | trent.nelson | 2008-03-19 08:45:19 +0100 (Mi, 19 Mär 2008) | 3 lines
Refine the Visual Studio 2008 build solution in order to improve how we deal with external components, as well as fixing outstanding issues with Windows x64 build support. Introduce two new .vcproj files, _bsddb44.vcproj and sqlite3.vcproj, which replace the previous pre-link event scripts for _bsddb and _sqlite3 respectively. The new project files inherit from our property files as if they were any other Python module. This has numerous benefits. First, the components get built with exactly the same compiler flags and settings as the rest of Python. Second, it makes it much easier to debug problems in the external components when they're part of the build system. Third, they'll benefit from profile guided optimisation in the release builds, just like the rest of Python core.
I've also introduced a slightly new pattern for managing externals in subversion. New components get checked in as <name>-<version>.x, where <version> matches the exact vendor version string. After the initial import of the external component, the .x is tagged as .0 (i.e. tcl-8.4.18.x -> tcl-8.4.18.0). Some components may not need any tweaking, whereas there are others that might (tcl/tk fall into this bucket). In that case, the relevant modifications are made to the .x branch, which will be subsequently tagged as .1 (and then n+1 going forward) when they build successfully and all tests pass. Buildbots will be converted to rely on these explicit tags only, which makes it easy for us to switch them over to a new version as and when required. (Simple change to external(-amd64).bat: if we've bumped tcl to 8.4.18.1, change the .bat to rmdir 8.4.18.0 if it exists and check out a new .1 copy.)
........
r61614 | trent.nelson | 2008-03-19 08:56:39 +0100 (Mi, 19 Mär 2008) | 1 line
Remove extraneous apostrophe and semi-colon from AdditionalIncludeDirectories.
........
r61615 | georg.brandl | 2008-03-19 08:56:40 +0100 (Mi, 19 Mär 2008) | 2 lines
Remove footnote from versionchanged as it upsets LaTeX.
........
r61616 | georg.brandl | 2008-03-19 08:57:57 +0100 (Mi, 19 Mär 2008) | 2 lines
Another one.
........
r61618 | trent.nelson | 2008-03-19 09:06:03 +0100 (Mi, 19 Mär 2008) | 1 line
Fix the tcl-8.4.18.1 path and make sure we cd into the right directory when building tcl/tk.
........
r61621 | trent.nelson | 2008-03-19 10:23:08 +0100 (Mi, 19 Mär 2008) | 1 line
Lets have another try at getting the Windows buildbots in a consistent state before rebuilding using the new process.
........
r61622 | eric.smith | 2008-03-19 13:09:55 +0100 (Mi, 19 Mär 2008) | 2 lines
Use test.test_support.captured_stdout instead of a custom contextmanager.
Thanks Nick Coghlan.
........
r61623 | eric.smith | 2008-03-19 13:15:10 +0100 (Mi, 19 Mär 2008) | 1 line
Trivial typo.
........
r61625 | thomas.heller | 2008-03-19 17:10:57 +0100 (Mi, 19 Mär 2008) | 2 lines
Checkout sqlite-source when it is not there.
........
r61627 | brett.cannon | 2008-03-19 17:50:13 +0100 (Mi, 19 Mär 2008) | 5 lines
test_nis would fail if test.test_support.verbose was true but NIS was not set
up on the machine.
Closes issue2411. Thanks Michael Bishop.
........
r61631 | brett.cannon | 2008-03-19 18:37:43 +0100 (Mi, 19 Mär 2008) | 2 lines
Use sys.py3kwarning instead of trying to trigger a Py3k-related warning.
........
r61632 | raymond.hettinger | 2008-03-19 18:45:19 +0100 (Mi, 19 Mär 2008) | 1 line
Issue 2354: Fix-up compare warning. Patch contributed by Jeff Balogh.
........
r61633 | raymond.hettinger | 2008-03-19 18:58:59 +0100 (Mi, 19 Mär 2008) | 1 line
The filter() function does support a None argument in Py3.0.
........
r61634 | raymond.hettinger | 2008-03-19 19:01:58 +0100 (Mi, 19 Mär 2008) | 1 line
Remove itertools warnings I had added before the 2-to-3 handled the migration.
........
2008-03-19 18:50:51 -03:00
|
|
|
the :mod:`zlib` module.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. warning::
|
|
|
|
|
2013-10-06 13:26:36 -03:00
|
|
|
Some algorithms have known hash collision weaknesses, refer to the "See
|
|
|
|
also" section at the end.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-10-12 19:52:43 -03:00
|
|
|
|
2013-12-20 17:33:52 -04:00
|
|
|
.. _hash-algorithms:
|
|
|
|
|
2013-10-12 19:52:43 -03:00
|
|
|
Hash algorithms
|
|
|
|
---------------
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
There is one constructor method named for each type of :dfn:`hash`. All return
|
2016-06-11 21:56:12 -03:00
|
|
|
a hash object with the same simple interface. For example: use :func:`sha256` to
|
|
|
|
create a SHA-256 hash object. You can now feed this object with :term:`bytes-like
|
2016-05-18 07:54:54 -03:00
|
|
|
objects <bytes-like object>` (normally :class:`bytes`) using the :meth:`update` method.
|
2013-05-04 12:06:34 -03:00
|
|
|
At any point you can ask it for the :dfn:`digest` of the
|
2007-09-06 11:09:10 -03:00
|
|
|
concatenation of the data fed to it so far using the :meth:`digest` or
|
|
|
|
:meth:`hexdigest` methods.
|
|
|
|
|
2009-01-08 17:17:16 -04:00
|
|
|
.. note::
|
|
|
|
|
2012-12-20 22:24:37 -04:00
|
|
|
For better multithreading performance, the Python :term:`GIL` is released for
|
2013-10-03 23:20:37 -03:00
|
|
|
data larger than 2047 bytes at object creation or on update.
|
2009-01-08 17:17:16 -04:00
|
|
|
|
2007-09-06 11:09:10 -03:00
|
|
|
.. note::
|
|
|
|
|
2012-12-20 22:22:47 -04:00
|
|
|
Feeding string objects into :meth:`update` is not supported, as hashes work
|
2007-09-06 11:09:10 -03:00
|
|
|
on bytes, not on characters.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 58095-58132,58136-58148,58151-58197 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r58096 | brett.cannon | 2007-09-10 23:38:27 +0200 (Mon, 10 Sep 2007) | 4 lines
Fix a possible segfault from recursing too deep to get the repr of a list.
Closes issue #1096.
........
r58097 | bill.janssen | 2007-09-10 23:51:02 +0200 (Mon, 10 Sep 2007) | 33 lines
More work on SSL support.
* Much expanded test suite:
All protocols tested against all other protocols.
All protocols tested with all certificate options.
Tests for bad key and bad cert.
Test of STARTTLS functionality.
Test of RAND_* functions.
* Fixes for threading/malloc bug.
* Issue 1065 fixed:
sslsocket class renamed to SSLSocket.
sslerror class renamed to SSLError.
Function "wrap_socket" now used to wrap an existing socket.
* Issue 1583946 finally fixed:
Support for subjectAltName added.
Subject name now returned as proper DN list of RDNs.
* SSLError exported from socket as "sslerror".
* RAND_* functions properly exported from ssl.py.
* Documentation improved:
Example of how to create a self-signed certificate.
Better indexing.
........
r58098 | guido.van.rossum | 2007-09-11 00:02:25 +0200 (Tue, 11 Sep 2007) | 9 lines
Patch # 1140 (my code, approved by Effbot).
Make sure the type of the return value of re.sub(x, y, z) is the type
of y+x (i.e. unicode if either is unicode, str if they are both str)
even if there are no substitutions or if x==z (which triggered various
special cases in join_list()).
Could be backported to 2.5; no need to port to 3.0.
........
r58099 | guido.van.rossum | 2007-09-11 00:36:02 +0200 (Tue, 11 Sep 2007) | 8 lines
Patch # 1026 by Benjamin Aranguren (with Alex Martelli):
Backport abc.py and isinstance/issubclass overloading to 2.6.
I had to backport test_typechecks.py myself, and make one small change
to abc.py to avoid duplicate work when x.__class__ and type(x) are the
same.
........
r58100 | bill.janssen | 2007-09-11 01:41:24 +0200 (Tue, 11 Sep 2007) | 3 lines
A better way of finding an open port to test with.
........
r58101 | bill.janssen | 2007-09-11 03:09:19 +0200 (Tue, 11 Sep 2007) | 4 lines
Make sure test_ssl doesn't reference the ssl module in a
context where it can't be imported.
........
r58102 | bill.janssen | 2007-09-11 04:42:07 +0200 (Tue, 11 Sep 2007) | 3 lines
Fix some documentation bugs.
........
r58103 | nick.coghlan | 2007-09-11 16:01:18 +0200 (Tue, 11 Sep 2007) | 1 line
Always use the -E flag when spawning subprocesses in test_cmd_line (Issue 1056)
........
r58106 | thomas.heller | 2007-09-11 21:17:48 +0200 (Tue, 11 Sep 2007) | 3 lines
Disable some tests that fail on the 'ppc Debian unstable' buildbot to
find out if they cause the segfault on the 'alpha Debian' machine.
........
r58108 | brett.cannon | 2007-09-11 23:02:28 +0200 (Tue, 11 Sep 2007) | 6 lines
Generators had their throw() method allowing string exceptions. That's a
no-no.
Fixes issue #1147. Need to fix 2.5 to raise a proper warning if a string
exception is passed in.
........
r58112 | georg.brandl | 2007-09-12 20:03:51 +0200 (Wed, 12 Sep 2007) | 3 lines
New documentation page for the bdb module.
(This doesn't need to be merged to Py3k.)
........
r58114 | georg.brandl | 2007-09-12 20:05:57 +0200 (Wed, 12 Sep 2007) | 2 lines
Bug #1152: use non-deprecated name in example.
........
r58115 | georg.brandl | 2007-09-12 20:08:33 +0200 (Wed, 12 Sep 2007) | 2 lines
Fix #1122: wrong return type documented for various _Size() functions.
........
r58117 | georg.brandl | 2007-09-12 20:10:56 +0200 (Wed, 12 Sep 2007) | 2 lines
Fix #1139: PyFile_Encoding really is PyFile_SetEncoding.
........
r58119 | georg.brandl | 2007-09-12 20:29:18 +0200 (Wed, 12 Sep 2007) | 2 lines
bug #1154: release memory allocated by "es" PyArg_ParseTuple format specifier.
........
r58121 | bill.janssen | 2007-09-12 20:52:05 +0200 (Wed, 12 Sep 2007) | 1 line
root certificate for https://svn.python.org/, used in test_ssl
........
r58122 | georg.brandl | 2007-09-12 21:00:07 +0200 (Wed, 12 Sep 2007) | 3 lines
Bug #1153: repr.repr() now doesn't require set and dictionary items
to be orderable to properly represent them.
........
r58125 | georg.brandl | 2007-09-12 21:29:28 +0200 (Wed, 12 Sep 2007) | 4 lines
#1120: put explicit version in the shebang lines of pydoc, idle
and smtpd.py scripts that are installed by setup.py. That way, they
work when only "make altinstall" is used.
........
r58139 | mark.summerfield | 2007-09-13 16:54:30 +0200 (Thu, 13 Sep 2007) | 9 lines
Replaced variable o with obj in operator.rst because o is easy to
confuse.
Added a note about Python 3's collections.Mapping etc., above section
that describes isMappingType() etc.
Added xrefs between os, os.path, fileinput, and open().
........
r58143 | facundo.batista | 2007-09-13 20:13:15 +0200 (Thu, 13 Sep 2007) | 7 lines
Merged the decimal-branch (revisions 54886 to 58140). Decimal is now
fully updated to the latests Decimal Specification (v1.66) and the
latests test cases (v2.56).
Thanks to Mark Dickinson for all his help during this process.
........
r58145 | facundo.batista | 2007-09-13 20:42:09 +0200 (Thu, 13 Sep 2007) | 7 lines
Put the parameter watchexp back in (changed watchexp from an int
to a bool). Also second argument to watchexp is now converted
to Decimal, just as with all the other two-argument operations.
Thanks Mark Dickinson.
........
r58147 | andrew.kuchling | 2007-09-14 00:49:34 +0200 (Fri, 14 Sep 2007) | 1 line
Add various items
........
r58148 | andrew.kuchling | 2007-09-14 00:50:10 +0200 (Fri, 14 Sep 2007) | 1 line
Make target unique
........
r58154 | facundo.batista | 2007-09-14 20:58:34 +0200 (Fri, 14 Sep 2007) | 3 lines
Included the new functions, and new descriptions.
........
r58155 | thomas.heller | 2007-09-14 21:40:35 +0200 (Fri, 14 Sep 2007) | 2 lines
ctypes.util.find_library uses dump(1) instead of objdump(1) on Solaris.
Fixes issue #1777530; will backport to release25-maint.
........
r58159 | facundo.batista | 2007-09-14 23:29:52 +0200 (Fri, 14 Sep 2007) | 3 lines
Some additions (examples and a bit on the tutorial).
........
r58160 | georg.brandl | 2007-09-15 18:53:36 +0200 (Sat, 15 Sep 2007) | 2 lines
Remove bdb from the "undocumented modules" list.
........
r58164 | bill.janssen | 2007-09-17 00:06:00 +0200 (Mon, 17 Sep 2007) | 15 lines
Add support for asyncore server-side SSL support. This requires
adding the 'makefile' method to ssl.SSLSocket, and importing the
requisite fakefile class from socket.py, and making the appropriate
changes to it to make it use the SSL connection.
Added sample HTTPS server to test_ssl.py, and test that uses it.
Change SSL tests to use https://svn.python.org/, instead of
www.sf.net and pop.gmail.com.
Added utility function to ssl module, get_server_certificate,
to wrap up the several things to be done to pull a certificate
from a remote server.
........
r58173 | bill.janssen | 2007-09-17 01:16:46 +0200 (Mon, 17 Sep 2007) | 1 line
use binary mode when reading files for testAsyncore to make Windows happy
........
r58175 | raymond.hettinger | 2007-09-17 02:55:00 +0200 (Mon, 17 Sep 2007) | 7 lines
Sync-up named tuples with the latest version of the ASPN recipe.
Allows optional commas in the field-name spec (help when named tuples are used in conjuction with sql queries).
Adds the __fields__ attribute for introspection and to support conversion to dictionary form.
Adds a __replace__() method similar to str.replace() but using a named field as a target.
Clean-up spelling and presentation in doc-strings.
........
r58176 | brett.cannon | 2007-09-17 05:28:34 +0200 (Mon, 17 Sep 2007) | 5 lines
Add a bunch of GIL release/acquire points in tp_print implementations and for
PyObject_Print().
Closes issue #1164.
........
r58177 | sean.reifschneider | 2007-09-17 07:45:04 +0200 (Mon, 17 Sep 2007) | 2 lines
issue1597011: Fix for bz2 module corner-case error due to error checking bug.
........
r58180 | facundo.batista | 2007-09-17 18:26:50 +0200 (Mon, 17 Sep 2007) | 3 lines
Decimal is updated, :)
........
r58181 | facundo.batista | 2007-09-17 19:30:13 +0200 (Mon, 17 Sep 2007) | 5 lines
The methods always return Decimal classes, even if they're
executed through a subclass (thanks Mark Dickinson).
Added a bit of testing for this.
........
r58183 | sean.reifschneider | 2007-09-17 22:53:21 +0200 (Mon, 17 Sep 2007) | 2 lines
issue1082: Fixing platform and system for Vista.
........
r58185 | andrew.kuchling | 2007-09-18 03:36:16 +0200 (Tue, 18 Sep 2007) | 1 line
Add item; sort properly
........
r58186 | raymond.hettinger | 2007-09-18 05:33:19 +0200 (Tue, 18 Sep 2007) | 1 line
Handle corner cased on 0-tuples and 1-tuples. Add verbose option so people can see how it works.
........
r58192 | georg.brandl | 2007-09-18 09:24:40 +0200 (Tue, 18 Sep 2007) | 2 lines
A bit of reordering, also show more subheadings in the lang ref index.
........
r58193 | facundo.batista | 2007-09-18 18:53:18 +0200 (Tue, 18 Sep 2007) | 4 lines
Speed up of the various division operations (remainder, divide,
divideint and divmod). Thanks Mark Dickinson.
........
r58197 | raymond.hettinger | 2007-09-19 00:18:02 +0200 (Wed, 19 Sep 2007) | 1 line
Cleanup docs for NamedTuple.
........
2007-09-19 00:06:30 -03:00
|
|
|
.. index:: single: OpenSSL; (use in module hashlib)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
Constructors for hash algorithms that are always present in this module are
|
2016-06-11 21:56:12 -03:00
|
|
|
:func:`sha1`, :func:`sha224`, :func:`sha256`, :func:`sha384`,
|
2016-09-06 17:03:25 -03:00
|
|
|
:func:`sha512`, :func:`blake2b`, and :func:`blake2s`.
|
|
|
|
:func:`md5` is normally available as well, though it
|
2019-09-12 21:30:00 -03:00
|
|
|
may be missing or blocked if you are using a rare "FIPS compliant" build of Python.
|
2016-06-11 21:56:12 -03:00
|
|
|
Additional algorithms may also be available depending upon the OpenSSL
|
2016-09-07 06:58:24 -03:00
|
|
|
library that Python uses on your platform. On most platforms the
|
|
|
|
:func:`sha3_224`, :func:`sha3_256`, :func:`sha3_384`, :func:`sha3_512`,
|
|
|
|
:func:`shake_128`, :func:`shake_256` are also available.
|
|
|
|
|
|
|
|
.. versionadded:: 3.6
|
|
|
|
SHA3 (Keccak) and SHAKE constructors :func:`sha3_224`, :func:`sha3_256`,
|
|
|
|
:func:`sha3_384`, :func:`sha3_512`, :func:`shake_128`, :func:`shake_256`.
|
2012-10-05 21:23:36 -03:00
|
|
|
|
2016-09-06 17:03:25 -03:00
|
|
|
.. versionadded:: 3.6
|
|
|
|
:func:`blake2b` and :func:`blake2s` were added.
|
|
|
|
|
2019-09-12 21:30:00 -03:00
|
|
|
.. versionchanged:: 3.9
|
|
|
|
All hashlib constructors take a keyword-only argument *usedforsecurity*
|
2019-11-12 10:57:03 -04:00
|
|
|
with default value ``True``. A false value allows the use of insecure and
|
|
|
|
blocked hashing algorithms in restricted environments. ``False`` indicates
|
2019-09-12 21:30:00 -03:00
|
|
|
that the hashing algorithm is not used in a security context, e.g. as a
|
|
|
|
non-cryptographic one-way compression function.
|
|
|
|
|
2020-05-16 17:27:06 -03:00
|
|
|
Hashlib now uses SHA3 and SHAKE from OpenSSL 1.1.1 and newer.
|
|
|
|
|
2007-09-06 11:09:10 -03:00
|
|
|
For example, to obtain the digest of the byte string ``b'Nobody inspects the
|
|
|
|
spammish repetition'``::
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
>>> import hashlib
|
2016-06-11 21:56:12 -03:00
|
|
|
>>> m = hashlib.sha256()
|
2007-09-06 11:09:10 -03:00
|
|
|
>>> m.update(b"Nobody inspects")
|
|
|
|
>>> m.update(b" the spammish repetition")
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> m.digest()
|
2016-06-11 21:56:12 -03:00
|
|
|
b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06'
|
2007-11-06 16:51:31 -04:00
|
|
|
>>> m.digest_size
|
2016-06-11 21:56:12 -03:00
|
|
|
32
|
2007-11-06 16:51:31 -04:00
|
|
|
>>> m.block_size
|
|
|
|
64
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
More condensed:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2007-09-06 11:09:10 -03:00
|
|
|
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
|
2008-12-29 16:52:09 -04:00
|
|
|
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2019-09-12 21:30:00 -03:00
|
|
|
.. function:: new(name[, data], *, usedforsecurity=True)
|
2010-09-06 05:30:23 -03:00
|
|
|
|
2018-07-31 03:50:16 -03:00
|
|
|
Is a generic constructor that takes the string *name* of the desired
|
2010-09-06 05:30:23 -03:00
|
|
|
algorithm as its first parameter. It 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 :func:`new`
|
|
|
|
and should be preferred.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
Merged revisions 61724-61725,61731-61735,61737,61739,61741,61743-61744,61753,61761,61765-61767,61769,61773,61776-61778,61780-61783,61788,61793,61796,61807,61813 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
................
r61724 | martin.v.loewis | 2008-03-22 01:01:12 +0100 (Sat, 22 Mar 2008) | 49 lines
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 M?\195?\164r 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 M?\195?\164r 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 M?\195?\164r 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 M?\195?\164r 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 M?\195?\164r 2008) | 2 lines
Fix whitespace.
........
................
r61725 | martin.v.loewis | 2008-03-22 01:02:41 +0100 (Sat, 22 Mar 2008) | 2 lines
Install lib2to3.
................
r61731 | facundo.batista | 2008-03-22 03:45:37 +0100 (Sat, 22 Mar 2008) | 4 lines
Small fix that complicated the test actually when that
test failed.
................
r61732 | alexandre.vassalotti | 2008-03-22 05:08:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Added warning for the removal of 'hotshot' in Py3k.
................
r61733 | georg.brandl | 2008-03-22 11:07:29 +0100 (Sat, 22 Mar 2008) | 4 lines
#1918: document that weak references *to* an object are
cleared before the object's __del__ is called, to ensure that the weak
reference callback (if any) finds the object healthy.
................
r61734 | georg.brandl | 2008-03-22 11:56:23 +0100 (Sat, 22 Mar 2008) | 2 lines
Activate the Sphinx doctest extension and convert howto/functional to use it.
................
r61735 | georg.brandl | 2008-03-22 11:58:38 +0100 (Sat, 22 Mar 2008) | 2 lines
Allow giving source names on the cmdline.
................
r61737 | georg.brandl | 2008-03-22 12:00:48 +0100 (Sat, 22 Mar 2008) | 2 lines
Fixup this HOWTO's doctest blocks so that they can be run with sphinx' doctest builder.
................
r61739 | georg.brandl | 2008-03-22 12:47:10 +0100 (Sat, 22 Mar 2008) | 2 lines
Test decimal.rst doctests as far as possible with sphinx doctest.
................
r61741 | georg.brandl | 2008-03-22 13:04:26 +0100 (Sat, 22 Mar 2008) | 2 lines
Make doctests in re docs usable with sphinx' doctest.
................
r61743 | georg.brandl | 2008-03-22 13:59:37 +0100 (Sat, 22 Mar 2008) | 2 lines
Make more doctests in pprint docs testable.
................
r61744 | georg.brandl | 2008-03-22 14:07:06 +0100 (Sat, 22 Mar 2008) | 2 lines
No need to specify explicit "doctest_block" anymore.
................
r61753 | georg.brandl | 2008-03-22 21:08:43 +0100 (Sat, 22 Mar 2008) | 2 lines
Fix-up syntax problems.
................
r61761 | georg.brandl | 2008-03-22 22:06:20 +0100 (Sat, 22 Mar 2008) | 4 lines
Make collections' doctests executable.
(The <BLANKLINE>s will be stripped from presentation output.)
................
r61765 | georg.brandl | 2008-03-22 22:21:57 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in datetime docs.
................
r61766 | georg.brandl | 2008-03-22 22:26:44 +0100 (Sat, 22 Mar 2008) | 2 lines
Test doctests in operator docs.
................
r61767 | georg.brandl | 2008-03-22 22:38:33 +0100 (Sat, 22 Mar 2008) | 2 lines
Enable doctests in functions.rst. Already found two errors :)
................
r61769 | georg.brandl | 2008-03-22 23:04:10 +0100 (Sat, 22 Mar 2008) | 3 lines
Enable doctest running for several other documents.
We have now over 640 doctests that are run with "make doctest".
................
r61773 | raymond.hettinger | 2008-03-23 01:55:46 +0100 (Sun, 23 Mar 2008) | 1 line
Simplify demo code.
................
r61776 | neal.norwitz | 2008-03-23 04:43:33 +0100 (Sun, 23 Mar 2008) | 7 lines
Try to make this test a little more robust and not fail with:
timeout (10.0025) is more than 2 seconds more than expected (0.001)
I'm assuming this problem is caused by DNS lookup. This change
does a DNS lookup of the hostname before trying to connect, so the time
is not included.
................
r61777 | neal.norwitz | 2008-03-23 05:08:30 +0100 (Sun, 23 Mar 2008) | 1 line
Speed up the test by avoiding socket timeouts.
................
r61778 | neal.norwitz | 2008-03-23 05:43:09 +0100 (Sun, 23 Mar 2008) | 1 line
Skip the epoll test if epoll() does not work
................
r61780 | neal.norwitz | 2008-03-23 06:47:20 +0100 (Sun, 23 Mar 2008) | 1 line
Suppress failure (to avoid a flaky test) if we cannot connect to svn.python.org
................
r61781 | neal.norwitz | 2008-03-23 07:13:25 +0100 (Sun, 23 Mar 2008) | 4 lines
Move itertools before future_builtins since the latter depends on the former.
From a clean build importing future_builtins would fail since itertools
wasn't built yet.
................
r61782 | neal.norwitz | 2008-03-23 07:16:04 +0100 (Sun, 23 Mar 2008) | 1 line
Try to prevent the alarm going off early in tearDown
................
r61783 | neal.norwitz | 2008-03-23 07:19:57 +0100 (Sun, 23 Mar 2008) | 4 lines
Remove compiler warnings (on Alpha at least) about using chars as
array subscripts. Using chars are dangerous b/c they are signed
on some platforms and unsigned on others.
................
r61788 | georg.brandl | 2008-03-23 09:05:30 +0100 (Sun, 23 Mar 2008) | 2 lines
Make the doctests presentation-friendlier.
................
r61793 | amaury.forgeotdarc | 2008-03-23 10:55:29 +0100 (Sun, 23 Mar 2008) | 4 lines
#1477: ur'\U0010FFFF' raised in narrow unicode builds.
Corrected the raw-unicode-escape codec to use UTF-16 surrogates in
this case, just like the unicode-escape codec.
................
r61796 | raymond.hettinger | 2008-03-23 14:32:32 +0100 (Sun, 23 Mar 2008) | 1 line
Issue 1681432: Add triangular distribution the random module.
................
r61807 | raymond.hettinger | 2008-03-23 20:37:53 +0100 (Sun, 23 Mar 2008) | 4 lines
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
................
r61813 | gregory.p.smith | 2008-03-23 22:04:43 +0100 (Sun, 23 Mar 2008) | 6 lines
Fix gzip to deal with CRC's being signed values in Python 2.x properly and to
read 32bit values as unsigned to start with rather than applying signedness
fixups allover the place afterwards.
This hopefully fixes the test_tarfile failure on the alpha/tru64 buildbot.
................
2008-03-23 18:54:12 -03:00
|
|
|
Using :func:`new` with an algorithm provided by OpenSSL:
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
>>> h = hashlib.new('ripemd160')
|
2007-09-06 11:09:10 -03:00
|
|
|
>>> h.update(b"Nobody inspects the spammish repetition")
|
2007-08-15 11:28:22 -03:00
|
|
|
>>> h.hexdigest()
|
2008-12-29 16:52:09 -04:00
|
|
|
'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2010-09-06 05:30:23 -03:00
|
|
|
Hashlib provides the following constant attributes:
|
|
|
|
|
|
|
|
.. data:: algorithms_guaranteed
|
|
|
|
|
2014-03-16 01:13:56 -03:00
|
|
|
A set containing the names of the hash algorithms guaranteed to be supported
|
2016-06-11 22:02:13 -03:00
|
|
|
by this module on all platforms. Note that 'md5' is in this list despite
|
|
|
|
some upstream vendors offering an odd "FIPS compliant" Python build that
|
|
|
|
excludes it.
|
2010-09-06 05:30:23 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
2010-02-28 22:05:26 -04:00
|
|
|
|
2010-09-06 05:30:23 -03:00
|
|
|
.. data:: algorithms_available
|
2010-02-28 22:05:26 -04:00
|
|
|
|
2014-03-16 01:13:56 -03:00
|
|
|
A set containing the names of the hash algorithms that are available in the
|
|
|
|
running Python interpreter. These names will be recognized when passed to
|
|
|
|
:func:`new`. :attr:`algorithms_guaranteed` will always be a subset. The
|
|
|
|
same algorithm may appear multiple times in this set under different names
|
|
|
|
(thanks to OpenSSL).
|
2010-02-28 22:05:26 -04:00
|
|
|
|
|
|
|
.. versionadded:: 3.2
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
The following values are provided as constant attributes of the hash objects
|
|
|
|
returned by the constructors:
|
|
|
|
|
|
|
|
|
Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74779 | michael.foord | 2009-09-13 11:13:36 -0500 (Sun, 13 Sep 2009) | 1 line
Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
r74780 | michael.foord | 2009-09-13 11:40:02 -0500 (Sun, 13 Sep 2009) | 1 line
Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567.
........
r74781 | michael.foord | 2009-09-13 11:46:19 -0500 (Sun, 13 Sep 2009) | 1 line
Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
r74782 | michael.foord | 2009-09-13 12:07:46 -0500 (Sun, 13 Sep 2009) | 1 line
Tutorial tweaks. Issue 6849.
........
r74783 | michael.foord | 2009-09-13 12:28:35 -0500 (Sun, 13 Sep 2009) | 1 line
unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866.
........
r74784 | georg.brandl | 2009-09-13 13:15:07 -0500 (Sun, 13 Sep 2009) | 1 line
Typo fix.
........
r74785 | michael.foord | 2009-09-13 14:07:03 -0500 (Sun, 13 Sep 2009) | 1 line
Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568.
........
r74786 | michael.foord | 2009-09-13 14:08:18 -0500 (Sun, 13 Sep 2009) | 1 line
Remove an extraneous space in unittest documentation.
........
r74793 | georg.brandl | 2009-09-14 09:50:47 -0500 (Mon, 14 Sep 2009) | 1 line
#6908: fix association of hashlib hash attributes.
........
r74795 | benjamin.peterson | 2009-09-14 22:36:26 -0500 (Mon, 14 Sep 2009) | 1 line
Py_SetPythonHome uses static storage #6913
........
r74811 | georg.brandl | 2009-09-15 15:26:59 -0500 (Tue, 15 Sep 2009) | 1 line
Add Armin Ronacher.
........
r74860 | benjamin.peterson | 2009-09-16 21:46:54 -0500 (Wed, 16 Sep 2009) | 1 line
kill bare except
........
r74861 | benjamin.peterson | 2009-09-16 22:18:28 -0500 (Wed, 16 Sep 2009) | 1 line
pep 8 defaults
........
r74863 | benjamin.peterson | 2009-09-16 22:27:33 -0500 (Wed, 16 Sep 2009) | 1 line
rationalize a bit
........
r74876 | georg.brandl | 2009-09-17 11:15:53 -0500 (Thu, 17 Sep 2009) | 1 line
#6932: remove paragraph that advises relying on __del__ being called.
........
r74886 | benjamin.peterson | 2009-09-17 16:33:46 -0500 (Thu, 17 Sep 2009) | 1 line
use macros
........
r74896 | georg.brandl | 2009-09-18 02:22:41 -0500 (Fri, 18 Sep 2009) | 1 line
#6936: for interactive use, quit() is just fine.
........
r74901 | georg.brandl | 2009-09-18 04:14:52 -0500 (Fri, 18 Sep 2009) | 1 line
#6905: use better exception messages in inspect when the argument is of the wrong type.
........
r74903 | georg.brandl | 2009-09-18 04:18:27 -0500 (Fri, 18 Sep 2009) | 1 line
#6938: "ident" is always a string, so use a format code which works.
........
r74908 | georg.brandl | 2009-09-18 08:57:11 -0500 (Fri, 18 Sep 2009) | 1 line
Use str.format() to fix beginner's mistake with %-style string formatting.
........
r74912 | georg.brandl | 2009-09-18 11:19:56 -0500 (Fri, 18 Sep 2009) | 1 line
Optimize optimization and fix method name in docstring.
........
r74930 | georg.brandl | 2009-09-18 16:21:41 -0500 (Fri, 18 Sep 2009) | 1 line
#6925: rewrite docs for locals() and vars() a bit.
........
r74933 | georg.brandl | 2009-09-18 16:35:59 -0500 (Fri, 18 Sep 2009) | 1 line
#6930: clarify description about byteorder handling in UTF decoder routines.
........
r74943 | georg.brandl | 2009-09-19 02:35:07 -0500 (Sat, 19 Sep 2009) | 1 line
#6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple.
........
r74946 | georg.brandl | 2009-09-19 03:43:16 -0500 (Sat, 19 Sep 2009) | 1 line
Update bug tracker reference.
........
r74952 | georg.brandl | 2009-09-19 05:42:34 -0500 (Sat, 19 Sep 2009) | 1 line
#6946: fix duplicate index entries for datetime classes.
........
r74953 | georg.brandl | 2009-09-19 07:04:16 -0500 (Sat, 19 Sep 2009) | 1 line
Fix references to threading.enumerate().
........
r74954 | georg.brandl | 2009-09-19 08:13:56 -0500 (Sat, 19 Sep 2009) | 1 line
Add Doug.
........
r74955 | georg.brandl | 2009-09-19 08:20:49 -0500 (Sat, 19 Sep 2009) | 1 line
Add Mark Summerfield.
........
r75015 | georg.brandl | 2009-09-22 05:55:08 -0500 (Tue, 22 Sep 2009) | 1 line
Fix encoding name.
........
r75019 | vinay.sajip | 2009-09-22 12:23:41 -0500 (Tue, 22 Sep 2009) | 1 line
Fixed a typo, and added sections on optimization and using arbitrary objects as messages.
........
r75032 | benjamin.peterson | 2009-09-22 17:15:28 -0500 (Tue, 22 Sep 2009) | 1 line
fix typos/rephrase
........
r75068 | benjamin.peterson | 2009-09-25 21:57:59 -0500 (Fri, 25 Sep 2009) | 1 line
comment out ugly xxx
........
r75076 | vinay.sajip | 2009-09-26 09:53:32 -0500 (Sat, 26 Sep 2009) | 1 line
Tidied up name of parameter in StreamHandler
........
r75095 | michael.foord | 2009-09-27 14:15:41 -0500 (Sun, 27 Sep 2009) | 1 line
Test creation moved from TestProgram.parseArgs to TestProgram.createTests exclusively. Issue 6956.
........
r75098 | michael.foord | 2009-09-27 15:08:23 -0500 (Sun, 27 Sep 2009) | 1 line
Documentation improvement for load_tests protocol in unittest. Issue 6515.
........
r75102 | skip.montanaro | 2009-09-27 21:12:27 -0500 (Sun, 27 Sep 2009) | 3 lines
Patch from Thomas Barr so that csv.Sniffer will set doublequote property.
Closes issue 6606.
........
r75129 | vinay.sajip | 2009-09-29 02:08:54 -0500 (Tue, 29 Sep 2009) | 1 line
Issue #7014: logging: Improved IronPython 2.6 compatibility.
........
r75139 | raymond.hettinger | 2009-09-29 13:53:24 -0500 (Tue, 29 Sep 2009) | 3 lines
Issue 7008: Better document str.title and show how to work around the apostrophe problem.
........
r75230 | benjamin.peterson | 2009-10-04 08:38:38 -0500 (Sun, 04 Oct 2009) | 1 line
test logging
........
2009-10-04 11:49:41 -03:00
|
|
|
.. data:: hash.digest_size
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2007-11-06 16:51:31 -04:00
|
|
|
The size of the resulting hash in bytes.
|
|
|
|
|
Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74779 | michael.foord | 2009-09-13 11:13:36 -0500 (Sun, 13 Sep 2009) | 1 line
Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
r74780 | michael.foord | 2009-09-13 11:40:02 -0500 (Sun, 13 Sep 2009) | 1 line
Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567.
........
r74781 | michael.foord | 2009-09-13 11:46:19 -0500 (Sun, 13 Sep 2009) | 1 line
Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
r74782 | michael.foord | 2009-09-13 12:07:46 -0500 (Sun, 13 Sep 2009) | 1 line
Tutorial tweaks. Issue 6849.
........
r74783 | michael.foord | 2009-09-13 12:28:35 -0500 (Sun, 13 Sep 2009) | 1 line
unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866.
........
r74784 | georg.brandl | 2009-09-13 13:15:07 -0500 (Sun, 13 Sep 2009) | 1 line
Typo fix.
........
r74785 | michael.foord | 2009-09-13 14:07:03 -0500 (Sun, 13 Sep 2009) | 1 line
Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568.
........
r74786 | michael.foord | 2009-09-13 14:08:18 -0500 (Sun, 13 Sep 2009) | 1 line
Remove an extraneous space in unittest documentation.
........
r74793 | georg.brandl | 2009-09-14 09:50:47 -0500 (Mon, 14 Sep 2009) | 1 line
#6908: fix association of hashlib hash attributes.
........
r74795 | benjamin.peterson | 2009-09-14 22:36:26 -0500 (Mon, 14 Sep 2009) | 1 line
Py_SetPythonHome uses static storage #6913
........
r74811 | georg.brandl | 2009-09-15 15:26:59 -0500 (Tue, 15 Sep 2009) | 1 line
Add Armin Ronacher.
........
r74860 | benjamin.peterson | 2009-09-16 21:46:54 -0500 (Wed, 16 Sep 2009) | 1 line
kill bare except
........
r74861 | benjamin.peterson | 2009-09-16 22:18:28 -0500 (Wed, 16 Sep 2009) | 1 line
pep 8 defaults
........
r74863 | benjamin.peterson | 2009-09-16 22:27:33 -0500 (Wed, 16 Sep 2009) | 1 line
rationalize a bit
........
r74876 | georg.brandl | 2009-09-17 11:15:53 -0500 (Thu, 17 Sep 2009) | 1 line
#6932: remove paragraph that advises relying on __del__ being called.
........
r74886 | benjamin.peterson | 2009-09-17 16:33:46 -0500 (Thu, 17 Sep 2009) | 1 line
use macros
........
r74896 | georg.brandl | 2009-09-18 02:22:41 -0500 (Fri, 18 Sep 2009) | 1 line
#6936: for interactive use, quit() is just fine.
........
r74901 | georg.brandl | 2009-09-18 04:14:52 -0500 (Fri, 18 Sep 2009) | 1 line
#6905: use better exception messages in inspect when the argument is of the wrong type.
........
r74903 | georg.brandl | 2009-09-18 04:18:27 -0500 (Fri, 18 Sep 2009) | 1 line
#6938: "ident" is always a string, so use a format code which works.
........
r74908 | georg.brandl | 2009-09-18 08:57:11 -0500 (Fri, 18 Sep 2009) | 1 line
Use str.format() to fix beginner's mistake with %-style string formatting.
........
r74912 | georg.brandl | 2009-09-18 11:19:56 -0500 (Fri, 18 Sep 2009) | 1 line
Optimize optimization and fix method name in docstring.
........
r74930 | georg.brandl | 2009-09-18 16:21:41 -0500 (Fri, 18 Sep 2009) | 1 line
#6925: rewrite docs for locals() and vars() a bit.
........
r74933 | georg.brandl | 2009-09-18 16:35:59 -0500 (Fri, 18 Sep 2009) | 1 line
#6930: clarify description about byteorder handling in UTF decoder routines.
........
r74943 | georg.brandl | 2009-09-19 02:35:07 -0500 (Sat, 19 Sep 2009) | 1 line
#6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set. Also clean up another usage of PyArg_ParseTuple.
........
r74946 | georg.brandl | 2009-09-19 03:43:16 -0500 (Sat, 19 Sep 2009) | 1 line
Update bug tracker reference.
........
r74952 | georg.brandl | 2009-09-19 05:42:34 -0500 (Sat, 19 Sep 2009) | 1 line
#6946: fix duplicate index entries for datetime classes.
........
r74953 | georg.brandl | 2009-09-19 07:04:16 -0500 (Sat, 19 Sep 2009) | 1 line
Fix references to threading.enumerate().
........
r74954 | georg.brandl | 2009-09-19 08:13:56 -0500 (Sat, 19 Sep 2009) | 1 line
Add Doug.
........
r74955 | georg.brandl | 2009-09-19 08:20:49 -0500 (Sat, 19 Sep 2009) | 1 line
Add Mark Summerfield.
........
r75015 | georg.brandl | 2009-09-22 05:55:08 -0500 (Tue, 22 Sep 2009) | 1 line
Fix encoding name.
........
r75019 | vinay.sajip | 2009-09-22 12:23:41 -0500 (Tue, 22 Sep 2009) | 1 line
Fixed a typo, and added sections on optimization and using arbitrary objects as messages.
........
r75032 | benjamin.peterson | 2009-09-22 17:15:28 -0500 (Tue, 22 Sep 2009) | 1 line
fix typos/rephrase
........
r75068 | benjamin.peterson | 2009-09-25 21:57:59 -0500 (Fri, 25 Sep 2009) | 1 line
comment out ugly xxx
........
r75076 | vinay.sajip | 2009-09-26 09:53:32 -0500 (Sat, 26 Sep 2009) | 1 line
Tidied up name of parameter in StreamHandler
........
r75095 | michael.foord | 2009-09-27 14:15:41 -0500 (Sun, 27 Sep 2009) | 1 line
Test creation moved from TestProgram.parseArgs to TestProgram.createTests exclusively. Issue 6956.
........
r75098 | michael.foord | 2009-09-27 15:08:23 -0500 (Sun, 27 Sep 2009) | 1 line
Documentation improvement for load_tests protocol in unittest. Issue 6515.
........
r75102 | skip.montanaro | 2009-09-27 21:12:27 -0500 (Sun, 27 Sep 2009) | 3 lines
Patch from Thomas Barr so that csv.Sniffer will set doublequote property.
Closes issue 6606.
........
r75129 | vinay.sajip | 2009-09-29 02:08:54 -0500 (Tue, 29 Sep 2009) | 1 line
Issue #7014: logging: Improved IronPython 2.6 compatibility.
........
r75139 | raymond.hettinger | 2009-09-29 13:53:24 -0500 (Tue, 29 Sep 2009) | 3 lines
Issue 7008: Better document str.title and show how to work around the apostrophe problem.
........
r75230 | benjamin.peterson | 2009-10-04 08:38:38 -0500 (Sun, 04 Oct 2009) | 1 line
test logging
........
2009-10-04 11:49:41 -03:00
|
|
|
.. data:: hash.block_size
|
2007-11-06 16:51:31 -04:00
|
|
|
|
|
|
|
The internal block size of the hash algorithm in bytes.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2013-08-03 06:39:39 -03:00
|
|
|
A hash object has the following attributes:
|
|
|
|
|
|
|
|
.. attribute:: hash.name
|
|
|
|
|
|
|
|
The canonical name of this hash, always lowercase and always suitable as a
|
|
|
|
parameter to :func:`new` to create another hash of this type.
|
|
|
|
|
|
|
|
.. versionchanged:: 3.4
|
|
|
|
The name attribute has been present in CPython since its inception, but
|
|
|
|
until Python 3.4 was not formally specified, so may not exist on some
|
|
|
|
platforms.
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
A hash object has the following methods:
|
|
|
|
|
|
|
|
|
2018-07-31 03:50:16 -03:00
|
|
|
.. method:: hash.update(data)
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2018-07-31 03:50:16 -03:00
|
|
|
Update the hash object with the :term:`bytes-like object`.
|
|
|
|
Repeated calls are equivalent to a single call with the
|
2007-09-06 11:09:10 -03:00
|
|
|
concatenation of all the arguments: ``m.update(a); m.update(b)`` is
|
|
|
|
equivalent to ``m.update(a+b)``.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
2009-05-05 06:29:50 -03:00
|
|
|
.. versionchanged:: 3.1
|
2010-08-17 12:07:14 -03:00
|
|
|
The Python GIL is released to allow other threads to run while hash
|
2013-10-03 23:20:37 -03:00
|
|
|
updates on data larger than 2047 bytes is taking place when using hash
|
2010-08-17 12:07:14 -03:00
|
|
|
algorithms supplied by OpenSSL.
|
2009-05-03 21:45:33 -03:00
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
.. method:: hash.digest()
|
|
|
|
|
2007-09-06 11:09:10 -03:00
|
|
|
Return the digest of the data passed to the :meth:`update` method so far.
|
2010-12-30 03:07:58 -04:00
|
|
|
This is a bytes object of size :attr:`digest_size` which may contain bytes in
|
2007-09-06 11:09:10 -03:00
|
|
|
the whole range from 0 to 255.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: hash.hexdigest()
|
|
|
|
|
2007-09-06 11:09:10 -03:00
|
|
|
Like :meth:`digest` except the digest is returned as a string object of
|
|
|
|
double length, containing only hexadecimal digits. This may be used to
|
|
|
|
exchange the value safely in email or other non-binary environments.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. method:: hash.copy()
|
|
|
|
|
|
|
|
Return a copy ("clone") of the hash object. This can be used to efficiently
|
2007-09-06 11:09:10 -03:00
|
|
|
compute the digests of data sharing a common initial substring.
|
2007-08-15 11:28:22 -03:00
|
|
|
|
|
|
|
|
2016-09-07 06:58:24 -03:00
|
|
|
SHAKE variable length digests
|
|
|
|
-----------------------------
|
|
|
|
|
|
|
|
The :func:`shake_128` and :func:`shake_256` algorithms provide variable
|
|
|
|
length digests with length_in_bits//2 up to 128 or 256 bits of security.
|
|
|
|
As such, their digest methods require a length. Maximum length is not limited
|
|
|
|
by the SHAKE algorithm.
|
|
|
|
|
|
|
|
.. method:: shake.digest(length)
|
|
|
|
|
|
|
|
Return the digest of the data passed to the :meth:`update` method so far.
|
2018-07-31 03:50:16 -03:00
|
|
|
This is a bytes object of size *length* which may contain bytes in
|
2016-09-07 06:58:24 -03:00
|
|
|
the whole range from 0 to 255.
|
|
|
|
|
|
|
|
|
|
|
|
.. method:: shake.hexdigest(length)
|
|
|
|
|
|
|
|
Like :meth:`digest` except the digest is returned as a string object of
|
|
|
|
double length, containing only hexadecimal digits. This may be used to
|
|
|
|
exchange the value safely in email or other non-binary environments.
|
|
|
|
|
|
|
|
|
2015-09-27 05:23:10 -03:00
|
|
|
Key derivation
|
|
|
|
--------------
|
2013-10-12 19:52:43 -03:00
|
|
|
|
|
|
|
Key derivation and key stretching algorithms are designed for secure password
|
2014-05-26 19:41:26 -03:00
|
|
|
hashing. Naive algorithms such as ``sha1(password)`` are not resistant against
|
|
|
|
brute-force attacks. A good password hashing function must be tunable, slow, and
|
2014-05-26 19:42:29 -03:00
|
|
|
include a `salt <https://en.wikipedia.org/wiki/Salt_%28cryptography%29>`_.
|
2013-10-12 19:52:43 -03:00
|
|
|
|
|
|
|
|
2016-02-22 05:21:49 -04:00
|
|
|
.. function:: pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None)
|
2013-10-12 19:52:43 -03:00
|
|
|
|
|
|
|
The function provides PKCS#5 password-based key derivation function 2. It
|
|
|
|
uses HMAC as pseudorandom function.
|
|
|
|
|
2016-02-22 05:21:49 -04:00
|
|
|
The string *hash_name* is the desired name of the hash digest algorithm for
|
2013-10-12 19:52:43 -03:00
|
|
|
HMAC, e.g. 'sha1' or 'sha256'. *password* and *salt* are interpreted as
|
|
|
|
buffers of bytes. Applications and libraries should limit *password* to
|
2016-02-22 05:21:49 -04:00
|
|
|
a sensible length (e.g. 1024). *salt* should be about 16 or more bytes from
|
2013-10-12 19:52:43 -03:00
|
|
|
a proper source, e.g. :func:`os.urandom`.
|
|
|
|
|
2016-02-22 05:21:49 -04:00
|
|
|
The number of *iterations* should be chosen based on the hash algorithm and
|
|
|
|
computing power. As of 2013, at least 100,000 iterations of SHA-256 are
|
|
|
|
suggested.
|
2013-10-12 19:52:43 -03:00
|
|
|
|
|
|
|
*dklen* is the length of the derived key. If *dklen* is ``None`` then the
|
2016-02-22 05:21:49 -04:00
|
|
|
digest size of the hash algorithm *hash_name* is used, e.g. 64 for SHA-512.
|
2013-10-12 19:52:43 -03:00
|
|
|
|
2018-09-10 22:07:19 -03:00
|
|
|
>>> import hashlib
|
2013-10-12 19:52:43 -03:00
|
|
|
>>> dk = hashlib.pbkdf2_hmac('sha256', b'password', b'salt', 100000)
|
2018-09-10 22:07:19 -03:00
|
|
|
>>> dk.hex()
|
|
|
|
'0394a2ede332c9a13eb82e9b24631604c31df978b4e2f0fbd2c549944f9d79a5'
|
2013-10-12 19:52:43 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.4
|
|
|
|
|
2014-05-26 19:45:14 -03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
A fast implementation of *pbkdf2_hmac* is available with OpenSSL. The
|
|
|
|
Python implementation uses an inline version of :mod:`hmac`. It is about
|
|
|
|
three times slower and doesn't release the GIL.
|
2013-10-12 19:52:43 -03:00
|
|
|
|
2016-09-06 15:22:28 -03:00
|
|
|
.. function:: scrypt(password, *, salt, n, r, p, maxmem=0, dklen=64)
|
|
|
|
|
|
|
|
The function provides scrypt password-based key derivation function as
|
|
|
|
defined in :rfc:`7914`.
|
|
|
|
|
2018-07-31 03:50:16 -03:00
|
|
|
*password* and *salt* must be :term:`bytes-like objects
|
|
|
|
<bytes-like object>`. Applications and libraries should limit *password*
|
|
|
|
to a sensible length (e.g. 1024). *salt* should be about 16 or more
|
|
|
|
bytes from a proper source, e.g. :func:`os.urandom`.
|
2016-09-06 15:22:28 -03:00
|
|
|
|
|
|
|
*n* is the CPU/Memory cost factor, *r* the block size, *p* parallelization
|
2017-11-08 18:44:44 -04:00
|
|
|
factor and *maxmem* limits memory (OpenSSL 1.1.0 defaults to 32 MiB).
|
2016-09-06 15:22:28 -03:00
|
|
|
*dklen* is the length of the derived key.
|
|
|
|
|
2018-10-12 11:55:20 -03:00
|
|
|
.. availability:: OpenSSL 1.1+.
|
2016-09-06 15:22:28 -03:00
|
|
|
|
|
|
|
.. versionadded:: 3.6
|
|
|
|
|
2013-10-12 19:52:43 -03:00
|
|
|
|
2016-09-06 17:03:25 -03:00
|
|
|
BLAKE2
|
|
|
|
------
|
|
|
|
|
2017-01-13 06:29:58 -04:00
|
|
|
.. sectionauthor:: Dmitry Chestnykh
|
|
|
|
|
|
|
|
.. index::
|
|
|
|
single: blake2b, blake2s
|
|
|
|
|
2018-05-31 01:39:00 -03:00
|
|
|
BLAKE2_ is a cryptographic hash function defined in :rfc:`7693` that comes in two
|
2017-01-13 06:29:58 -04:00
|
|
|
flavors:
|
|
|
|
|
|
|
|
* **BLAKE2b**, optimized for 64-bit platforms and produces digests of any size
|
|
|
|
between 1 and 64 bytes,
|
|
|
|
|
|
|
|
* **BLAKE2s**, optimized for 8- to 32-bit platforms and produces digests of any
|
|
|
|
size between 1 and 32 bytes.
|
|
|
|
|
|
|
|
BLAKE2 supports **keyed mode** (a faster and simpler replacement for HMAC_),
|
|
|
|
**salted hashing**, **personalization**, and **tree hashing**.
|
|
|
|
|
|
|
|
Hash objects from this module follow the API of standard library's
|
|
|
|
:mod:`hashlib` objects.
|
|
|
|
|
|
|
|
|
|
|
|
Creating hash objects
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
New hash objects are created by calling constructor functions:
|
|
|
|
|
|
|
|
|
2018-07-31 03:50:16 -03:00
|
|
|
.. function:: blake2b(data=b'', *, digest_size=64, key=b'', salt=b'', \
|
2017-01-13 06:29:58 -04:00
|
|
|
person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
|
2019-09-12 21:30:00 -03:00
|
|
|
node_depth=0, inner_size=0, last_node=False, \
|
|
|
|
usedforsecurity=True)
|
2017-01-13 06:29:58 -04:00
|
|
|
|
2018-07-31 03:50:16 -03:00
|
|
|
.. function:: blake2s(data=b'', *, digest_size=32, key=b'', salt=b'', \
|
2017-01-13 06:29:58 -04:00
|
|
|
person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, \
|
2019-09-12 21:30:00 -03:00
|
|
|
node_depth=0, inner_size=0, last_node=False, \
|
|
|
|
usedforsecurity=True)
|
2017-01-13 06:29:58 -04:00
|
|
|
|
|
|
|
|
|
|
|
These functions return the corresponding hash objects for calculating
|
|
|
|
BLAKE2b or BLAKE2s. They optionally take these general parameters:
|
|
|
|
|
2018-07-31 03:50:16 -03:00
|
|
|
* *data*: initial chunk of data to hash, which must be
|
|
|
|
:term:`bytes-like object`. It can be passed only as positional argument.
|
2017-01-13 06:29:58 -04:00
|
|
|
|
|
|
|
* *digest_size*: size of output digest in bytes.
|
|
|
|
|
|
|
|
* *key*: key for keyed hashing (up to 64 bytes for BLAKE2b, up to 32 bytes for
|
|
|
|
BLAKE2s).
|
|
|
|
|
|
|
|
* *salt*: salt for randomized hashing (up to 16 bytes for BLAKE2b, up to 8
|
|
|
|
bytes for BLAKE2s).
|
|
|
|
|
|
|
|
* *person*: personalization string (up to 16 bytes for BLAKE2b, up to 8 bytes
|
|
|
|
for BLAKE2s).
|
|
|
|
|
|
|
|
The following table shows limits for general parameters (in bytes):
|
|
|
|
|
|
|
|
======= =========== ======== ========= ===========
|
|
|
|
Hash digest_size len(key) len(salt) len(person)
|
|
|
|
======= =========== ======== ========= ===========
|
|
|
|
BLAKE2b 64 64 16 16
|
|
|
|
BLAKE2s 32 32 8 8
|
|
|
|
======= =========== ======== ========= ===========
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
BLAKE2 specification defines constant lengths for salt and personalization
|
|
|
|
parameters, however, for convenience, this implementation accepts byte
|
|
|
|
strings of any size up to the specified length. If the length of the
|
|
|
|
parameter is less than specified, it is padded with zeros, thus, for
|
|
|
|
example, ``b'salt'`` and ``b'salt\x00'`` is the same value. (This is not
|
|
|
|
the case for *key*.)
|
|
|
|
|
|
|
|
These sizes are available as module `constants`_ described below.
|
|
|
|
|
|
|
|
Constructor functions also accept the following tree hashing parameters:
|
|
|
|
|
|
|
|
* *fanout*: fanout (0 to 255, 0 if unlimited, 1 in sequential mode).
|
|
|
|
|
|
|
|
* *depth*: maximal depth of tree (1 to 255, 255 if unlimited, 1 in
|
|
|
|
sequential mode).
|
|
|
|
|
|
|
|
* *leaf_size*: maximal byte length of leaf (0 to 2**32-1, 0 if unlimited or in
|
|
|
|
sequential mode).
|
|
|
|
|
|
|
|
* *node_offset*: node offset (0 to 2**64-1 for BLAKE2b, 0 to 2**48-1 for
|
|
|
|
BLAKE2s, 0 for the first, leftmost, leaf, or in sequential mode).
|
|
|
|
|
|
|
|
* *node_depth*: node depth (0 to 255, 0 for leaves, or in sequential mode).
|
|
|
|
|
|
|
|
* *inner_size*: inner digest size (0 to 64 for BLAKE2b, 0 to 32 for
|
|
|
|
BLAKE2s, 0 in sequential mode).
|
|
|
|
|
|
|
|
* *last_node*: boolean indicating whether the processed node is the last
|
|
|
|
one (`False` for sequential mode).
|
|
|
|
|
|
|
|
.. figure:: hashlib-blake2-tree.png
|
|
|
|
:alt: Explanation of tree mode parameters.
|
|
|
|
|
|
|
|
See section 2.10 in `BLAKE2 specification
|
|
|
|
<https://blake2.net/blake2_20130129.pdf>`_ for comprehensive review of tree
|
|
|
|
hashing.
|
|
|
|
|
|
|
|
|
|
|
|
Constants
|
|
|
|
^^^^^^^^^
|
|
|
|
|
|
|
|
.. data:: blake2b.SALT_SIZE
|
|
|
|
.. data:: blake2s.SALT_SIZE
|
|
|
|
|
|
|
|
Salt length (maximum length accepted by constructors).
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: blake2b.PERSON_SIZE
|
|
|
|
.. data:: blake2s.PERSON_SIZE
|
|
|
|
|
|
|
|
Personalization string length (maximum length accepted by constructors).
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: blake2b.MAX_KEY_SIZE
|
|
|
|
.. data:: blake2s.MAX_KEY_SIZE
|
|
|
|
|
|
|
|
Maximum key size.
|
|
|
|
|
|
|
|
|
|
|
|
.. data:: blake2b.MAX_DIGEST_SIZE
|
|
|
|
.. data:: blake2s.MAX_DIGEST_SIZE
|
|
|
|
|
|
|
|
Maximum digest size that the hash function can output.
|
|
|
|
|
|
|
|
|
|
|
|
Examples
|
|
|
|
^^^^^^^^
|
|
|
|
|
|
|
|
Simple hashing
|
|
|
|
""""""""""""""
|
|
|
|
|
|
|
|
To calculate hash of some data, you should first construct a hash object by
|
|
|
|
calling the appropriate constructor function (:func:`blake2b` or
|
|
|
|
:func:`blake2s`), then update it with the data by calling :meth:`update` on the
|
|
|
|
object, and, finally, get the digest out of the object by calling
|
|
|
|
:meth:`digest` (or :meth:`hexdigest` for hex-encoded string).
|
|
|
|
|
|
|
|
>>> from hashlib import blake2b
|
|
|
|
>>> h = blake2b()
|
|
|
|
>>> h.update(b'Hello world')
|
|
|
|
>>> h.hexdigest()
|
|
|
|
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
|
|
|
|
|
|
|
|
|
|
|
|
As a shortcut, you can pass the first chunk of data to update directly to the
|
2018-07-31 03:50:16 -03:00
|
|
|
constructor as the positional argument:
|
2017-01-13 06:29:58 -04:00
|
|
|
|
|
|
|
>>> from hashlib import blake2b
|
|
|
|
>>> blake2b(b'Hello world').hexdigest()
|
|
|
|
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
|
|
|
|
|
|
|
|
You can call :meth:`hash.update` as many times as you need to iteratively
|
|
|
|
update the hash:
|
|
|
|
|
|
|
|
>>> from hashlib import blake2b
|
|
|
|
>>> items = [b'Hello', b' ', b'world']
|
|
|
|
>>> h = blake2b()
|
|
|
|
>>> for item in items:
|
|
|
|
... h.update(item)
|
|
|
|
>>> h.hexdigest()
|
|
|
|
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
|
|
|
|
|
|
|
|
|
|
|
|
Using different digest sizes
|
|
|
|
""""""""""""""""""""""""""""
|
|
|
|
|
|
|
|
BLAKE2 has configurable size of digests up to 64 bytes for BLAKE2b and up to 32
|
|
|
|
bytes for BLAKE2s. For example, to replace SHA-1 with BLAKE2b without changing
|
|
|
|
the size of output, we can tell BLAKE2b to produce 20-byte digests:
|
|
|
|
|
|
|
|
>>> from hashlib import blake2b
|
|
|
|
>>> h = blake2b(digest_size=20)
|
|
|
|
>>> h.update(b'Replacing SHA1 with the more secure function')
|
|
|
|
>>> h.hexdigest()
|
|
|
|
'd24f26cf8de66472d58d4e1b1774b4c9158b1f4c'
|
|
|
|
>>> h.digest_size
|
|
|
|
20
|
|
|
|
>>> len(h.digest())
|
|
|
|
20
|
|
|
|
|
|
|
|
Hash objects with different digest sizes have completely different outputs
|
|
|
|
(shorter hashes are *not* prefixes of longer hashes); BLAKE2b and BLAKE2s
|
|
|
|
produce different outputs even if the output length is the same:
|
|
|
|
|
|
|
|
>>> from hashlib import blake2b, blake2s
|
|
|
|
>>> blake2b(digest_size=10).hexdigest()
|
|
|
|
'6fa1d8fcfd719046d762'
|
|
|
|
>>> blake2b(digest_size=11).hexdigest()
|
|
|
|
'eb6ec15daf9546254f0809'
|
|
|
|
>>> blake2s(digest_size=10).hexdigest()
|
|
|
|
'1bf21a98c78a1c376ae9'
|
|
|
|
>>> blake2s(digest_size=11).hexdigest()
|
|
|
|
'567004bf96e4a25773ebf4'
|
|
|
|
|
|
|
|
|
|
|
|
Keyed hashing
|
|
|
|
"""""""""""""
|
|
|
|
|
|
|
|
Keyed hashing can be used for authentication as a faster and simpler
|
|
|
|
replacement for `Hash-based message authentication code
|
2017-12-06 12:39:33 -04:00
|
|
|
<https://en.wikipedia.org/wiki/Hash-based_message_authentication_code>`_ (HMAC).
|
2017-01-13 06:29:58 -04:00
|
|
|
BLAKE2 can be securely used in prefix-MAC mode thanks to the
|
|
|
|
indifferentiability property inherited from BLAKE.
|
|
|
|
|
|
|
|
This example shows how to get a (hex-encoded) 128-bit authentication code for
|
|
|
|
message ``b'message data'`` with key ``b'pseudorandom key'``::
|
|
|
|
|
|
|
|
>>> from hashlib import blake2b
|
|
|
|
>>> h = blake2b(key=b'pseudorandom key', digest_size=16)
|
|
|
|
>>> h.update(b'message data')
|
|
|
|
>>> h.hexdigest()
|
|
|
|
'3d363ff7401e02026f4a4687d4863ced'
|
|
|
|
|
|
|
|
|
|
|
|
As a practical example, a web application can symmetrically sign cookies sent
|
|
|
|
to users and later verify them to make sure they weren't tampered with::
|
|
|
|
|
|
|
|
>>> from hashlib import blake2b
|
|
|
|
>>> from hmac import compare_digest
|
|
|
|
>>>
|
|
|
|
>>> SECRET_KEY = b'pseudorandomly generated server secret key'
|
|
|
|
>>> AUTH_SIZE = 16
|
|
|
|
>>>
|
|
|
|
>>> def sign(cookie):
|
2017-09-14 03:24:36 -03:00
|
|
|
... h = blake2b(digest_size=AUTH_SIZE, key=SECRET_KEY)
|
|
|
|
... h.update(cookie)
|
|
|
|
... return h.hexdigest().encode('utf-8')
|
2017-01-13 06:29:58 -04:00
|
|
|
>>>
|
2017-09-23 14:18:40 -03:00
|
|
|
>>> def verify(cookie, sig):
|
|
|
|
... good_sig = sign(cookie)
|
|
|
|
... return compare_digest(good_sig, sig)
|
|
|
|
>>>
|
|
|
|
>>> cookie = b'user-alice'
|
2017-01-13 06:29:58 -04:00
|
|
|
>>> sig = sign(cookie)
|
|
|
|
>>> print("{0},{1}".format(cookie.decode('utf-8'), sig))
|
2017-09-23 14:18:40 -03:00
|
|
|
user-alice,b'43b3c982cf697e0c5ab22172d1ca7421'
|
|
|
|
>>> verify(cookie, sig)
|
2017-01-13 06:29:58 -04:00
|
|
|
True
|
2017-09-23 14:18:40 -03:00
|
|
|
>>> verify(b'user-bob', sig)
|
2017-01-13 06:29:58 -04:00
|
|
|
False
|
2017-09-23 14:18:40 -03:00
|
|
|
>>> verify(cookie, b'0102030405060708090a0b0c0d0e0f00')
|
2017-01-13 06:29:58 -04:00
|
|
|
False
|
|
|
|
|
|
|
|
Even though there's a native keyed hashing mode, BLAKE2 can, of course, be used
|
|
|
|
in HMAC construction with :mod:`hmac` module::
|
|
|
|
|
|
|
|
>>> import hmac, hashlib
|
|
|
|
>>> m = hmac.new(b'secret key', digestmod=hashlib.blake2s)
|
|
|
|
>>> m.update(b'message')
|
|
|
|
>>> m.hexdigest()
|
|
|
|
'e3c8102868d28b5ff85fc35dda07329970d1a01e273c37481326fe0c861c8142'
|
|
|
|
|
|
|
|
|
|
|
|
Randomized hashing
|
|
|
|
""""""""""""""""""
|
|
|
|
|
|
|
|
By setting *salt* parameter users can introduce randomization to the hash
|
|
|
|
function. Randomized hashing is useful for protecting against collision attacks
|
|
|
|
on the hash function used in digital signatures.
|
|
|
|
|
|
|
|
Randomized hashing is designed for situations where one party, the message
|
|
|
|
preparer, generates all or part of a message to be signed by a second
|
|
|
|
party, the message signer. If the message preparer is able to find
|
|
|
|
cryptographic hash function collisions (i.e., two messages producing the
|
2018-06-18 01:34:30 -03:00
|
|
|
same hash value), then they might prepare meaningful versions of the message
|
2017-01-13 06:29:58 -04:00
|
|
|
that would produce the same hash value and digital signature, but with
|
|
|
|
different results (e.g., transferring $1,000,000 to an account, rather than
|
|
|
|
$10). Cryptographic hash functions have been designed with collision
|
|
|
|
resistance as a major goal, but the current concentration on attacking
|
|
|
|
cryptographic hash functions may result in a given cryptographic hash
|
|
|
|
function providing less collision resistance than expected. Randomized
|
|
|
|
hashing offers the signer additional protection by reducing the likelihood
|
|
|
|
that a preparer can generate two or more messages that ultimately yield the
|
|
|
|
same hash value during the digital signature generation process --- even if
|
|
|
|
it is practical to find collisions for the hash function. However, the use
|
|
|
|
of randomized hashing may reduce the amount of security provided by a
|
|
|
|
digital signature when all portions of the message are prepared
|
|
|
|
by the signer.
|
|
|
|
|
|
|
|
(`NIST SP-800-106 "Randomized Hashing for Digital Signatures"
|
2018-01-19 20:25:37 -04:00
|
|
|
<https://csrc.nist.gov/publications/detail/sp/800-106/final>`_)
|
2017-01-13 06:29:58 -04:00
|
|
|
|
|
|
|
In BLAKE2 the salt is processed as a one-time input to the hash function during
|
|
|
|
initialization, rather than as an input to each compression function.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
*Salted hashing* (or just hashing) with BLAKE2 or any other general-purpose
|
|
|
|
cryptographic hash function, such as SHA-256, is not suitable for hashing
|
|
|
|
passwords. See `BLAKE2 FAQ <https://blake2.net/#qa>`_ for more
|
|
|
|
information.
|
|
|
|
..
|
|
|
|
|
|
|
|
>>> import os
|
|
|
|
>>> from hashlib import blake2b
|
|
|
|
>>> msg = b'some message'
|
|
|
|
>>> # Calculate the first hash with a random salt.
|
|
|
|
>>> salt1 = os.urandom(blake2b.SALT_SIZE)
|
|
|
|
>>> h1 = blake2b(salt=salt1)
|
|
|
|
>>> h1.update(msg)
|
|
|
|
>>> # Calculate the second hash with a different random salt.
|
|
|
|
>>> salt2 = os.urandom(blake2b.SALT_SIZE)
|
|
|
|
>>> h2 = blake2b(salt=salt2)
|
|
|
|
>>> h2.update(msg)
|
|
|
|
>>> # The digests are different.
|
|
|
|
>>> h1.digest() != h2.digest()
|
|
|
|
True
|
|
|
|
|
|
|
|
|
|
|
|
Personalization
|
|
|
|
"""""""""""""""
|
|
|
|
|
|
|
|
Sometimes it is useful to force hash function to produce different digests for
|
|
|
|
the same input for different purposes. Quoting the authors of the Skein hash
|
|
|
|
function:
|
|
|
|
|
|
|
|
We recommend that all application designers seriously consider doing this;
|
|
|
|
we have seen many protocols where a hash that is computed in one part of
|
|
|
|
the protocol can be used in an entirely different part because two hash
|
|
|
|
computations were done on similar or related data, and the attacker can
|
|
|
|
force the application to make the hash inputs the same. Personalizing each
|
|
|
|
hash function used in the protocol summarily stops this type of attack.
|
|
|
|
|
|
|
|
(`The Skein Hash Function Family
|
|
|
|
<http://www.skein-hash.info/sites/default/files/skein1.3.pdf>`_,
|
|
|
|
p. 21)
|
|
|
|
|
|
|
|
BLAKE2 can be personalized by passing bytes to the *person* argument::
|
|
|
|
|
|
|
|
>>> from hashlib import blake2b
|
|
|
|
>>> FILES_HASH_PERSON = b'MyApp Files Hash'
|
|
|
|
>>> BLOCK_HASH_PERSON = b'MyApp Block Hash'
|
|
|
|
>>> h = blake2b(digest_size=32, person=FILES_HASH_PERSON)
|
|
|
|
>>> h.update(b'the same content')
|
|
|
|
>>> h.hexdigest()
|
|
|
|
'20d9cd024d4fb086aae819a1432dd2466de12947831b75c5a30cf2676095d3b4'
|
|
|
|
>>> h = blake2b(digest_size=32, person=BLOCK_HASH_PERSON)
|
|
|
|
>>> h.update(b'the same content')
|
|
|
|
>>> h.hexdigest()
|
|
|
|
'cf68fb5761b9c44e7878bfb2c4c9aea52264a80b75005e65619778de59f383a3'
|
|
|
|
|
|
|
|
Personalization together with the keyed mode can also be used to derive different
|
|
|
|
keys from a single one.
|
|
|
|
|
|
|
|
>>> from hashlib import blake2s
|
|
|
|
>>> from base64 import b64decode, b64encode
|
|
|
|
>>> orig_key = b64decode(b'Rm5EPJai72qcK3RGBpW3vPNfZy5OZothY+kHY6h21KM=')
|
|
|
|
>>> enc_key = blake2s(key=orig_key, person=b'kEncrypt').digest()
|
|
|
|
>>> mac_key = blake2s(key=orig_key, person=b'kMAC').digest()
|
|
|
|
>>> print(b64encode(enc_key).decode('utf-8'))
|
|
|
|
rbPb15S/Z9t+agffno5wuhB77VbRi6F9Iv2qIxU7WHw=
|
|
|
|
>>> print(b64encode(mac_key).decode('utf-8'))
|
|
|
|
G9GtHFE1YluXY1zWPlYk1e/nWfu0WSEb0KRcjhDeP/o=
|
|
|
|
|
|
|
|
Tree mode
|
|
|
|
"""""""""
|
|
|
|
|
|
|
|
Here's an example of hashing a minimal tree with two leaf nodes::
|
|
|
|
|
|
|
|
10
|
|
|
|
/ \
|
|
|
|
00 01
|
|
|
|
|
|
|
|
This example uses 64-byte internal digests, and returns the 32-byte final
|
|
|
|
digest::
|
|
|
|
|
|
|
|
>>> from hashlib import blake2b
|
|
|
|
>>>
|
|
|
|
>>> FANOUT = 2
|
|
|
|
>>> DEPTH = 2
|
|
|
|
>>> LEAF_SIZE = 4096
|
|
|
|
>>> INNER_SIZE = 64
|
|
|
|
>>>
|
|
|
|
>>> buf = bytearray(6000)
|
|
|
|
>>>
|
|
|
|
>>> # Left leaf
|
|
|
|
... h00 = blake2b(buf[0:LEAF_SIZE], fanout=FANOUT, depth=DEPTH,
|
|
|
|
... leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
|
|
|
|
... node_offset=0, node_depth=0, last_node=False)
|
|
|
|
>>> # Right leaf
|
|
|
|
... h01 = blake2b(buf[LEAF_SIZE:], fanout=FANOUT, depth=DEPTH,
|
|
|
|
... leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
|
|
|
|
... node_offset=1, node_depth=0, last_node=True)
|
|
|
|
>>> # Root node
|
|
|
|
... h10 = blake2b(digest_size=32, fanout=FANOUT, depth=DEPTH,
|
|
|
|
... leaf_size=LEAF_SIZE, inner_size=INNER_SIZE,
|
|
|
|
... node_offset=0, node_depth=1, last_node=True)
|
|
|
|
>>> h10.update(h00.digest())
|
|
|
|
>>> h10.update(h01.digest())
|
|
|
|
>>> h10.hexdigest()
|
|
|
|
'3ad2a9b37c6070e374c7a8c508fe20ca86b6ed54e286e93a0318e95e881db5aa'
|
|
|
|
|
|
|
|
Credits
|
|
|
|
^^^^^^^
|
|
|
|
|
|
|
|
BLAKE2_ was designed by *Jean-Philippe Aumasson*, *Samuel Neves*, *Zooko
|
|
|
|
Wilcox-O'Hearn*, and *Christian Winnerlein* based on SHA-3_ finalist BLAKE_
|
|
|
|
created by *Jean-Philippe Aumasson*, *Luca Henzen*, *Willi Meier*, and
|
|
|
|
*Raphael C.-W. Phan*.
|
|
|
|
|
|
|
|
It uses core algorithm from ChaCha_ cipher designed by *Daniel J. Bernstein*.
|
|
|
|
|
|
|
|
The stdlib implementation is based on pyblake2_ module. It was written by
|
|
|
|
*Dmitry Chestnykh* based on C implementation written by *Samuel Neves*. The
|
|
|
|
documentation was copied from pyblake2_ and written by *Dmitry Chestnykh*.
|
|
|
|
|
|
|
|
The C code was partly rewritten for Python by *Christian Heimes*.
|
|
|
|
|
|
|
|
The following public domain dedication applies for both C hash function
|
|
|
|
implementation, extension code, and this documentation:
|
|
|
|
|
|
|
|
To the extent possible under law, the author(s) have dedicated all copyright
|
|
|
|
and related and neighboring rights to this software to the public domain
|
|
|
|
worldwide. This software is distributed without any warranty.
|
|
|
|
|
|
|
|
You should have received a copy of the CC0 Public Domain Dedication along
|
|
|
|
with this software. If not, see
|
2017-12-06 12:39:33 -04:00
|
|
|
https://creativecommons.org/publicdomain/zero/1.0/.
|
2017-01-13 06:29:58 -04:00
|
|
|
|
|
|
|
The following people have helped with development or contributed their changes
|
|
|
|
to the project and the public domain according to the Creative Commons Public
|
|
|
|
Domain Dedication 1.0 Universal:
|
|
|
|
|
|
|
|
* *Alexandr Sokolovskiy*
|
|
|
|
|
|
|
|
.. _BLAKE2: https://blake2.net
|
|
|
|
.. _HMAC: https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
|
|
|
|
.. _BLAKE: https://131002.net/blake/
|
|
|
|
.. _SHA-3: https://en.wikipedia.org/wiki/NIST_hash_function_competition
|
|
|
|
.. _ChaCha: https://cr.yp.to/chacha.html
|
|
|
|
.. _pyblake2: https://pythonhosted.org/pyblake2/
|
|
|
|
|
2016-09-06 17:03:25 -03:00
|
|
|
|
|
|
|
|
2007-08-15 11:28:22 -03:00
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Module :mod:`hmac`
|
|
|
|
A module to generate message authentication codes using hashes.
|
|
|
|
|
|
|
|
Module :mod:`base64`
|
|
|
|
Another way to encode binary hashes for non-binary environments.
|
|
|
|
|
2017-01-13 06:29:58 -04:00
|
|
|
https://blake2.net
|
|
|
|
Official BLAKE2 website.
|
2016-09-06 17:03:25 -03:00
|
|
|
|
2018-01-19 20:25:37 -04:00
|
|
|
https://csrc.nist.gov/csrc/media/publications/fips/180/2/archive/2002-08-01/documents/fips180-2.pdf
|
2007-08-15 11:28:22 -03:00
|
|
|
The FIPS 180-2 publication on Secure Hash Algorithms.
|
|
|
|
|
2015-09-27 06:05:01 -03:00
|
|
|
https://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms
|
2010-05-21 17:28:13 -03:00
|
|
|
Wikipedia article with information on which algorithms have known issues and
|
2007-08-15 11:28:22 -03:00
|
|
|
what that means regarding their use.
|
|
|
|
|
2021-01-22 08:06:00 -04:00
|
|
|
https://www.ietf.org/rfc/rfc8018.txt
|
|
|
|
PKCS #5: Password-Based Cryptography Specification Version 2.1
|