#!/usr/bin/python import sys import sha def str2uni(data): buf = "" for char in data: buf += char + "\x00" return buf class CSQLServerPassword: data = "" _header = "" _key = "" _password = "" _upperPassword = "" def __init__(self, data = None): if data: self.data = data if len(self.data) != 94: raise "Invalid password hash size" if self.data[0:2].lower() != "0x": raise "Invalid password hash" self._header = int(self.data[2:6]) self._key = int(self.data[6:8]) self._password = self.data[8:40] self._upperPassword = self.data[40:] def printSummary(self): print "Header : ", "0x" + self._header print "Key : ", self._key print "Password : ", self._password print "Password (Upper) : ", self._upperPassword def encrypt(self, passwd): # Convert the password to an unicode string mPasswd = str2uni(passwd) # Append the random stuff (the key) mPasswd += str(self._key) # Get the first hash (normal) baseHash = sha.sha(mPasswd).hexdigest().upper() # Get the upper case hash upperHash = sha.sha(mPasswd.upper()).hexdigest().upper() # Generate the password buf = "0x" buf += str(self._header) buf += str(self._key) buf += baseHash buf += upperHash return buf if __name__ == "__main__": passwd = "0x01008444930543174C59CC918D34B6A12C9CC9EF99C4769F819B43174C59CC918D34B6A12C9CC9EF99C4769F819B" objSQLServer = CSQLServerPassword(passwd) print objSQLServer.encrypt("sa")