summaryrefslogtreecommitdiff
path: root/paramiko/hostkeys.py
diff options
context:
space:
mode:
authorJeff Forcier <jeff@bitprophet.org>2018-09-17 15:51:22 -0700
committerJeff Forcier <jeff@bitprophet.org>2018-09-17 15:51:22 -0700
commit4e31612cfbcd7cb333ac54571f11dd5593b15d94 (patch)
treeb9af871f3ee08287229260d26079a6485d0f0273 /paramiko/hostkeys.py
parent025a250d38b0046e06dfacf437a09276a84e2c59 (diff)
downloadparamiko-4e31612cfbcd7cb333ac54571f11dd5593b15d94.tar.gz
Blacken 2.0 with black 18.6b4
Diffstat (limited to 'paramiko/hostkeys.py')
-rw-r--r--paramiko/hostkeys.py55
1 files changed, 30 insertions, 25 deletions
diff --git a/paramiko/hostkeys.py b/paramiko/hostkeys.py
index c873f58b..7faf32d8 100644
--- a/paramiko/hostkeys.py
+++ b/paramiko/hostkeys.py
@@ -33,7 +33,7 @@ from paramiko.ecdsakey import ECDSAKey
from paramiko.ssh_exception import SSHException
-class HostKeys (MutableMapping):
+class HostKeys(MutableMapping):
"""
Representation of an OpenSSH-style "known hosts" file. Host keys can be
read from one or more files, and then individual hosts can be looked up to
@@ -87,10 +87,10 @@ class HostKeys (MutableMapping):
:raises: ``IOError`` -- if there was an error reading the file
"""
- with open(filename, 'r') as f:
+ with open(filename, "r") as f:
for lineno, line in enumerate(f, 1):
line = line.strip()
- if (len(line) == 0) or (line[0] == '#'):
+ if (len(line) == 0) or (line[0] == "#"):
continue
try:
e = HostKeyEntry.from_line(line, lineno)
@@ -117,7 +117,7 @@ class HostKeys (MutableMapping):
.. versionadded:: 1.6.1
"""
- with open(filename, 'w') as f:
+ with open(filename, "w") as f:
for e in self._entries:
line = e.to_line()
if line:
@@ -133,7 +133,8 @@ class HostKeys (MutableMapping):
:return: dict of `str` -> `.PKey` keys associated with this host
(or ``None``)
"""
- class SubDict (MutableMapping):
+
+ class SubDict(MutableMapping):
def __init__(self, hostname, entries, hostkeys):
self._hostname = hostname
self._entries = entries
@@ -175,7 +176,8 @@ class HostKeys (MutableMapping):
def keys(self):
return [
- e.key.get_name() for e in self._entries
+ e.key.get_name()
+ for e in self._entries
if e.key is not None
]
@@ -195,10 +197,10 @@ class HostKeys (MutableMapping):
"""
for h in entry.hostnames:
if (
- h == hostname or
- h.startswith('|1|') and
- not hostname.startswith('|1|') and
- constant_time_bytes_eq(self.hash_host(hostname, h), h)
+ h == hostname
+ or h.startswith("|1|")
+ and not hostname.startswith("|1|")
+ and constant_time_bytes_eq(self.hash_host(hostname, h), h)
):
return True
return False
@@ -294,13 +296,13 @@ class HostKeys (MutableMapping):
if salt is None:
salt = os.urandom(sha1().digest_size)
else:
- if salt.startswith('|1|'):
- salt = salt.split('|')[2]
+ if salt.startswith("|1|"):
+ salt = salt.split("|")[2]
salt = decodebytes(b(salt))
assert len(salt) == sha1().digest_size
hmac = HMAC(salt, b(hostname), sha1).digest()
- hostkey = '|1|%s|%s' % (u(encodebytes(salt)), u(encodebytes(hmac)))
- return hostkey.replace('\n', '')
+ hostkey = "|1|%s|%s" % (u(encodebytes(salt)), u(encodebytes(hmac)))
+ return hostkey.replace("\n", "")
class InvalidHostKey(Exception):
@@ -333,25 +335,27 @@ class HostKeyEntry:
:param str line: a line from an OpenSSH known_hosts file
"""
- log = get_logger('paramiko.hostkeys')
- fields = line.split(' ')
+ log = get_logger("paramiko.hostkeys")
+ fields = line.split(" ")
if len(fields) < 3:
# Bad number of fields
- log.info("Not enough fields found in known_hosts in line %s (%r)" %
- (lineno, line))
+ log.info(
+ "Not enough fields found in known_hosts in line %s (%r)"
+ % (lineno, line)
+ )
return None
fields = fields[:3]
names, keytype, key = fields
- names = names.split(',')
+ names = names.split(",")
# Decide what kind of key we're looking at and create an object
# to hold it accordingly.
try:
key = b(key)
- if keytype == 'ssh-rsa':
+ if keytype == "ssh-rsa":
key = RSAKey(data=decodebytes(key))
- elif keytype == 'ssh-dss':
+ elif keytype == "ssh-dss":
key = DSSKey(data=decodebytes(key))
elif keytype in ECDSAKey.supported_key_format_identifiers():
key = ECDSAKey(data=decodebytes(key), validate_point=False)
@@ -371,11 +375,12 @@ class HostKeyEntry:
included.
"""
if self.valid:
- return '%s %s %s\n' % (
- ','.join(self.hostnames),
+ return "%s %s %s\n" % (
+ ",".join(self.hostnames),
self.key.get_name(),
- self.key.get_base64())
+ self.key.get_base64(),
+ )
return None
def __repr__(self):
- return '<HostKeyEntry %r: %r>' % (self.hostnames, self.key)
+ return "<HostKeyEntry %r: %r>" % (self.hostnames, self.key)