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

Commit d2be3fb

Browse files
authored
Merge pull request #5 from fulder/default-to-hash-salt-length
Default to hash length for salt in PSS
2 parents c6951c2 + f517f39 commit d2be3fb

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

httpsig/sign_algorithms.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,37 @@ def __init__(self, hash_algorithm=DEFAULT_HASH_ALGORITHM, salt_length=None, mgfu
3535
self.salt_length = salt_length
3636
self.mgfunc = mgfunc
3737

38-
def _create_pss(self, key):
38+
def _create_pss(self, key, salt_length):
3939
try:
4040
rsa_key = RSA.importKey(key)
41-
pss = PKCS1_PSS.new(rsa_key, saltLen=self.salt_length, mgfunc=self.mgfunc)
41+
pss = PKCS1_PSS.new(rsa_key, saltLen=salt_length, mgfunc=self.mgfunc)
4242
except ValueError:
4343
raise HttpSigException("Invalid key.")
4444
return pss
4545

4646
def sign(self, private_key, data):
47-
pss = self._create_pss(private_key)
48-
4947
if isinstance(data, six.string_types):
5048
data = data.encode("ascii")
5149

5250
h = self.hash_algorithm.new()
5351
h.update(data)
52+
53+
salt_length = self.salt_length
54+
if salt_length is None:
55+
salt_length = h.digest_size
56+
57+
pss = self._create_pss(private_key, salt_length)
58+
5459
return pss.sign(h)
5560

5661
def verify(self, public_key, data, signature):
57-
pss = self._create_pss(public_key)
58-
5962
h = self.hash_algorithm.new()
6063
h.update(data)
64+
65+
salt_length = self.salt_length
66+
if salt_length is None:
67+
salt_length = h.digest_size
68+
69+
pss = self._create_pss(public_key, salt_length)
70+
6171
return pss.verify(h, base64.b64decode(signature))

0 commit comments

Comments
 (0)