Skip to content

Commit 9388f9b

Browse files
committed
Fixed compatibilty of pyrsa-priv2pub with Python 3
1 parent 2707f29 commit 9388f9b

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

rsa/util.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
'''Utility functions.'''
1818

19-
from __future__ import with_statement
19+
from __future__ import with_statement, print_function
2020

2121
import sys
2222
from optparse import OptionParser
@@ -49,14 +49,16 @@ def private_to_public():
4949

5050
# Read the input data
5151
if cli.infilename:
52-
print >>sys.stderr, 'Reading private key from %s in %s format' % \
53-
(cli.infilename, cli.inform)
54-
with open(cli.infilename) as infile:
52+
print('Reading private key from %s in %s format' % \
53+
(cli.infilename, cli.inform), file=sys.stderr)
54+
with open(cli.infilename, 'rb') as infile:
5555
in_data = infile.read()
5656
else:
57-
print >>sys.stderr, 'Reading private key from stdin in %s format' % \
58-
cli.inform
59-
in_data = sys.stdin.read()
57+
print('Reading private key from stdin in %s format' % cli.inform,
58+
file=sys.stderr)
59+
in_data = sys.stdin.read().encode('ascii')
60+
61+
assert type(in_data) == bytes, type(in_data)
6062

6163

6264
# Take the public fields and create a public key
@@ -67,13 +69,13 @@ def private_to_public():
6769
out_data = pub_key.save_pkcs1(cli.outform)
6870

6971
if cli.outfilename:
70-
print >>sys.stderr, 'Writing public key to %s in %s format' % \
71-
(cli.outfilename, cli.outform)
72-
with open(cli.outfilename, 'w') as outfile:
72+
print('Writing public key to %s in %s format' % \
73+
(cli.outfilename, cli.outform), file=sys.stderr)
74+
with open(cli.outfilename, 'wb') as outfile:
7375
outfile.write(out_data)
7476
else:
75-
print >>sys.stderr, 'Writing public key to stdout in %s format' % \
76-
cli.outform
77-
sys.stdout.write(out_data)
77+
print('Writing public key to stdout in %s format' % cli.outform,
78+
file=sys.stderr)
79+
sys.stdout.write(out_data.decode('ascii'))
7880

7981

0 commit comments

Comments
 (0)