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

Commit 39dc3fe

Browse files
committed
Merge pull request #14 from google/clean_rsa
Separate PythonRSASigner's second constructor path
2 parents ebf8817 + f8cde27 commit 39dc3fe

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

adb/adb_debug.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@
2727

2828
try:
2929
import sign_m2crypto
30+
rsa_signer = sign_m2crypto.M2CryptoSigner
3031
except ImportError:
31-
sign_m2crypto = None
32-
try:
33-
import sign_pythonrsa
34-
except ImportError:
35-
sign_pythonrsa = None
32+
try:
33+
import sign_pythonrsa
34+
rsa_signer = sign_pythonrsa.PythonRSASigner.FromRSAKeyPath
35+
except ImportError:
36+
rsa_signer = None
3637

3738

3839
gflags.ADOPT_module_key_flags(common_cli)
@@ -47,14 +48,11 @@
4748

4849
def GetRSAKwargs():
4950
if FLAGS.rsa_key_path:
50-
if not sign_m2crypto and not sign_pythonrsa:
51+
if rsa_signer is None:
5152
print >> sys.stderr, 'Please install either M2Crypto or python-rsa'
5253
sys.exit(1)
53-
signer = (
54-
sign_m2crypto.M2CryptoSigner if sign_m2crypto
55-
else sign_pythonrsa.PythonRSASigner)
5654
return {
57-
'rsa_keys': [signer(os.path.expanduser(path))
55+
'rsa_keys': [rsa_signer(os.path.expanduser(path))
5856
for path in FLAGS.rsa_key_path],
5957
'auth_timeout_ms': int(FLAGS.auth_timeout_s * 1000.0),
6058
}

adb/sign_pythonrsa.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,15 @@ def _load_rsa_private_key(pem):
5353

5454
class PythonRSASigner(object):
5555
"""Implements adb_protocol.AuthSigner using http://stuvel.eu/rsa."""
56-
def __init__(self, rsa_key_path=None, pub=None, priv=None):
57-
if rsa_key_path:
58-
with open(rsa_key_path + '.pub') as f:
59-
pub = f.read()
60-
with open(rsa_key_path) as f:
61-
priv = f.read()
56+
@classmethod
57+
def FromRSAKeyPath(cls, rsa_key_path):
58+
with open(rsa_key_path + '.pub') as f:
59+
pub = f.read()
60+
with open(rsa_key_path) as f:
61+
priv = f.read()
62+
return cls(pub, priv)
63+
64+
def __init__(self, pub=None, priv=None):
6265
self.priv_key = _load_rsa_private_key(priv)
6366
self.pub_key = pub
6467

0 commit comments

Comments
 (0)