mirror of
https://github.com/ammaraskar/pyCraft.git
synced 2025-03-18 01:19:30 +01:00
Added an easy encryption test
This commit is contained in:
parent
b37c095039
commit
4fe5caa7d9
@ -41,10 +41,14 @@ def generate_verification_hash(server_id, shared_secret, public_key):
|
||||
verification_hash.update(shared_secret)
|
||||
verification_hash.update(public_key)
|
||||
|
||||
return minecraft_sha1_hash_digest(verification_hash)
|
||||
|
||||
|
||||
def minecraft_sha1_hash_digest(sha1_hash):
|
||||
# Minecraft first parses the sha1 bytes as a signed number and then spits outs
|
||||
# its hex representation
|
||||
number = _number_from_bytes(verification_hash.digest(), signed=True)
|
||||
return format(number, 'x')
|
||||
number_representation = _number_from_bytes(sha1_hash.digest(), signed=True)
|
||||
return format(number_representation, 'x')
|
||||
|
||||
|
||||
def _number_from_bytes(b, signed=False):
|
||||
|
@ -80,27 +80,27 @@ class Integer(Type):
|
||||
|
||||
class VarInt(Type):
|
||||
@staticmethod
|
||||
def read_socket(s):
|
||||
d = 0
|
||||
def read_socket(socket):
|
||||
number = 0
|
||||
for i in range(5):
|
||||
b = s.recv(1)
|
||||
if b == "":
|
||||
byte = socket.recv(1)
|
||||
if byte == "":
|
||||
raise RuntimeError("Socket disconnected")
|
||||
b = ord(b)
|
||||
d |= (b & 0x7F) << 7 * i
|
||||
if not b & 0x80:
|
||||
byte = ord(byte)
|
||||
number |= (byte & 0x7F) << 7 * i
|
||||
if not byte & 0x80:
|
||||
break
|
||||
return d
|
||||
return number
|
||||
|
||||
@staticmethod
|
||||
def read(file_object):
|
||||
d = 0
|
||||
number = 0
|
||||
for i in range(5):
|
||||
b = ord(file_object.read(1))
|
||||
d |= (b & 0x7F) << 7 * i
|
||||
if not b & 0x80:
|
||||
byte = ord(file_object.read(1))
|
||||
number |= (byte & 0x7F) << 7 * i
|
||||
if not byte & 0x80:
|
||||
break
|
||||
return d
|
||||
return number
|
||||
|
||||
@staticmethod
|
||||
def send(value, socket):
|
||||
@ -115,12 +115,12 @@ class VarInt(Type):
|
||||
|
||||
@staticmethod
|
||||
def size(value):
|
||||
for max_value, size in VarInt_size_table.iteritems():
|
||||
for max_value, size in VARINT_SIZE_TABLE.iteritems():
|
||||
if value < max_value:
|
||||
return size
|
||||
|
||||
# Maps (maximum integer value -> size of VarInt in bytes)
|
||||
VarInt_size_table = {
|
||||
VARINT_SIZE_TABLE = {
|
||||
2**7: 1,
|
||||
2**14: 2,
|
||||
2**21: 3,
|
||||
|
17
tests/test_encryption.py
Normal file
17
tests/test_encryption.py
Normal file
@ -0,0 +1,17 @@
|
||||
import unittest
|
||||
import hashlib
|
||||
from minecraft.networking.encryption import minecraft_sha1_hash_digest
|
||||
|
||||
|
||||
class Hashing(unittest.TestCase):
|
||||
|
||||
test_data = {'Notch': '4ed1f46bbe04bc756bcb17c0c7ce3e4632f06a48',
|
||||
'jeb_': '-7c9d5b0044c130109a5d7b5fb5c317c02b4e28c1',
|
||||
'simon': '88e16a1019277b15d58faf0541e11910eb756f6'}
|
||||
|
||||
def test_hashing(self):
|
||||
for input_value in self.test_data.iterkeys():
|
||||
sha1_hash = hashlib.sha1()
|
||||
sha1_hash.update(input_value)
|
||||
self.assertEquals(minecraft_sha1_hash_digest(sha1_hash), self.test_data[input_value])
|
||||
|
Loading…
Reference in New Issue
Block a user