Skip to content
This repository was archived by the owner on Apr 13, 2024. It is now read-only.

Commit 68b7369

Browse files
committed
Test creating superclass for sign algorithm
1 parent c4e36fb commit 68b7369

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

httpsig/sign.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from Crypto.Hash import HMAC
66
from Crypto.PublicKey import RSA
77
from Crypto.Signature import PKCS1_v1_5
8-
from .sign_algorithms import SIGN_ALGORITHMS
8+
from .sign_algorithms import SignAlgorithm
99
from .utils import *
1010

1111
DEFAULT_SIGN_ALGORITHM = "hs2019"
@@ -19,15 +19,12 @@ class Signer(object):
1919
Password-protected keyfiles are not supported.
2020
"""
2121

22-
def __init__(self, secret, algorithm=None, sign_algorithm=None):
22+
def __init__(self, secret, algorithm=None, sign_algorithm: SignAlgorithm=None):
2323
if algorithm is None:
2424
algorithm = DEFAULT_SIGN_ALGORITHM
2525

2626
assert algorithm in ALGORITHMS, "Unknown algorithm"
2727

28-
if sign_algorithm is not None and sign_algorithm.__class__.__name__ not in SIGN_ALGORITHMS:
29-
raise HttpSigException("Unsupported digital signature algorithm")
30-
3128
if algorithm != DEFAULT_SIGN_ALGORITHM:
3229
print("Algorithm: {} is deprecated please update to {}".format(algorithm, DEFAULT_SIGN_ALGORITHM))
3330

@@ -79,7 +76,7 @@ def sign(self, data):
7976
signed = self._sign_rsa(data)
8077
elif self._hash:
8178
signed = self._sign_hmac(data)
82-
elif self.sign_algorithm.__class__.__name__ in SIGN_ALGORITHMS:
79+
elif isinstance(self.sign_algorithm, SignAlgorithm):
8380
signed = self.sign_algorithm.sign(self.secret, data)
8481
if not signed:
8582
raise SystemError('No valid encryptor found.')
@@ -98,7 +95,6 @@ class HeaderSigner(Signer):
9895
match the algorithm)
9996
:param algorithm: one of the seven specified algorithms
10097
:param sign_algorithm: required for 'hs2019' algorithm. Sign algorithm for the secret
101-
:param sign_algorithm: Custom salt length for 'hs2019' and 'PSS' sign algorithm.
10298
:param headers: a list of http headers to be included in the signing
10399
string, defaulting to ['date'].
104100
:param sign_header: header used to include signature, defaulting to

httpsig/sign_algorithms.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44
from Crypto.PublicKey import RSA
55
from Crypto.Signature import PKCS1_PSS
66
from httpsig.utils import HttpSigException, HASHES
7+
from abc import ABCMeta, abstractmethod
78

89
DEFAULT_HASH_ALGORITHM = "sha512"
910

1011

11-
class PSS(object):
12+
class SignAlgorithm(object):
13+
__metaclass__ = ABCMeta
14+
15+
@abstractmethod
16+
def sign(self, *args):
17+
raise NotImplementedError()
18+
19+
@abstractmethod
20+
def verify(self, *args):
21+
raise NotImplementedError()
22+
23+
24+
class PSS(SignAlgorithm):
1225

1326
def __init__(self, hash_algorithm=DEFAULT_HASH_ALGORITHM, salt_length=None, mgfunc=None):
1427
if hash_algorithm not in HASHES:
@@ -46,8 +59,3 @@ def verify(self, public_key, data, signature):
4659
h = self.hash_algorithm.new()
4760
h.update(data)
4861
return pss.verify(h, base64.b64decode(signature))
49-
50-
51-
SIGN_ALGORITHMS = frozenset([
52-
"PSS"
53-
])

httpsig/verify.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import six
66

77
from .sign import Signer, DEFAULT_SIGN_ALGORITHM
8-
from .sign_algorithms import SIGN_ALGORITHMS
8+
from .sign_algorithms import SignAlgorithm
99
from .utils import *
1010

1111

@@ -38,7 +38,7 @@ def _verify(self, data, signature):
3838
s = base64.b64decode(signature)
3939
return ct_bytes_compare(h, s)
4040

41-
elif self.sign_algorithm.__class__.__name__ in SIGN_ALGORITHMS:
41+
elif isinstance(self.sign_algorithm, SignAlgorithm):
4242
return self.sign_algorithm.verify(self.secret, data, signature)
4343

4444
else:
@@ -72,7 +72,6 @@ def __init__(self, headers, secret, required_headers=None, method=None,
7272
Default is 'authorization'.
7373
:param sign_algorithm: Required for 'hs2019' algorithm, specifies the
7474
digital signature algorithm (derived from keyId) to use.
75-
:param sign_algorithm: Custom salt length for 'hs2019' and 'PSS' sign algorithm.
7675
"""
7776
required_headers = required_headers or ['date']
7877
self.headers = CaseInsensitiveDict(headers)

0 commit comments

Comments
 (0)