Beispiel #1
0
def get_common_checksums_sparse(common_dict, message, min_length, max_length, sparse_value, character_list, max_length_only):
	if len(message) > max_length:
		return False
	#sparse_characters_to_use = sorted(sample(character_list,sparse_value))
	sparse_characters_to_use = sample(character_list,sparse_value)
	#print sparse_characters_to_use
	for n in sparse_characters_to_use:
		new_message = message + str(unichr(n))
		try:
			if len(new_message) > 1:
				formatted_message = str(Command(new_message[0],new_message[1:]))
			else:
				formatted_message = str(Command(new_message))
		except CommandException as e:
			continue
		checksum_char = formatted_message[-1]
		#common_dict.setdefault(checksum_char,[]).append(formatted_message)
		if len(message) >= min_length:
			if max_length_only:
				if len(message) == max_length:
					common_dict.setdefault(checksum_char,0)
					common_dict[checksum_char] += 1
			else:
				common_dict.setdefault(checksum_char,0)
				common_dict[checksum_char] += 1
		if not get_common_checksums_sparse(common_dict, new_message, min_length, max_length, sparse_value, character_list, max_length_only):
			break
	return True
def test_command_formatted_string():
    command_string = "testcommand"
    value_string = "testvalue"
    proper_beginning = command_string + "|" + value_string
    command = Command(command_string, [value_string])
    assert command.get_formatted_string().startswith(proper_beginning)
    assert command.get_formatted_string()[-2] == "*"
    assert command.get_formatted_string()[-1] == Command.get_checksum(
        proper_beginning)
Beispiel #3
0
def generate_random_strings_with_checksum(repeats):
	for n in range(0,repeats):
		commandString = ""
		for n in range(randint(1,11)):
			commandString += str(unichr(randint(33,126)))
		try:
			newcommand = Command(commandString)
		except CommandException as e:
			print("{} --> {}".format(str(e),commandString))
		else:
			print("------")
			print(newcommand)
			print(ord(str(newcommand)[-1]))
			print("------")
Beispiel #4
0
def get_common_checksums(common_dict, message, min_length, max_length, character_list, max_length_only):
	if len(message) > max_length:
		return False
	for n in range(33,127):
		new_message = message + str(unichr(n))
		try:
			formatted_message = str(Command(new_message))
		except CommandException as e:
			continue
		checksum_char = formatted_message[-1]
		#common_dict.setdefault(checksum_char,[]).append(formatted_message)
		if len(message) >= min_length:
			if max_length_only:
				if len(message) == max_length:
					common_dict.setdefault(checksum_char,0)
					common_dict[checksum_char] += 1
			else:
				common_dict.setdefault(checksum_char,0)
				common_dict[checksum_char] += 1
		if not get_common_checksums(common_dict, new_message, min_length, max_length, character_list, max_length_only):
			break
	return True
def test_command_list_value():
    command_string = "testcommand"
    value_string = "testvalue"
    command = Command(command_string, [value_string])
    assert command.get_command() == command_string
    assert command.get_values() == [value_string]
def test_command_only_command_given():
    command_string = "testcommand"
    command = Command(command_string)
    assert command.get_command() == command_string
Beispiel #7
0
	max_val = float(max(dict1.values()))
	for key,value in sorted(dict1.iteritems()):
		print("{}: {:.2f}  -->  {}".format(key,value/max_val,value))

def checksum_distribution():
	old_dict = {}
	min_length = 1
	max_length = 6 # depth of tree (where each node + leaf is a possible string)
	max_length_only=False
	min_char = 97#33
	max_char = 122#126
	sparse_value = 2 # branching factor (don't exceed max_char-min_char); affects time exponentially
	repetitions = 10000 # repeats; affects time linearly
	for n in range(repetitions):
		new_dict = get_common_checksums_sparse_wrapper(min_length, max_length,sparse_value,min_char,max_char,max_length_only)
		combine_dicts(old_dict,new_dict)
	print_dict_normalized(old_dict)

	print("-----")

if __name__=="__main__":
	#print Command("gb")
	
	print(Command("ready"))
	#checksum_distribution()
	


	#other_dict = get_common_checksums_wrapper(3)
	#print_dict(other_dict)