Skip to content

Commit

Permalink
brittspace.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robey Pointer committed Nov 2, 2009
1 parent 71e872e commit e0a9f91
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 103 deletions.
52 changes: 26 additions & 26 deletions paramiko/hostkeys.py
Expand Up @@ -33,7 +33,7 @@ class HostKeyEntry:
"""
Representation of a line in an OpenSSH-style "known hosts" file.
"""

def __init__(self, hostnames=None, key=None):
self.valid = (hostnames is not None) and (key is not None)
self.hostnames = hostnames
Expand Down Expand Up @@ -83,7 +83,7 @@ def to_line(self):
return '%s %s %s\n' % (','.join(self.hostnames), self.key.get_name(),
self.key.get_base64())
return None

def __repr__(self):
return '<HostKeyEntry %r: %r>' % (self.hostnames, self.key)

Expand All @@ -93,31 +93,31 @@ class HostKeys (UserDict.DictMixin):
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
verify server keys during SSH negotiation.
A HostKeys object can be treated like a dict; any dict lookup is equivalent
to calling L{lookup}.
@since: 1.5.3
"""

def __init__(self, filename=None):
"""
Create a new HostKeys object, optionally loading keys from an openssh
style host-key file.
@param filename: filename to load host keys from, or C{None}
@type filename: str
"""
# emulate a dict of { hostname: { keytype: PKey } }
self._entries = []
if filename is not None:
self.load(filename)

def add(self, hostname, keytype, key):
"""
Add a host key entry to the table. Any existing entry for a
C{(hostname, keytype)} pair will be replaced.
@param hostname: the hostname (or IP) to add
@type hostname: str
@param keytype: key type (C{"ssh-rsa"} or C{"ssh-dss"})
Expand All @@ -130,21 +130,21 @@ def add(self, hostname, keytype, key):
e.key = key
return
self._entries.append(HostKeyEntry([hostname], key))

def load(self, filename):
"""
Read a file of known SSH host keys, in the format used by openssh.
This type of file unfortunately doesn't exist on Windows, but on
posix, it will usually be stored in
C{os.path.expanduser("~/.ssh/known_hosts")}.
If this method is called multiple times, the host keys are merged,
not cleared. So multiple calls to C{load} will just call L{add},
replacing any existing entries and adding new ones.
@param filename: name of the file to read host keys from
@type filename: str
@raise IOError: if there was an error reading the file
"""
f = open(filename, 'r')
Expand All @@ -156,19 +156,19 @@ def load(self, filename):
if e is not None:
self._entries.append(e)
f.close()

def save(self, filename):
"""
Save host keys into a file, in the format used by openssh. The order of
keys in the file will be preserved when possible (if these keys were
loaded from a file originally). The single exception is that combined
lines will be split into individual key lines, which is arguably a bug.
@param filename: name of the file to write
@type filename: str
@raise IOError: if there was an error writing the file
@since: 1.6.1
"""
f = open(filename, 'w')
Expand All @@ -183,7 +183,7 @@ def lookup(self, hostname):
Find a hostkey entry for a given hostname or IP. If no entry is found,
C{None} is returned. Otherwise a dictionary of keytype to key is
returned. The keytype will be either C{"ssh-rsa"} or C{"ssh-dss"}.
@param hostname: the hostname (or IP) to lookup
@type hostname: str
@return: keys associated with this host (or C{None})
Expand All @@ -194,13 +194,13 @@ def __init__(self, hostname, entries, hostkeys):
self._hostname = hostname
self._entries = entries
self._hostkeys = hostkeys

def __getitem__(self, key):
for e in self._entries:
if e.key.get_name() == key:
return e.key
raise KeyError(key)

def __setitem__(self, key, val):
for e in self._entries:
if e.key is None:
Expand All @@ -214,7 +214,7 @@ def __setitem__(self, key, val):
e = HostKeyEntry([hostname], val)
self._entries.append(e)
self._hostkeys._entries.append(e)

def keys(self):
return [e.key.get_name() for e in self._entries if e.key is not None]

Expand All @@ -226,12 +226,12 @@ def keys(self):
if len(entries) == 0:
return None
return SubDict(hostname, entries, self)

def check(self, hostname, key):
"""
Return True if the given key is associated with the given hostname
in this dictionary.
@param hostname: hostname (or IP) of the SSH server
@type hostname: str
@param key: the key to check
Expand All @@ -253,13 +253,13 @@ def clear(self):
Remove all host keys from the dictionary.
"""
self._entries = []

def __getitem__(self, key):
ret = self.lookup(key)
if ret is None:
raise KeyError(key)
return ret

def __setitem__(self, hostname, entry):
# don't use this please.
if len(entry) == 0:
Expand All @@ -274,7 +274,7 @@ def __setitem__(self, hostname, entry):
found = True
if not found:
self._entries.append(HostKeyEntry([hostname], entry[key_type]))

def keys(self):
# python 2.4 sets would be nice here.
ret = []
Expand All @@ -294,7 +294,7 @@ def hash_host(hostname, salt=None):
"""
Return a "hashed" form of the hostname, as used by openssh when storing
hashed hostnames in the known_hosts file.
@param hostname: the hostname to hash
@type hostname: str
@param salt: optional salt to use when hashing (must be 20 bytes long)
Expand Down
10 changes: 5 additions & 5 deletions paramiko/rng_posix.py
Expand Up @@ -43,7 +43,7 @@ def open_rng_device(device_path=None):

f = None
g = None

if device_path is None:
device_path = "/dev/urandom"

Expand All @@ -54,7 +54,7 @@ def open_rng_device(device_path=None):
f = open(device_path, "rb", 0)
except EnvironmentError:
raise error("Unable to open /dev/urandom")

# Open a second file descriptor for sanity checking later.
try:
g = open(device_path, "rb", 0)
Expand All @@ -65,17 +65,17 @@ def open_rng_device(device_path=None):
st = os.fstat(f.fileno()) # f
if stat.S_ISREG(st.st_mode) or not stat.S_ISCHR(st.st_mode):
raise error("/dev/urandom is not a character special device")

st = os.fstat(g.fileno()) # g
if stat.S_ISREG(st.st_mode) or not stat.S_ISCHR(st.st_mode):
raise error("/dev/urandom is not a character special device")

# Check that /dev/urandom always returns the number of bytes requested
x = f.read(20)
y = g.read(20)
if len(x) != 20 or len(y) != 20:
raise error("Error reading from /dev/urandom: input truncated")

# Check that different reads return different data
if x == y:
raise error("/dev/urandom is broken; returning identical data: %r == %r" % (x, y))
Expand Down

0 comments on commit e0a9f91

Please sign in to comment.