Skip to content

Commit 9b87571

Browse files
committed
fix: #1108 - Replace ecdsa with cryptography
1 parent 7eafe18 commit 9b87571

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ We welcome direct contributions to the sendgrid-python code base. Thank you!
2626

2727
- Python version 2.7, 3.5, 3.6, 3.7, or 3.8
2828
- [python_http_client](https://github.com/sendgrid/python-http-client)
29-
- [ecdsa_python](https://github.com/starkbank/ecdsa-python)
29+
- [cryptography](https://github.com/pyca/cryptography)
3030
- [pyenv](https://github.com/yyuu/pyenv)
3131
- [tox](https://pypi.python.org/pypi/tox)
3232

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pip install sendgrid
7070
## Dependencies
7171

7272
- [Python-HTTP-Client](https://github.com/sendgrid/python-http-client)
73-
- [ECDSA-Python](https://github.com/starkbank/ecdsa-python)
73+
- [Cryptography](https://github.com/pyca/cryptography)
7474

7575

7676
<a name="quick-start"></a>

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Dependencies
9090
------------
9191

9292
- `Python-HTTP-Client`_
93-
- `ECDSA-Python`_
93+
- `Cryptography`_
9494

9595
Quick Start
9696
===========
@@ -259,7 +259,7 @@ License
259259
.. _Twilio account: https://www.twilio.com/try-twilio?source=sendgrid-python
260260
.. _SENDGRID_API_KEY: https://app.sendgrid.com/settings/api_keys
261261
.. _Python-HTTP-Client: https://github.com/sendgrid/python-http-client
262-
.. _ECDSA-Python: https://github.com/starkbank/ecdsa-python
262+
.. _Cryptography: https://github.com/pyca/cryptography
263263
.. _/mail/send Helper: https://github.com/sendgrid/sendgrid-python/tree/HEAD/sendgrid/helpers/mail
264264
.. _personalization object: https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html
265265
.. _Fluent Interface: https://sendgrid.com/blog/using-python-to-implement-a-fluent-interface-to-any-rest-api/

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ Flask==3.1.0
22
PyYAML>=4.2b1
33
python-http-client>=3.2.1
44
six==1.17.0
5-
ecdsa>=0.19.1,<1
5+
cryptography>=45.0.6
66
more-itertools==5.0.0

sendgrid/helpers/eventwebhook/__init__.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from ecdsa import VerifyingKey, BadSignatureError
2-
from ecdsa.util import sigdecode_der
1+
from cryptography.exceptions import InvalidSignature
2+
from cryptography.hazmat.primitives import hashes
3+
from cryptography.hazmat.primitives.asymmetric import ec
4+
from cryptography.hazmat.primitives.serialization import load_pem_public_key
35
import base64
4-
import hashlib
5-
from .eventwebhook_header import EventWebhookHeader
66

77
class EventWebhook:
88
"""
@@ -20,15 +20,15 @@ def __init__(self, public_key=None):
2020

2121
def convert_public_key_to_ecdsa(self, public_key):
2222
"""
23-
Convert the public key string to a VerifyingKey object.
23+
Convert the public key string to a EllipticCurvePublicKey object.
2424
2525
:param public_key: verification key under Mail Settings
2626
:type public_key string
27-
:return: VerifyingKey object using the ECDSA algorithm
28-
:rtype VerifyingKey
27+
:return: EllipticCurvePublicKey object using the ECDSA algorithm
28+
:rtype EllipticCurvePublicKey
2929
"""
3030
pem_key = "-----BEGIN PUBLIC KEY-----\n" + public_key + "\n-----END PUBLIC KEY-----"
31-
return VerifyingKey.from_pem(pem_key)
31+
return load_pem_public_key(pem_key.encode("utf-8"))
3232

3333
def verify_signature(self, payload, signature, timestamp, public_key=None):
3434
"""
@@ -41,15 +41,15 @@ def verify_signature(self, payload, signature, timestamp, public_key=None):
4141
:param timestamp: value obtained from the 'X-Twilio-Email-Event-Webhook-Timestamp' header
4242
:type timestamp: string
4343
:param public_key: elliptic curve public key
44-
:type public_key: VerifyingKey
44+
:type public_key: EllipticCurvePublicKey
4545
:return: true or false if signature is valid
4646
"""
4747
timestamped_payload = (timestamp + payload).encode('utf-8')
4848
decoded_signature = base64.b64decode(signature)
4949

5050
key = public_key or self.public_key
5151
try:
52-
key.verify(decoded_signature, timestamped_payload, hashfunc=hashlib.sha256, sigdecode=sigdecode_der)
52+
key.verify(decoded_signature, timestamped_payload, ec.ECDSA(hashes.SHA256()))
5353
return True
54-
except BadSignatureError:
54+
except InvalidSignature:
5555
return False

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def getRequires():
1111
deps = [
1212
'python_http_client>=3.2.1',
13-
'ecdsa>=0.19.1,<1',
13+
'cryptography>=45.0.6',
1414
"werkzeug>=0.11.15,<1.0.0 ; python_version < '3.0'",
1515
"werkzeug>=0.15.0,<2.0.0 ; python_version >= '3.0' and python_version < '3.7'",
1616
"werkzeug>=0.15.0,<2.3.0 ; python_version >= '3.0' and python_version < '3.8'", # version 2.3.0 dropped support for Python 3.7

0 commit comments

Comments
 (0)