util.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # https://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Utility functions."""
  17. from __future__ import with_statement, print_function
  18. import sys
  19. from optparse import OptionParser
  20. import rsa.key
  21. def private_to_public():
  22. """Reads a private key and outputs the corresponding public key."""
  23. # Parse the CLI options
  24. parser = OptionParser(usage='usage: %prog [options]',
  25. description='Reads a private key and outputs the '
  26. 'corresponding public key. Both private and public keys use '
  27. 'the format described in PKCS#1 v1.5')
  28. parser.add_option('-i', '--input', dest='infilename', type='string',
  29. help='Input filename. Reads from stdin if not specified')
  30. parser.add_option('-o', '--output', dest='outfilename', type='string',
  31. help='Output filename. Writes to stdout of not specified')
  32. parser.add_option('--inform', dest='inform',
  33. help='key format of input - default PEM',
  34. choices=('PEM', 'DER'), default='PEM')
  35. parser.add_option('--outform', dest='outform',
  36. help='key format of output - default PEM',
  37. choices=('PEM', 'DER'), default='PEM')
  38. (cli, cli_args) = parser.parse_args(sys.argv)
  39. # Read the input data
  40. if cli.infilename:
  41. print('Reading private key from %s in %s format' %
  42. (cli.infilename, cli.inform), file=sys.stderr)
  43. with open(cli.infilename, 'rb') as infile:
  44. in_data = infile.read()
  45. else:
  46. print('Reading private key from stdin in %s format' % cli.inform,
  47. file=sys.stderr)
  48. in_data = sys.stdin.read().encode('ascii')
  49. assert type(in_data) == bytes, type(in_data)
  50. # Take the public fields and create a public key
  51. priv_key = rsa.key.PrivateKey.load_pkcs1(in_data, cli.inform)
  52. pub_key = rsa.key.PublicKey(priv_key.n, priv_key.e)
  53. # Save to the output file
  54. out_data = pub_key.save_pkcs1(cli.outform)
  55. if cli.outfilename:
  56. print('Writing public key to %s in %s format' %
  57. (cli.outfilename, cli.outform), file=sys.stderr)
  58. with open(cli.outfilename, 'wb') as outfile:
  59. outfile.write(out_data)
  60. else:
  61. print('Writing public key to stdout in %s format' % cli.outform,
  62. file=sys.stderr)
  63. sys.stdout.write(out_data.decode('ascii'))