This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
forked from ahknight/httpsig
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from fulder/hs2019-support
Hs2019 support
- Loading branch information
Showing
14 changed files
with
282 additions
and
96 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import base64 | ||
|
||
import six | ||
from Crypto.PublicKey import RSA | ||
from Crypto.Signature import PKCS1_PSS | ||
from httpsig.utils import HttpSigException, HASHES | ||
from abc import ABCMeta, abstractmethod | ||
|
||
DEFAULT_HASH_ALGORITHM = "sha512" | ||
|
||
|
||
class SignAlgorithm(object): | ||
__metaclass__ = ABCMeta | ||
|
||
@abstractmethod | ||
def sign(self, private, data): | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def verify(self, public, data, signature): | ||
raise NotImplementedError() | ||
|
||
|
||
class PSS(SignAlgorithm): | ||
|
||
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)) |
File renamed without changes.
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,27 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIIEogIBAAKCAQB7eXXK+gSpDXsvZkcXd19X85iemJd0KywRH+/W+1J1j8pd+O1l | ||
H2He8GLaDFCwFijTvTmptfMYB2XyvG8/tPpaSzaIbSBlKXWxSo1fdUMf2e7SbqVr | ||
Fi5DolPrIfRpVqw4iqnTZZ46Y2vfa57Ee3NRF5zoagMS9BM7nfuCKvzZcUK81V75 | ||
hup5kpMHW1ofBZAPwQMm8CoXD1bpM+acN1N+63vgTY2QyUq2yJOI3HJvyFZTw+Sj | ||
/ialYtDvDTluBH98i4504OIA6z0SCijF11irvAOSPc0GVXB8HjtUlqbD0BD6Hyqg | ||
MeXgi9nGJhJDnJDiCVlPwg6Ni+h3nW/sXXopAgMBAAECggEANkOg8v2CAtG7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A30ZU | ||
V3DFAoGAfaPc8p6o7tCaPMpRxynIAvgIqg4sIBJdX/G4Q+SZeZR/mFlfpuhY4kzh | ||
CL+RKAhOyOaYsSxlk4vB954y4UZFl6/t2W6gNxouelA77TgV2/rjx/fLk06J+RIF | ||
QQkiAXwUZ2xpmdnUk+UREBwrB3LoU9kZM6fKX/LB4QEZuOmbERQ= | ||
-----END RSA PRIVATE KEY----- |
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,9 @@ | ||
-----BEGIN PUBLIC KEY----- | ||
MIIBITANBgkqhkiG9w0BAQEFAAOCAQ4AMIIBCQKCAQB7eXXK+gSpDXsvZkcXd19X | ||
85iemJd0KywRH+/W+1J1j8pd+O1lH2He8GLaDFCwFijTvTmptfMYB2XyvG8/tPpa | ||
SzaIbSBlKXWxSo1fdUMf2e7SbqVrFi5DolPrIfRpVqw4iqnTZZ46Y2vfa57Ee3NR | ||
F5zoagMS9BM7nfuCKvzZcUK81V75hup5kpMHW1ofBZAPwQMm8CoXD1bpM+acN1N+ | ||
63vgTY2QyUq2yJOI3HJvyFZTw+Sj/ialYtDvDTluBH98i4504OIA6z0SCijF11ir | ||
vAOSPc0GVXB8HjtUlqbD0BD6HyqgMeXgi9nGJhJDnJDiCVlPwg6Ni+h3nW/sXXop | ||
AgMBAAE= | ||
-----END PUBLIC KEY----- |
Oops, something went wrong.