Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit 0e50795

Browse files
authored
Merge pull request #155 from bif-g/master
replace M2Crypto with py-cryptography
2 parents d9b94b2 + 4b555e6 commit 0e50795

File tree

6 files changed

+23
-19
lines changed

6 files changed

+23
-19
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ A [presentation was made at PyCon 2016][pycon_preso], and here's some demo code:
4646
import os.path as op
4747

4848
from adb import adb_commands
49-
from adb import sign_m2crypto
49+
from adb import sign_cryptography
5050

5151

5252
# KitKat+ devices require authentication
53-
signer = sign_m2crypto.M2CryptoSigner(
53+
signer = sign_cryptography.CryptographySigner(
5454
op.expanduser('~/.android/adbkey'))
5555
# Connect to the device
5656
device = adb_commands.AdbCommands()
@@ -83,7 +83,7 @@ for i in xrange(10):
8383
* libusb1 (1.0.16+)
8484
* python-libusb1 (1.2.0+)
8585
* `adb.zip`: one of:
86-
* python-m2crypto (0.21.1+)
86+
* py-cryptography
8787
* python-rsa (3.2+)
8888
* `fastboot.zip` (optional):
8989
* python-progressbar (2.3+)

adb/adb_commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040

4141
try:
4242
# Imported locally to keep compatibility with previous code.
43-
from adb.sign_m2crypto import M2CryptoSigner
43+
from adb.sign_cryptography import CryptographySigner
4444
except ImportError:
45-
# Ignore this error when M2Crypto is not installed, there are other options.
45+
# Ignore this error when cryptography is not installed, there are other options.
4646
pass
4747

4848

adb/adb_debug.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
from adb import common_cli
2828

2929
try:
30-
from adb import sign_m2crypto
30+
from adb import sign_cryptography
3131

32-
rsa_signer = sign_m2crypto.M2CryptoSigner
32+
rsa_signer = sign_cryptography.CryptographySigner
3333
except ImportError:
3434
try:
3535
from adb import sign_pythonrsa
@@ -187,7 +187,7 @@ def main():
187187
if os.path.isfile(default):
188188
args.rsa_key_path = [default]
189189
if args.rsa_key_path and not rsa_signer:
190-
parser.error('Please install either M2Crypto, python-rsa, or PycryptoDome')
190+
parser.error('Please install either cryptography, python-rsa, or PycryptoDome')
191191

192192
# Hacks so that the generated doc is nicer.
193193
if args.command_name == 'devices':

adb/sign_m2crypto.py renamed to adb/sign_cryptography.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,26 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from M2Crypto import RSA
16-
1715
from adb import adb_protocol
1816

17+
from cryptography.hazmat.backends import default_backend
18+
from cryptography.hazmat.primitives import hashes
19+
from cryptography.hazmat.primitives import serialization
20+
from cryptography.hazmat.primitives.asymmetric import padding
21+
from cryptography.hazmat.primitives.asymmetric import utils
22+
1923

20-
class M2CryptoSigner(adb_protocol.AuthSigner):
21-
"""AuthSigner using M2Crypto."""
24+
class CryptographySigner(adb_protocol.AuthSigner):
25+
"""AuthSigner using cryptography.io."""
2226

2327
def __init__(self, rsa_key_path):
2428
with open(rsa_key_path + '.pub') as rsa_pub_file:
2529
self.public_key = rsa_pub_file.read()
2630

27-
self.rsa_key = RSA.load_key(rsa_key_path)
31+
with open(rsa_key_path) as rsa_prv_file:
32+
self.rsa_key = serialization.load_pem_private_key(
33+
rsa_prv_file.read(), None, default_backend())
2834

2935
def Sign(self, data):
30-
return self.rsa_key.sign(data, 'sha1')
31-
32-
def GetPublicKey(self):
33-
return self.public_key
36+
return self.rsa_key.sign(
37+
data, padding.PKCS1v15(), utils.Prehashed(hashes.SHA1()))

make_tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def main():
4343
z.write('adb/common.py')
4444
z.write('adb/common_cli.py')
4545
z.write('adb/filesync_protocol.py')
46-
z.write('adb/sign_m2crypto.py')
46+
z.write('adb/sign_cryptography.py')
4747
z.write('adb/sign_pythonrsa.py')
4848
z.write('adb/usb_exceptions.py')
4949
with zipfile.ZipFile('fastboot.zip', 'w', zipfile.ZIP_DEFLATED) as z:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from setuptools import setup
1616

1717
# Figure out if the system already has a supported Crypto library
18-
rsa_signer_library = 'M2Crypto>=0.21.1,<=0.26.4'
18+
rsa_signer_library = 'cryptography'
1919
try:
2020
import rsa
2121

0 commit comments

Comments
 (0)