sign_pycryptodome.py 731 B

12345678910111213141516171819202122232425
  1. from adb import adb_protocol
  2. from Crypto.Hash import SHA256
  3. from Crypto.PublicKey import RSA
  4. from Crypto.Signature import pkcs1_15
  5. class PycryptodomeAuthSigner(adb_protocol.AuthSigner):
  6. def __init__(self, rsa_key_path=None):
  7. super(PycryptodomeAuthSigner, self).__init__()
  8. if rsa_key_path:
  9. with open(rsa_key_path + '.pub', 'rb') as rsa_pub_file:
  10. self.public_key = rsa_pub_file.read()
  11. with open(rsa_key_path, 'rb') as rsa_priv_file:
  12. self.rsa_key = RSA.import_key(rsa_priv_file.read())
  13. def Sign(self, data):
  14. h = SHA256.new(data)
  15. return pkcs1_15.new(self.rsa_key).sign(h)
  16. def GetPublicKey(self):
  17. return self.public_key