Skip to content

Commit

Permalink
implement __hash__() method for Ed25519Key
Browse files Browse the repository at this point in the history
makes Ed25519Key objs comparable, needed for host keys
  • Loading branch information
ploxiln committed Jun 13, 2017
1 parent 517685c commit 1d8bb42
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions paramiko/ed25519key.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ def asbytes(self):
m.add_string(v.encode())
return m.asbytes()

def __hash__(self):
if self.can_sign():
v = self._signing_key.verify_key
else:
v = self._verifying_key
return hash((self.get_name(), v))

def get_name(self):
return "ssh-ed25519"

Expand Down
11 changes: 10 additions & 1 deletion tests/test_pkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,18 @@ def test_ed25519(self):
key2 = Ed25519Key.from_private_key_file(
test_path('test_ed25519_password.key'), b'abc123'
)

self.assertNotEqual(key1.asbytes(), key2.asbytes())

def test_ed25519_compare(self):
# verify that the private & public keys compare equal
key = Ed25519Key.from_private_key_file(test_path('test_ed25519.key'))
self.assertEqual(key, key)
pub = Ed25519Key(data=key.asbytes())
self.assertTrue(key.can_sign())
self.assertTrue(not pub.can_sign())
self.assertEqual(key, pub)


def test_keyfile_is_actually_encrypted(self):
# Read an existing encrypted private key
file_ = test_path('test_rsa_password.key')
Expand Down

0 comments on commit 1d8bb42

Please sign in to comment.