def generate_script_hash(script): """ Return a hash for a given script. """ encoded_script = script.encode("utf-8") # As this hash is sent in message which requires bytes in the schema, we # have to encode here. return md5(encoded_script).hexdigest().encode("ascii")
def test_wb_include_accepted_types(self): """ Every payload from the client needs to specify an ID which represents the types that we think the server wants. """ payload = self.exchanger._make_payload() self.assertIn("accepted-types", payload) self.assertEqual(payload["accepted-types"], md5(b"").digest())
def test_exchange_does_not_send_message_types_when_hash_matches(self): self.exchanger.register_client_accepted_message_type("type-A") self.exchanger.register_client_accepted_message_type("type-B") types = sorted(["type-A", "type-B"] + DEFAULT_ACCEPTED_TYPES) accepted_types_digest = md5(";".join(types).encode("ascii")).digest() self.transport.extra["client-accepted-types-hash"] = \ accepted_types_digest self.exchanger.exchange() self.exchanger.exchange() self.assertNotIn("client-accepted-types", self.transport.payloads[1])
def test_wb_accepted_types_roundtrip(self): """ Telling the client to set the accepted types with a message should affect its future payloads. """ self.exchanger.handle_message( {"type": "accepted-types", "types": ["ack", "bar"]}) payload = self.exchanger._make_payload() self.assertIn("accepted-types", payload) self.assertEqual(payload["accepted-types"], md5(b"ack;bar").digest())
def test_exchange_sends_new_accepted_types_hash(self): """ If the accepted types on the client change between exchanges, the client will send a new list to the server. """ self.exchanger.register_client_accepted_message_type("type-A") types_hash = md5(b"type-A").digest() self.transport.extra["client-accepted-types-hash"] = types_hash self.exchanger.exchange() self.exchanger.register_client_accepted_message_type("type-B") self.exchanger.exchange() self.assertEqual( self.transport.payloads[1]["client-accepted-types"], sorted(["type-A", "type-B"] + DEFAULT_ACCEPTED_TYPES))
def test_exchange_sends_new_types_when_server_screws_up(self): """ If the server suddenly and without warning changes the hash of accepted client types that it sends to the client, the client will send a new list of types. """ self.exchanger.register_client_accepted_message_type("type-A") types_hash = md5(b"type-A").digest() self.transport.extra["client-accepted-types-hash"] = types_hash self.exchanger.exchange() self.transport.extra["client-accepted-types-hash"] = "lol" self.exchanger.exchange() self.exchanger.exchange() self.assertEqual( self.transport.payloads[2]["client-accepted-types"], sorted(["type-A"] + DEFAULT_ACCEPTED_TYPES))
def _hash_types(self, types): accepted_types_str = ";".join(types) return md5(accepted_types_str).digest()
def _hash_types(self, types): accepted_types_str = ";".join(types).encode("ascii") return md5(accepted_types_str).digest()
def generate_script_hash(script): """ Return a hash for a given script. """ return md5(script).hexdigest()
def unix_md5_crypt(pw, salt, magic=None): if magic==None: magic = MAGIC # Take care of the magic string if present if salt[:len(magic)] == magic: salt = salt[len(magic):] # salt can have up to 8 characters: import string salt = string.split(salt, '$', 1)[0] salt = salt[:8] ctx = pw + magic + salt final = md5(pw + salt + pw).digest() for pl in range(len(pw),0,-16): if pl > 16: ctx = ctx + final[:16] else: ctx = ctx + final[:pl] # Now the 'weird' xform (??) i = len(pw) while i: if i & 1: ctx = ctx + chr(0) #if ($i & 1) { $ctx->add(pack("C", 0)); } else: ctx = ctx + pw[0] i = i >> 1 final = md5(ctx).digest() # The following is supposed to make # things run slower. # my question: WTF??? for i in range(1000): ctx1 = '' if i & 1: ctx1 = ctx1 + pw else: ctx1 = ctx1 + final[:16] if i % 3: ctx1 = ctx1 + salt if i % 7: ctx1 = ctx1 + pw if i & 1: ctx1 = ctx1 + final[:16] else: ctx1 = ctx1 + pw final = md5(ctx1).digest() # Final xform passwd = '' passwd = passwd + to64((int(ord(final[0])) << 16) |(int(ord(final[6])) << 8) |(int(ord(final[12]))),4) passwd = passwd + to64((int(ord(final[1])) << 16) |(int(ord(final[7])) << 8) |(int(ord(final[13]))), 4) passwd = passwd + to64((int(ord(final[2])) << 16) |(int(ord(final[8])) << 8) |(int(ord(final[14]))), 4) passwd = passwd + to64((int(ord(final[3])) << 16) |(int(ord(final[9])) << 8) |(int(ord(final[15]))), 4) passwd = passwd + to64((int(ord(final[4])) << 16) |(int(ord(final[10])) << 8) |(int(ord(final[5]))), 4) passwd = passwd + to64((int(ord(final[11]))), 2) return magic + salt + '$' + passwd