This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
forked from fulder/python-httpsig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create sign_algorithms with first PSS class
- Loading branch information
Showing
6 changed files
with
131 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import base64 | ||
|
||
import six | ||
from Crypto.PublicKey import RSA | ||
from Crypto.Signature import PKCS1_PSS | ||
from httpsig.utils import HttpSigException, HASHES | ||
|
||
DEFAULT_HASH_ALGORITHM = "sha512" | ||
|
||
|
||
class PSS(object): | ||
|
||
def __init__(self, hash_algorithm=DEFAULT_HASH_ALGORITHM, salt_length=None, mgfunc=None): | ||
if hash_algorithm not in HASHES: | ||
raise HttpSigException("Unsupported hash algorithm") | ||
|
||
if hash_algorithm != DEFAULT_HASH_ALGORITHM: | ||
raise HttpSigException( | ||
"Hash algorithm: {} is deprecated. Please use: {}".format(hash_algorithm, DEFAULT_HASH_ALGORITHM)) | ||
|
||
self.hash_algorithm = HASHES[hash_algorithm] | ||
self.salt_length = salt_length | ||
self.mgfunc = mgfunc | ||
|
||
def _create_pss(self, key): | ||
try: | ||
rsa_key = RSA.importKey(key) | ||
pss = PKCS1_PSS.new(rsa_key, saltLen=self.salt_length, mgfunc=self.mgfunc) | ||
except ValueError: | ||
raise HttpSigException("Invalid key.") | ||
return pss | ||
|
||
def sign(self, private_key, data): | ||
pss = self._create_pss(private_key) | ||
|
||
if isinstance(data, six.string_types): | ||
data = data.encode("ascii") | ||
|
||
h = self.hash_algorithm.new() | ||
h.update(data) | ||
return pss.sign(h) | ||
|
||
def verify(self, public_key, data, signature): | ||
pss = self._create_pss(public_key) | ||
|
||
h = self.hash_algorithm.new() | ||
h.update(data) | ||
return pss.verify(h, base64.b64decode(signature)) | ||
|
||
|
||
SIGN_ALGORITHMS = frozenset([ | ||
"PSS" | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.