KeyPairCreator.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. import argparse
  3. import os
  4. from typing import Tuple
  5. import zmq.auth
  6. # @brief Creates a new keypair for ZMQ encryption
  7. # @param[in] directory The location where to store the keys
  8. # @return The public and private key tuple
  9. def KeyPairCreator(directory: str, server: bool) -> Tuple[str, str]:
  10. if not server:
  11. print('Creating a new pair for a client...')
  12. target = 'client'
  13. else:
  14. print('Creating a new pair for the server...')
  15. target = 'server'
  16. public, private = zmq.auth.create_certificates(directory, target)
  17. return (public, private)
  18. def str2bool(value):
  19. if isinstance(value, bool):
  20. return value
  21. elif value.lower() in ('yes', 'true', 't', 'y', '1'):
  22. return True
  23. elif value.lower() in ('no', 'false', 'f', 'n', '0'):
  24. return False
  25. else:
  26. raise argparse.ArgumentTypeError('Boolean value expected')
  27. if __name__ == '__main__':
  28. # create the commandline parser
  29. parser = argparse.ArgumentParser(description='Create a new key-value pair')
  30. parser.add_argument('directory', help='Directory where to store the key pair')
  31. parser.add_argument('--server', default=False, action='store_true', help="Creates server key pair")
  32. args = parser.parse_args()
  33. # create the directory if it does not exist
  34. if not os.path.exists(args.directory):
  35. os.makedirs(args.directory)
  36. # create the keys
  37. KeyPairCreator(args.directory, args.server)