From 79ee31c44e7467339ac6406d66c82788a8bed148 Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Fri, 17 Apr 2015 17:57:34 +0500 Subject: [PATCH] Fix two python3 compatibility problems: We need to explicitly encode the serverID before hashing it Pass read/binary modes to the file made from socket to avoid encoding problems, fixes #29 --- minecraft/networking/connection.py | 2 +- minecraft/networking/encryption.py | 2 +- tests/test_encryption.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/minecraft/networking/connection.py b/minecraft/networking/connection.py index 9ef6a54..645d289 100644 --- a/minecraft/networking/connection.py +++ b/minecraft/networking/connection.py @@ -138,7 +138,7 @@ class Connection(object): # the server. self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((self.options.address, self.options.port)) - self.file_object = self.socket.makefile() + self.file_object = self.socket.makefile("rb") def _handshake(self, next_state=2): handshake = packets.HandShakePacket() diff --git a/minecraft/networking/encryption.py b/minecraft/networking/encryption.py index 9a47461..5d463d1 100644 --- a/minecraft/networking/encryption.py +++ b/minecraft/networking/encryption.py @@ -35,7 +35,7 @@ def encrypt_token_and_secret(pubkey, verification_token, shared_secret): def generate_verification_hash(server_id, shared_secret, public_key): verification_hash = sha1() - verification_hash.update(server_id) + verification_hash.update(server_id.encode('utf-8')) verification_hash.update(shared_secret) verification_hash.update(public_key) diff --git a/tests/test_encryption.py b/tests/test_encryption.py index a267b30..9e1b7a0 100644 --- a/tests/test_encryption.py +++ b/tests/test_encryption.py @@ -58,7 +58,7 @@ class Encryption(unittest.TestCase): def test_generate_hash(self): verification_hash = generate_verification_hash( - b"", "secret".encode('utf-8'), self.public_key) + u"", "secret".encode('utf-8'), self.public_key) self.assertEquals("1f142e737a84a974a5f2a22f6174a78d80fd97f5", verification_hash)