Skip to content

Commit

Permalink
Change default cryptolib to cryptography (#45)
Browse files Browse the repository at this point in the history
Change default cryptography library from rsa to cryptography and update the README.rst accordingly.
  • Loading branch information
roaldnefs authored Mar 17, 2019
1 parent e8dc405 commit 5de6093
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
1 change: 0 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Prerequisite
* Whitelist your IP.
* Generate a new key-pair.
+ Copy-paste the private key into a file.
+ Either reformat the key to PKCS#1 format using ``$ openssl rsa -in input.key -out decrypted_key`` or install cryptography using ``$ pip install cryptography``.
+ Put the private key in a file called ``decrypted_key`` beside this ``README.rst`` file.

Setup
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def find_version(*file_paths):
},
install_requires=[
'requests',
'rsa',
'cryptography',
'suds-jurko',
],
classifiers=[
Expand Down
39 changes: 14 additions & 25 deletions transip/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import uuid
from collections import OrderedDict

import rsa
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from suds.client import Client as SudsClient
from suds.sudsobject import Object as SudsObject
from suds.xsd.doctor import Import, ImportDoctor
Expand All @@ -27,14 +30,6 @@
except ImportError:
suds_requests = None

try:
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
HAS_CRYPTOGRAPHY = True
except ImportError:
HAS_CRYPTOGRAPHY = False

URI_TEMPLATE = 'https://{}/wsdl/?service={}'

Expand Down Expand Up @@ -104,22 +99,16 @@ def _sign(self, message):
else:
raise RuntimeError('The private key does not exist.')

if HAS_CRYPTOGRAPHY:
private_key = serialization.load_pem_private_key(
str.encode(keydata),
password=None,
backend=default_backend()
)
signature = private_key.sign(
str.encode(message),
padding.PKCS1v15(),
hashes.SHA512(),
)
else:
privkey = rsa.PrivateKey.load_pkcs1(keydata)
signature = rsa.sign(
message.encode('utf-8'), privkey, 'SHA-512'
)
private_key = serialization.load_pem_private_key(
str.encode(keydata),
password=None,
backend=default_backend()
)
signature = private_key.sign(
str.encode(message),
padding.PKCS1v15(),
hashes.SHA512(),
)

signature = base64.b64encode(signature)
signature = quote_plus(signature)
Expand Down

0 comments on commit 5de6093

Please sign in to comment.