Fix hexdigest properly

Chat once again works in terminal
Moved stuff around to appropriate locations
This commit is contained in:
Ammar Askar 2012-10-09 20:16:37 +05:00
parent 9b6a195b6a
commit 2bb374c153
3 changed files with 50 additions and 74 deletions

View File

@ -1,31 +1,42 @@
import array
import re
from hashlib import sha1
def stringToByteArray(string):
return array.array('B', string.decode("hex"))
# This function courtesy of barneygale
def javaHexDigest(digest):
d = long(digest.hexdigest(), 16)
if d >> 39*4 & 0x8:
d = "-%x" % ((-d) & (2**(40*4)-1))
else:
d = "%x" % d
return d
def TwosCompliment(digest):
carry = True
for i in range((digest.__len__() - 1), -1, -1):
value = 255 - digest[i]
digest[i] = value
if(carry):
carry = digest[i] == 0xFF
digest[i] = digest[i] + 1
return digest
def translate_escape(m):
c = m.group(1).lower()
if c == "0": return "\x1b[30m\x1b[21m" # black
elif c == "1": return "\x1b[34m\x1b[21m" # dark blue
elif c == "2": return "\x1b[32m\x1b[21m" # dark green
elif c == "3": return "\x1b[36m\x1b[21m" # dark cyan
elif c == "4": return "\x1b[31m\x1b[21m" # dark red
elif c == "5": return "\x1b[35m\x1b[21m" # purple
elif c == "6": return "\x1b[33m\x1b[21m" # gold
elif c == "7": return "\x1b[37m\x1b[21m" # gray
elif c == "8": return "\x1b[30m\x1b[1m" # dark gray
elif c == "9": return "\x1b[34m\x1b[1m" # blue
elif c == "a": return "\x1b[32m\x1b[1m" # bright green
elif c == "b": return "\x1b[36m\x1b[1m" # cyan
elif c == "c": return "\x1b[31m\x1b[1m" # red
elif c == "d": return "\x1b[35m\x1b[1m" # pink
elif c == "e": return "\x1b[33m\x1b[1m" # yellow
elif c == "f": return "\x1b[37m\x1b[1m" # white
elif c == "k": return "\x1b[5m" # random
elif c == "l": return "\x1b[1m" # bold
elif c == "m": return "\x1b[9m" # strikethrough (escape code not widely supported)
elif c == "n": return "\x1b[4m" # underline
elif c == "o": return "\x1b[3m" # italic (escape code not widely supported)
elif c == "r": return "\x1b[0m" # reset
return ""
def trimStart(string, character):
for c in string:
if (c == character):
string = string[1:]
else:
break
return string
def getHexString(byteArray):
result = ""
for i in range(byteArray.__len__()):
if (byteArray[i] < 0x10):
result += '0'
result += hex(byteArray[i])[2:]
return result
def translate_escapes(s):
return re.sub(ur"\xa7([0-9a-zA-Z])", translate_escape, s) + "\x1b[0m"

View File

@ -7,7 +7,6 @@ import hashlib
import string
import Utils
import sys
import re
from networking import PacketSenderManager
from Crypto.Random import _UserFriendlyRNG
from Crypto.Util import asn1
@ -23,41 +22,11 @@ except ImportError:
EntityID = 0
def translate_escape(m):
c = m.group(1).lower()
if c == "0": return "\x1b[30m\x1b[21m" # black
elif c == "1": return "\x1b[34m\x1b[21m" # dark blue
elif c == "2": return "\x1b[32m\x1b[21m" # dark green
elif c == "3": return "\x1b[36m\x1b[21m" # dark cyan
elif c == "4": return "\x1b[31m\x1b[21m" # dark red
elif c == "5": return "\x1b[35m\x1b[21m" # purple
elif c == "6": return "\x1b[33m\x1b[21m" # gold
elif c == "7": return "\x1b[37m\x1b[21m" # gray
elif c == "8": return "\x1b[30m\x1b[1m" # dark gray
elif c == "9": return "\x1b[34m\x1b[1m" # blue
elif c == "a": return "\x1b[32m\x1b[1m" # bright green
elif c == "b": return "\x1b[36m\x1b[1m" # cyan
elif c == "c": return "\x1b[31m\x1b[1m" # red
elif c == "d": return "\x1b[35m\x1b[1m" # pink
elif c == "e": return "\x1b[33m\x1b[1m" # yellow
elif c == "f": return "\x1b[37m\x1b[1m" # white
elif c == "k": return "\x1b[5m" # random
elif c == "l": return "\x1b[1m" # bold
elif c == "m": return "\x1b[9m" # strikethrough (escape code not widely supported)
elif c == "n": return "\x1b[4m" # underline
elif c == "o": return "\x1b[3m" # italic (escape code not widely supported)
elif c == "r": return "\x1b[0m" # reset
return ""
def translate_escapes(s):
return re.sub(ur"\xa7([0-9a-zA-Z])", translate_escape, s) + "\x1b[0m"
class ServerConnection(threading.Thread):
def __init__(self, window, username, password, sessionID, server, port):
threading.Thread.__init__(self)
self.isConnected = False
self.username = username
self.password = password
self.sessionID = sessionID
@ -77,7 +46,7 @@ class ServerConnection(threading.Thread):
self.window = window
def grabSocket(self):
return self.socket
return self.socket
def run(self):
try:
@ -115,17 +84,8 @@ class ServerConnection(threading.Thread):
sha1.update(packetFD['ServerID'])
sha1.update(self.sharedSecret)
sha1.update(packetFD['Public Key'])
#lovely java style hex digest by SirCmpwn
sha1 = sha1.hexdigest()
negative = (int(sha1[0], 16) & 0x80) == 0x80
if(negative):
sha1 = Utils.TwosCompliment(sha1.digest())
#else:
# sha1 = sha1.digest()
Utils.trimStart(str(sha1), '0')
if (negative):
sha1 = '-' + sha1
serverid = sha1
#lovely java style hex digest by barneygale
serverid = Utils.javaHexDigest(sha1)
#Authenticate the server from sessions.minecraft.net
if(serverid != '-'):
@ -261,7 +221,7 @@ class PacketListener(threading.Thread):
message = PacketListenerManager.handle03(self.FileObject)
if(self.connection.NoGUI):
# Add "\x1b" because it is essential for ANSI escapes emitted by translate_escapes
filtered_string = filter(lambda x: x in string.printable + "\x1b", translate_escapes(message))
filtered_string = filter(lambda x: x in string.printable + "\x1b", Utils.translate_escapes(message))
#print message.replace(u'\xa7', '&')
print filtered_string
elif(self.window):
@ -382,6 +342,7 @@ class PacketListener(threading.Thread):
PacketListenerManager.handleFC(self.FileObject)
if (not self.encryptedConnection):
self.enableEncryption()
self.connection.isConnected = True
PacketSenderManager.sendCD(self.socket, 0)
elif(response == "\xFF"):
DisconMessage = PacketListenerManager.handleFF(self.FileObject)

View File

@ -123,7 +123,11 @@ if __name__ == "__main__":
connection.start()
while True:
try:
raw_input()
chat_input = raw_input()
if (connection.isConnected):
PacketSenderManager.send03(connection.grabSocket(), chat_input)
else:
pass
except KeyboardInterrupt, e:
connection.disconnect()
sys.exit(1)