summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rsa/util.py28
1 files changed, 15 insertions, 13 deletions
diff --git a/rsa/util.py b/rsa/util.py
index 307bda5..5bbb70b 100644
--- a/rsa/util.py
+++ b/rsa/util.py
@@ -16,7 +16,7 @@
'''Utility functions.'''
-from __future__ import with_statement
+from __future__ import with_statement, print_function
import sys
from optparse import OptionParser
@@ -49,14 +49,16 @@ def private_to_public():
# Read the input data
if cli.infilename:
- print >>sys.stderr, 'Reading private key from %s in %s format' % \
- (cli.infilename, cli.inform)
- with open(cli.infilename) as infile:
+ print('Reading private key from %s in %s format' % \
+ (cli.infilename, cli.inform), file=sys.stderr)
+ with open(cli.infilename, 'rb') as infile:
in_data = infile.read()
else:
- print >>sys.stderr, 'Reading private key from stdin in %s format' % \
- cli.inform
- in_data = sys.stdin.read()
+ print('Reading private key from stdin in %s format' % cli.inform,
+ file=sys.stderr)
+ in_data = sys.stdin.read().encode('ascii')
+
+ assert type(in_data) == bytes, type(in_data)
# Take the public fields and create a public key
@@ -67,13 +69,13 @@ def private_to_public():
out_data = pub_key.save_pkcs1(cli.outform)
if cli.outfilename:
- print >>sys.stderr, 'Writing public key to %s in %s format' % \
- (cli.outfilename, cli.outform)
- with open(cli.outfilename, 'w') as outfile:
+ print('Writing public key to %s in %s format' % \
+ (cli.outfilename, cli.outform), file=sys.stderr)
+ with open(cli.outfilename, 'wb') as outfile:
outfile.write(out_data)
else:
- print >>sys.stderr, 'Writing public key to stdout in %s format' % \
- cli.outform
- sys.stdout.write(out_data)
+ print('Writing public key to stdout in %s format' % cli.outform,
+ file=sys.stderr)
+ sys.stdout.write(out_data.decode('ascii'))