summaryrefslogtreecommitdiff
path: root/rsa
diff options
context:
space:
mode:
authorSybren A. Stüvel <sybren@stuvel.eu>2020-06-11 20:22:01 +0200
committerSybren A. Stüvel <sybren@stuvel.eu>2020-06-11 20:22:30 +0200
commit9032802c2574bc4538f8f54843fd1996aaf396e4 (patch)
tree626e41d63447255540e7935bc8591d128a776343 /rsa
parentfb8772a34b9086567b4b51da5a2d62e641131828 (diff)
downloadrsa-git-9032802c2574bc4538f8f54843fd1996aaf396e4.tar.gz
Limit SHA3 support to Python 3.6+
The third-party library that adds support for this to Python 3.5 is a binary package, and thus breaks the pure-Python nature of Python-RSA. This should fix [#147](https://github.com/sybrenstuvel/python-rsa/issues/147).
Diffstat (limited to 'rsa')
-rw-r--r--rsa/pkcs1.py27
1 files changed, 15 insertions, 12 deletions
diff --git a/rsa/pkcs1.py b/rsa/pkcs1.py
index 408bc5b..57b0276 100644
--- a/rsa/pkcs1.py
+++ b/rsa/pkcs1.py
@@ -33,12 +33,6 @@ import typing
from . import common, transform, core, key
-if sys.version_info < (3, 6):
- # Python 3.6 and newer have SHA-3 support. For Python 3.5 we need a third party library.
- # This library monkey-patches the hashlib module so that it looks like Python actually
- # supports SHA-3 natively.
- import sha3 # noqa: F401
-
# ASN.1 codes that describe the hash algorithm used.
HASH_ASN1 = {
'MD5': b'\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05\x05\x00\x04\x10',
@@ -47,9 +41,6 @@ HASH_ASN1 = {
'SHA-256': b'\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20',
'SHA-384': b'\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02\x05\x00\x04\x30',
'SHA-512': b'\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40',
- 'SHA3-256': b'\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x08\x05\x00\x04\x20',
- 'SHA3-384': b'\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x09\x05\x00\x04\x30',
- 'SHA3-512': b'\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x0a\x05\x00\x04\x40',
}
HASH_METHODS = {
@@ -59,12 +50,24 @@ HASH_METHODS = {
'SHA-256': hashlib.sha256,
'SHA-384': hashlib.sha384,
'SHA-512': hashlib.sha512,
- 'SHA3-256': hashlib.sha3_256,
- 'SHA3-384': hashlib.sha3_384,
- 'SHA3-512': hashlib.sha3_512,
}
+if sys.version_info >= (3, 6):
+ # Python 3.6 introduced SHA3 support.
+ HASH_ASN1.update({
+ 'SHA3-256': b'\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x08\x05\x00\x04\x20',
+ 'SHA3-384': b'\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x09\x05\x00\x04\x30',
+ 'SHA3-512': b'\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x0a\x05\x00\x04\x40',
+ })
+
+ HASH_METHODS.update({
+ 'SHA3-256': hashlib.sha3_256,
+ 'SHA3-384': hashlib.sha3_384,
+ 'SHA3-512': hashlib.sha3_512,
+ })
+
+
class CryptoError(Exception):
"""Base class for all exceptions in this module."""