Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 10ec1a1

Browse files
committed
Converted to pycryptodomex to decouple from Pycrypto
1 parent c58a4f5 commit 10ec1a1

File tree

4 files changed

+39
-39
lines changed

4 files changed

+39
-39
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
requests = 'requests[security]>=2.4.1,<3'
3333

3434
install_requires = [
35-
'pycryptodome==3.4.7',
35+
'pycryptodomex==3.4.7',
3636
'pyjwt==1.3.0',
3737
requests,
3838
'six>=1.8.0'

stream/httpsig/sign.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import base64
22
import six
33

4-
from Crypto.Hash import HMAC
5-
from Crypto.PublicKey import RSA
6-
from Crypto.Signature import PKCS1_v1_5
4+
from Cryptodome.Hash import HMAC
5+
from Cryptodome.PublicKey import RSA
6+
from Cryptodome.Signature import PKCS1_v1_5
77

88
from .utils import *
99

@@ -15,28 +15,28 @@ class Signer(object):
1515
"""
1616
When using an RSA algo, the secret is a PEM-encoded private key.
1717
When using an HMAC algo, the secret is the HMAC signing secret.
18-
18+
1919
Password-protected keyfiles are not supported.
2020
"""
2121
def __init__(self, secret, algorithm=None):
2222
if algorithm is None:
2323
algorithm = DEFAULT_SIGN_ALGORITHM
24-
24+
2525
assert algorithm in ALGORITHMS, "Unknown algorithm"
2626
if isinstance(secret, six.string_types): secret = secret.encode("ascii")
27-
27+
2828
self._rsa = None
2929
self._hash = None
3030
self.sign_algorithm, self.hash_algorithm = algorithm.split('-')
31-
31+
3232
if self.sign_algorithm == 'rsa':
3333
try:
3434
rsa_key = RSA.importKey(secret)
3535
self._rsa = PKCS1_v1_5.new(rsa_key)
3636
self._hash = HASHES[self.hash_algorithm]
3737
except ValueError:
3838
raise HttpSigException("Invalid key.")
39-
39+
4040
elif self.sign_algorithm == 'hmac':
4141
self._hash = HMAC.new(secret, digestmod=HASHES[self.hash_algorithm])
4242

@@ -81,7 +81,7 @@ class HeaderSigner(Signer):
8181
def __init__(self, key_id, secret, algorithm=None, headers=None):
8282
if algorithm is None:
8383
algorithm = DEFAULT_SIGN_ALGORITHM
84-
84+
8585
super(HeaderSigner, self).__init__(secret=secret, algorithm=algorithm)
8686
self.headers = headers or ['date']
8787
self.signature_template = build_signature_template(key_id, algorithm, headers)
@@ -98,9 +98,9 @@ def sign(self, headers, host=None, method=None, path=None):
9898
headers = CaseInsensitiveDict(headers)
9999
required_headers = self.headers or ['date']
100100
signable = generate_message(required_headers, headers, host, method, path)
101-
101+
102102
signature = self._sign(signable)
103103
headers['authorization'] = self.signature_template % signature
104-
104+
105105
return headers
106106

stream/httpsig/utils.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
# Python 2
1212
from urllib2 import parse_http_list
1313

14-
from Crypto.PublicKey import RSA
15-
from Crypto.Hash import SHA, SHA256, SHA512
14+
from Cryptodome.PublicKey import RSA
15+
from Cryptodome.Hash import SHA, SHA256, SHA512
1616

1717
ALGORITHMS = frozenset(['rsa-sha1', 'rsa-sha256', 'rsa-sha512', 'hmac-sha1', 'hmac-sha256', 'hmac-sha512'])
1818
HASHES = {'sha1': SHA,
@@ -42,23 +42,23 @@ def ct_bytes_compare(a, b):
4242
result |= ord(x) ^ ord(y)
4343
else:
4444
result |= x ^ y
45-
45+
4646
return (result == 0)
4747

4848
def generate_message(required_headers, headers, host=None, method=None, path=None):
4949
headers = CaseInsensitiveDict(headers)
50-
50+
5151
if not required_headers:
5252
required_headers = ['date']
53-
53+
5454
signable_list = []
5555
for h in required_headers:
5656
h = h.lower()
5757
if h == '(request-target)':
5858
if not method or not path:
5959
raise Exception('method and path arguments required when using "(request-target)"')
6060
signable_list.append('%s: %s %s' % (h, method.lower(), path))
61-
61+
6262
elif h == 'host':
6363
# 'host' special case due to requests lib restrictions
6464
# 'host' is not available when adding auth so must use a param
@@ -82,33 +82,33 @@ def generate_message(required_headers, headers, host=None, method=None, path=Non
8282
def parse_authorization_header(header):
8383
if not isinstance(header, six.string_types):
8484
header = header.decode("ascii") #HTTP headers cannot be Unicode.
85-
85+
8686
auth = header.split(" ", 1)
8787
if len(auth) > 2:
8888
raise ValueError('Invalid authorization header. (eg. Method key1=value1,key2="value, \"2\"")')
89-
89+
9090
# Split up any args into a dictionary.
9191
values = {}
9292
if len(auth) == 2:
9393
auth_value = auth[1]
9494
if auth_value and len(auth_value):
9595
# This is tricky string magic. Let urllib do it.
9696
fields = parse_http_list(auth_value)
97-
97+
9898
for item in fields:
9999
# Only include keypairs.
100100
if '=' in item:
101101
# Split on the first '=' only.
102102
key, value = item.split('=', 1)
103103
if not (len(key) and len(value)):
104104
continue
105-
105+
106106
# Unquote values, if quoted.
107107
if value[0] == '"':
108108
value = value[1:-1]
109-
109+
110110
values[key] = value
111-
111+
112112
# ("Signature", {"headers": "date", "algorithm": "hmac-sha256", ... })
113113
return (auth[0], CaseInsensitiveDict(values))
114114

stream/httpsig/verify.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
"""
44
import six
55

6-
from Crypto.Hash import HMAC
7-
from Crypto.PublicKey import RSA
8-
from Crypto.Signature import PKCS1_v1_5
6+
from Cryptodome.Hash import HMAC
7+
from Cryptodome.PublicKey import RSA
8+
from Cryptodome.Signature import PKCS1_v1_5
99
from base64 import b64decode
1010

1111
from .sign import Signer
@@ -24,20 +24,20 @@ def _verify(self, data, signature):
2424
`data` is the message to verify
2525
`signature` is a base64-encoded signature to verify against `data`
2626
"""
27-
27+
2828
if isinstance(data, six.string_types): data = data.encode("ascii")
2929
if isinstance(signature, six.string_types): signature = signature.encode("ascii")
30-
30+
3131
if self.sign_algorithm == 'rsa':
3232
h = self._hash.new()
3333
h.update(data)
3434
return self._rsa.verify(h, b64decode(signature))
35-
35+
3636
elif self.sign_algorithm == 'hmac':
3737
h = self._sign_hmac(data)
3838
s = b64decode(signature)
3939
return ct_bytes_compare(h, s)
40-
40+
4141
else:
4242
raise HttpSigException("Unsupported algorithm.")
4343

@@ -49,7 +49,7 @@ class HeaderVerifier(Verifier):
4949
def __init__(self, headers, secret, required_headers=None, method=None, path=None, host=None):
5050
"""
5151
Instantiate a HeaderVerifier object.
52-
52+
5353
:param headers: A dictionary of headers from the HTTP request.
5454
:param secret: The HMAC secret or RSA *public* key.
5555
:param required_headers: Optional. A list of headers required to be present to validate, even if the signature is otherwise valid. Defaults to ['date'].
@@ -58,33 +58,33 @@ def __init__(self, headers, secret, required_headers=None, method=None, path=Non
5858
:param host: Optional. The value to use for the Host header, if not supplied in :param:headers.
5959
"""
6060
required_headers = required_headers or ['date']
61-
61+
6262
auth = parse_authorization_header(headers['authorization'])
6363
if len(auth) == 2:
6464
self.auth_dict = auth[1]
6565
else:
6666
raise HttpSigException("Invalid authorization header.")
67-
67+
6868
self.headers = CaseInsensitiveDict(headers)
6969
self.required_headers = [s.lower() for s in required_headers]
7070
self.method = method
7171
self.path = path
7272
self.host = host
73-
73+
7474
super(HeaderVerifier, self).__init__(secret, algorithm=self.auth_dict['algorithm'])
7575

7676
def verify(self):
7777
"""
7878
Verify the headers based on the arguments passed at creation and current properties.
79-
79+
8080
Raises an Exception if a required header (:param:required_headers) is not found in the signature.
8181
Returns True or False.
8282
"""
8383
auth_headers = self.auth_dict.get('headers', 'date').split(' ')
84-
84+
8585
if len(set(self.required_headers) - set(auth_headers)) > 0:
8686
raise Exception('{} is a required header(s)'.format(', '.join(set(self.required_headers)-set(auth_headers))))
87-
87+
8888
signing_str = generate_message(auth_headers, self.headers, self.host, self.method, self.path)
89-
89+
9090
return self._verify(signing_str, self.auth_dict['signature'])

0 commit comments

Comments
 (0)