Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(29470)

Side by Side Diff: Lib/hashlib.py

Issue 16113: Add SHA-3 (Keccak) support
Patch Set: Created 7 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Doc/whatsnew/3.4.rst ('k') | Lib/test/test_hashlib.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) 1 # Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
2 # Licensed to PSF under a Contributor Agreement. 2 # Licensed to PSF under a Contributor Agreement.
3 # 3 #
4 4
5 __doc__ = """hashlib module - A common interface to many hash functions. 5 __doc__ = """hashlib module - A common interface to many hash functions.
6 6
7 new(name, data=b'') - returns a new hash object implementing the 7 new(name, data=b'') - returns a new hash object implementing the
8 given hash function; initializing the hash 8 given hash function; initializing the hash
9 using the given binary data. 9 using the given binary data.
10 10
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 47
48 More condensed: 48 More condensed:
49 49
50 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest() 50 >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
51 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2' 51 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
52 52
53 """ 53 """
54 54
55 # This tuple and __get_builtin_constructor() must be modified if a new 55 # This tuple and __get_builtin_constructor() must be modified if a new
56 # always available algorithm is added. 56 # always available algorithm is added.
57 __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512') 57 __always_supported = ('md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512',
58 'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512')
58 59
59 algorithms_guaranteed = set(__always_supported) 60 algorithms_guaranteed = set(__always_supported)
60 algorithms_available = set(__always_supported) 61 algorithms_available = set(__always_supported)
61 62
62 __all__ = __always_supported + ('new', 'algorithms_guaranteed', 63 __all__ = __always_supported + ('new', 'algorithms_guaranteed',
63 'algorithms_available') 64 'algorithms_available')
64 65
65 66
66 def __get_builtin_constructor(name): 67 def __get_builtin_constructor(name):
67 try: 68 try:
(...skipping 10 matching lines...) Expand all
78 return _sha256.sha256 79 return _sha256.sha256
79 elif bs == '224': 80 elif bs == '224':
80 return _sha256.sha224 81 return _sha256.sha224
81 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): 82 elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'):
82 import _sha512 83 import _sha512
83 bs = name[3:] 84 bs = name[3:]
84 if bs == '512': 85 if bs == '512':
85 return _sha512.sha512 86 return _sha512.sha512
86 elif bs == '384': 87 elif bs == '384':
87 return _sha512.sha384 88 return _sha512.sha384
89 elif name in {'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
90 'SHA3_224', 'SHA3_256', 'SHA3_384', 'SHA3_512'}:
91 import _sha3
92 bs = name[5:]
93 if bs == '224':
94 return _sha3.sha3_224
95 elif bs == '256':
96 return _sha3.sha3_256
97 elif bs == '384':
98 return _sha3.sha3_384
99 elif bs == '512':
100 return _sha3.sha3_512
88 except ImportError: 101 except ImportError:
89 pass # no extension module, this hash is unsupported. 102 pass # no extension module, this hash is unsupported.
90 103
91 raise ValueError('unsupported hash type ' + name) 104 raise ValueError('unsupported hash type ' + name)
92 105
93 106
94 def __get_openssl_constructor(name): 107 def __get_openssl_constructor(name):
95 try: 108 try:
96 f = getattr(_hashlib, 'openssl_' + name) 109 f = getattr(_hashlib, 'openssl_' + name)
97 # Allow the C module to raise ValueError. The function will be 110 # Allow the C module to raise ValueError. The function will be
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 # version not supporting that algorithm. 152 # version not supporting that algorithm.
140 try: 153 try:
141 globals()[__func_name] = __get_hash(__func_name) 154 globals()[__func_name] = __get_hash(__func_name)
142 except ValueError: 155 except ValueError:
143 import logging 156 import logging
144 logging.exception('code for hash %s was not found.', __func_name) 157 logging.exception('code for hash %s was not found.', __func_name)
145 158
146 # Cleanup locals() 159 # Cleanup locals()
147 del __always_supported, __func_name, __get_hash 160 del __always_supported, __func_name, __get_hash
148 del __py_new, __hash_new, __get_openssl_constructor 161 del __py_new, __hash_new, __get_openssl_constructor
OLDNEW
« no previous file with comments | « Doc/whatsnew/3.4.rst ('k') | Lib/test/test_hashlib.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+