Skip to content
This repository was archived by the owner on Apr 13, 2024. It is now read-only.

Commit e2e2c75

Browse files
author
David Sutton
committed
Call _get_key only if using RSA.
1 parent 7af3958 commit e2e2c75

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

http_signature/sign.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,31 @@ def __init__(self, secret='~/.ssh/id_rsa', algorithm='rsa-sha256'):
2222
self._rsa = False
2323
self._hash = None
2424
self.sign_algorithm, self.hash_algorithm = algorithm.split('-')
25-
self._get_key(secret)
25+
if self.sign_algorithm == 'rsa':
26+
self._rsa = self._get_key(secret)
27+
self._hash = HASHES[self.hash_algorithm]
28+
elif self.sign_algorithm == 'hmac':
29+
self._hash = HMAC.new(secret, digestmod=HASHES[self.hash_algorithm])
2630

2731
@property
2832
def algorithm(self):
2933
return '%s-%s' % (self.sign_algorithm, self.hash_algorithm)
3034

3135
def _get_key(self, secret):
32-
if self.sign_algorithm == 'rsa':
33-
if (secret.startswith('-----BEGIN RSA PRIVATE KEY-----') or
34-
secret.startswith('-----BEGIN PRIVATE KEY-----')):
35-
# string with PEM encoded key data
36-
k = secret
37-
else:
38-
# file with key data
39-
with open(expanduser(secret)) as fh:
40-
k = fh.read()
41-
try:
42-
rsa_key = RSA.importKey(k)
43-
except ValueError:
44-
pw = getpass('RSA SSH Key Password: ')
45-
rsa_key = RSA.importKey(k, pw)
46-
self._rsa = PKCS1_v1_5.new(rsa_key)
47-
self._hash = HASHES[self.hash_algorithm]
48-
elif self.sign_algorithm == 'hmac':
49-
self._hash = HMAC.new(secret, digestmod=HASHES[self.hash_algorithm])
50-
51-
return ""
36+
if (secret.startswith('-----BEGIN RSA PRIVATE KEY-----') or
37+
secret.startswith('-----BEGIN PRIVATE KEY-----')):
38+
# string with PEM encoded key data
39+
k = secret
40+
else:
41+
# file with key data
42+
with open(expanduser(secret)) as fh:
43+
k = fh.read()
44+
try:
45+
rsa_key = RSA.importKey(k)
46+
except ValueError:
47+
pw = getpass('RSA SSH Key Password: ')
48+
rsa_key = RSA.importKey(k, pw)
49+
return PKCS1_v1_5.new(rsa_key)
5250

5351
def sign_rsa(self, sign_string):
5452
h = self._hash.new()

0 commit comments

Comments
 (0)