This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Abstract base class for hashlib
Type: enhancement Stage: patch review
Components: Versions: Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, gregory.p.smith, pitrou, python-dev, rhettinger
Priority: normal Keywords: patch

Created on 2013-08-14 22:31 by christian.heimes, last changed 2022-04-11 14:57 by admin.

Files
File name Uploaded Description Edit
hashlib_abc3.patch christian.heimes, 2013-08-15 23:01 review
hashlib_abc4.patch christian.heimes, 2013-10-22 13:54 review
Messages (16)
msg195226 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-08-14 22:31
All hashlib types provide a common interfaces but there is no common super class. The patch implements provides hashlib.CryptoHash abstract base class as common virtual class for all hash types.

The patch also exposes all internal types of the internal hash C modules so I don't have to jump throw the type(constructor()) hoop.

I have also changed __get_builtin_constructor() to use a lookup cache instead of importing the module every time. It is necessary to avoid multiple calls to CryptoHash.register().
msg195259 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-08-15 15:28
Updated patch.

I'm going to add documentation when everybody is happy with the patch.
msg195301 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-08-15 23:01
Now with block_size
msg195308 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2013-08-16 00:47
your patch makes sense to me.
msg195561 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-08-18 13:29
Thanks Gregory!

Have you had a chance to read http://www.python.org/dev/peps/pep-0452/ , too?
msg200746 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-10-21 10:40
I'm going to add CryptoKeyedHash ABC and register hmac as keyed hash algorithm, too.
msg200940 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-22 12:59
New changeset 72d7b2185771 by Christian Heimes in branch 'default':
Issue #18742: Rework the internal hashlib construtor to pave the road for ABCs.
http://hg.python.org/cpython/rev/72d7b2185771
msg200941 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-22 13:05
New changeset 27da6e790c41 by Christian Heimes in branch 'default':
Issue #18742: Expose the internal hash type object for ABCs.
http://hg.python.org/cpython/rev/27da6e790c41
msg200953 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-10-22 13:54
I have checked in some of the basic and non-controversal parts of the PEP (optimization, expose type object). The ABCs are still missing. In order to cope with keyed and non-keyed CHF I have added a common base class for both and separate ABCs for keyed and non-keyed CHF. They are really different beasts but they provide a common API except for they constructor.

class AbstractCryptoHashFunction(metaclass=_abc.ABCMeta):
    """Common ABC for keyed and non-keyed cryptographic hash functions"""

class CryptoHashFunction(AbstractCryptoHashFunction):
    """Abstract base class for cryptographic hash functions (PEP 247)"""

class KeyedCryptoHashFunction(AbstractCryptoHashFunction):
    """Abstract base class for keyed cryptographic hashs function (PEP 247)"""
msg200988 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2013-10-22 20:03
>
> Have you had a chance to read http://www.python.org/dev/peps/pep-0452/ ,
too?

Overall pep 452 looks good but one thing popped out at me:

        >>> import hashlib
        >>> from Crypto.Hash import MD5
        >>> m = MD5.new()
        >>> isinstance(m, hashlib.CryptoHash)
        True

This suggests that there is an ABC hashlib.CryptoHash that non hashlib
implementations sound use but that is not spelled out anywhere as required
or recommended for third party hash implementations or why using it is a
good thing. I believe that is what you want to do with the ABC being
defined in this issue so I'd mention that explicitly in the pep.
msg200996 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2013-10-23 04:49
Consider inheriting from abc.ABC rather than specifying metaclass=abc.ABCMeta
msg203164 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-11-17 14:17
Raymond: Good idea!

to all: Do we need more discussion about the ABC, e.g. on the pycrypto mailing list?
msg203169 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-11-17 14:24
Some comments:
- AbstractCryptoHashFunction should be called CryptoHashBase or something (but does it warrant being public? I don't think so)
- having "Function" in a class name is a bit confusing to me. Why not simply "CryptoHash"?
- you don't need to add a __slots__ to your ABCs, IMO
- the default hexdigest() implementation looks a bit suboptimal to me, why not use binascii?
msg203197 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-11-17 16:39
* "Cryptographic Hash Function" is the correct technical term for algorithms like SHA-1. http://en.wikipedia.org/wiki/Cryptographic_hash_function

* PEP 452 is going to suggest that 3rd party libraries register their hash function as subclasses of the ABC.

* __slots__ are required for subclassing. All our ABCs in Python 3.x have __slots__

* I don't want to import yet another module just for the ABC. I'd rather not provide a sample implementation of hexdigest() if you think it is too slow.
msg203198 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-11-17 16:46
> * "Cryptographic Hash Function" is the correct technical term for
> algorithms like SHA-1.

Sure, but in a programming context, it's confusing when the "function"
is implemented by a class with additional properties and methods.
Why not simply "CryptoHash"? It sounds descriptive enough to me.

> * I don't want to import yet another module just for the ABC. I'd
> rather not provide a sample implementation of hexdigest() if you think
> it is too slow.

Well, it's probably not excruciatingly slow, so if you don't want
another import, I guess the current implementation is ok :)
msg203201 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2013-11-17 16:55
It seems the name of the ABC needs more discussion. I'll address it in PEP 452 for Python 3.5.
History
Date User Action Args
2022-04-11 14:57:49adminsetgithub: 62942
2013-11-17 16:55:18christian.heimessetmessages: + msg203201
versions: + Python 3.5, - Python 3.4
2013-11-17 16:46:58pitrousetmessages: + msg203198
2013-11-17 16:39:19christian.heimessetmessages: + msg203197
2013-11-17 14:24:05pitrousetmessages: + msg203169
2013-11-17 14:17:49christian.heimessetmessages: + msg203164
2013-10-23 04:49:49rhettingersetnosy: + rhettinger
messages: + msg200996
2013-10-22 20:03:06gregory.p.smithsetmessages: + msg200988
2013-10-22 13:54:50christian.heimessetfiles: + hashlib_abc4.patch

messages: + msg200953
2013-10-22 13:05:54python-devsetmessages: + msg200941
2013-10-22 12:59:44python-devsetnosy: + python-dev
messages: + msg200940
2013-10-21 10:40:39christian.heimessetmessages: + msg200746
2013-08-18 13:29:16christian.heimessetmessages: + msg195561
2013-08-16 00:47:13gregory.p.smithsetmessages: + msg195308
2013-08-15 23:01:18christian.heimessetfiles: + hashlib_abc3.patch

messages: + msg195301
2013-08-15 23:01:01christian.heimessetfiles: - hashlib_abc2.patch
2013-08-15 23:00:55christian.heimessetfiles: - hashlib_abc.patch
2013-08-15 15:28:32christian.heimessetfiles: + hashlib_abc2.patch
nosy: + pitrou
messages: + msg195259

2013-08-14 22:31:12christian.heimescreate