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

Commit 2a33c39

Browse files
committed
Add pyadb keygen command to generate keypairs
Address (#95 and #131)
1 parent e1e5840 commit 2a33c39

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

adb/adb_debug.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from adb import adb_commands
2727
from adb import common_cli
28+
from adb import android_pubkey
2829

2930
try:
3031
from adb import sign_m2crypto
@@ -149,6 +150,14 @@ def main():
149150
subparser.add_argument(
150151
'--output_port_path', action='store_true',
151152
help='Outputs the port_path alongside the serial')
153+
subparser = subparsers.add_parser(
154+
name='keygen', help='generate adb public/private key '
155+
'private key stored in {filepath} '
156+
'public key stored in {filepath}.pub '
157+
'(existing files overwritten)')
158+
subparser.add_argument(
159+
'filepath',
160+
help='File path to write the private/public keypair (existing files overwritten)')
152161

153162
common_cli.MakeSubparser(
154163
subparsers, parents, adb_commands.AdbCommands.Install)
@@ -195,6 +204,8 @@ def main():
195204
if args.command_name == 'help':
196205
parser.print_help()
197206
return 0
207+
if args.command_name == 'keygen':
208+
return android_pubkey.keygen(args.filepath)
198209
if args.command_name == 'logcat':
199210
args.positional = args.options
200211
elif args.command_name == 'shell':

adb/android_pubkey.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,19 @@ def write_public_keyfile(private_key_path, public_key_path):
139139
with open(public_key_path, 'wb') as public_key_file:
140140
public_key_file.write(base64.b64encode(public_key))
141141
public_key_file.write(get_user_info().encode())
142+
143+
144+
def keygen(filepath):
145+
"""generate adb public/private key
146+
private key stored in {filepath}
147+
public key stored in {filepath}.pub
148+
(existing files overwritten)
149+
150+
Args:
151+
filepath: File path to write the private/public keypair
152+
"""
153+
key = Crypto.PublicKey.RSA.generate(2048)
154+
with open(filepath, 'wb') as private_key_file:
155+
private_key_file.write(key.export_key(format='PEM', pkcs=8))
156+
157+
write_public_keyfile(filepath, filepath + '.pub')

0 commit comments

Comments
 (0)