Skip to content

Commit 3c5ee59

Browse files
committed
Add support for SHA3 hashing
This is based on sybrenstuvel#96, with a few improvements: - The minimum of one use of SHA3 in a unit test, to at least touch it at some point. - Documented the support of SHA3. - Only install the third-party library required by Python 3.5 when we're running on Python 3.5. Newer Python versions support SHA3 natively.
1 parent b68f618 commit 3c5ee59

File tree

6 files changed

+33
-3
lines changed

6 files changed

+33
-3
lines changed

CHANGELOG.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Version 4.1 - in development
1212
gives UnicodeDecodeError.
1313
- Switched to using [Poetry](https://poetry.eustace.io/) for package
1414
management.
15+
- Added support for SHA3 hashing: SHA3-256, SHA3-384, SHA3-512. This
16+
is natively supported by Python 3.6+ and supported via a third-party
17+
library on Python 3.5.
1518

1619

1720
Version 4.0 - released 2018-09-16

doc/compatibility.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Encryption:
1616

1717
Signatures:
1818
PKCS#1 v1.5 using the following hash methods:
19-
MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512
19+
MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA3-256, SHA3-384, SHA3-512
2020

2121
Private keys:
2222
PKCS#1 v1.5 in PEM and DER format, ASN.1 type RSAPrivateKey

poetry.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pyrsa-verify = "rsa.cli:verify"
3232
[tool.poetry.dependencies]
3333
python = "^3.5"
3434
pyasn1 = ">=0.1.3"
35+
pysha3 = {version="^1.0", python="~3.5"}
3536

3637
[tool.poetry.dev-dependencies]
3738
coveralls = "^1.8"

rsa/pkcs1.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,16 @@
3030

3131
import hashlib
3232
import os
33+
import sys
3334
import typing
3435

36+
if sys.version_info < (3, 6):
37+
# Python 3.6 and newer have SHA-3 support. For Python 3.5 we need a third party library.
38+
# This library monkey-patches the hashlib module so that it looks like Python actually
39+
# supports SHA-3 natively.
40+
import sha3
41+
42+
3543
from . import common, transform, core, key
3644

3745
# ASN.1 codes that describe the hash algorithm used.
@@ -42,6 +50,9 @@
4250
'SHA-256': b'\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01\x05\x00\x04\x20',
4351
'SHA-384': b'\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02\x05\x00\x04\x30',
4452
'SHA-512': b'\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03\x05\x00\x04\x40',
53+
'SHA3-256': b'\x30\x31\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x08\x05\x00\x04\x20',
54+
'SHA3-384': b'\x30\x41\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x09\x05\x00\x04\x30',
55+
'SHA3-512': b'\x30\x51\x30\x0d\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x0a\x05\x00\x04\x40',
4556
}
4657

4758
HASH_METHODS = {
@@ -51,6 +62,9 @@
5162
'SHA-256': hashlib.sha256,
5263
'SHA-384': hashlib.sha384,
5364
'SHA-512': hashlib.sha512,
65+
'SHA3-256': hashlib.sha3_256,
66+
'SHA3-384': hashlib.sha3_384,
67+
'SHA3-512': hashlib.sha3_512,
5468
}
5569

5670

tests/test_pkcs1.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ def test_sign_verify(self):
7575

7676
message = b'je moeder'
7777
signature = pkcs1.sign(message, self.priv, 'SHA-256')
78-
7978
self.assertEqual('SHA-256', pkcs1.verify(message, signature, self.pub))
8079

80+
signature = pkcs1.sign(message, self.priv, 'SHA3-256')
81+
self.assertEqual('SHA3-256', pkcs1.verify(message, signature, self.pub))
82+
8183
def test_find_signature_hash(self):
8284
"""Test happy flow of sign and find_signature_hash"""
8385

0 commit comments

Comments
 (0)