def test_large_file(self): stream = bytearray('') try: os.remove('warandpeace2.txt') except OSError: pass encode('warandpeace.txt', stream) decode(stream, 'warandpeace2.txt') self.assertEqual(filecmp.cmp('warandpeace.txt','warandpeace2.txt'), True)
def main(): if len(sys.argv) != 3: print('use: sudoku_solver <sudoku_instances_file> <encoding>') exit(1) if sys.argv[2] != '1' and sys.argv[2] != '2': print('use 1 for minimal or 2 for extendend in encoding') exit(1) if not os.path.isfile('./build/release/bin/minisat'): print('run `make r` first') exit(1) # Get constant clauses encoding = int(sys.argv[2]) clauses_string = clauses(encoding) sudoku = NINExNINE instances = open(sys.argv[1],'r') solution_file = open('solution','w') minisat_solname = 'sudoku_solution' sudoku_cnf = 'sudoku.cnf' for inst in instances: for i in range(0, 81): sudoku[i//9][i%9] = inst[i] encode(sudoku, clauses_string, encoding) # Get a solution and measure it's time start = time.time() subprocess.call(['./build/release/bin/minisat', sudoku_cnf, minisat_solname], stdout = open(os.devnull, 'w'), stderr = open(os.devnull, 'w')) delta = time.time() - start # If the file was created if os.path.isfile(minisat_solname): solution_file.write("-----------------------\ninstance: '" + inst + "'\ntime: " + str(delta) + '\n\n') decode(minisat_solname, solution_file) else: print("there is no solution for the instance '" + inst[:-1] +"'\n") if os.path.isfile(minisat_solname): os.remove(minisat_solname) os.remove(sudoku_cnf) instances.close() solution_file.close() # If this is the module running if __name__ == '__main__': main()
def test_decode_ambiguous(): text = "mango magno" encoded = encoder.encode(text) decoded = encoder.decode(encoded) decoded = decoded.split() assert decoded[0] == "magno" or decoded[0] == "mango" assert decoded[1] == "magno" or decoded[1] == "mango"
def interpret(args): with open(args.infile.name, args.infile.mode) as file: code = file.read() if args.non_encoded == False: code = encoder.decode(code.hex()) else: code = cleanup(code.decode('cp1251')) arguments = {} if args.user_input != None: arguments["user_input"] = args.user_input if args.upper != None: arguments["upper"] = args.upper if args.lower != None: arguments["lower"] = args.lower if args.default != None: arguments["default"] = args.default if args.eof != None: arguments["eof"] = args.eof if args.non_wrapping == True: arguments["wrapping"] = False interpreter.evaluate(code, **arguments)
def build_file(self, parent_id): """Download a uds file This will fetch the Docs one by one, concatenating them to a local base64 file. The file will then be converted from base64 to the appropriate mime-type. Args: parent_id (str): The ID of the containing folder :return: :param parent_id: """ items = self.api.recursive_list_folder(parent_id) folder = self.api.get_file(parent_id) if not items: print('No parts found.') return # Fix part as int for item in items: item['properties']['part'] = int(item['properties']['part']) items.sort(key=lambda x: x['properties']['part'], reverse=False) f = open("%s/%s" % (get_downloads_folder(), folder['name']), "a+b") progress_bar_chunks = tqdm(total=len(items), unit='chunks', dynamic_ncols=True, position=0) progress_bar_speed = tqdm(total=len(items) * CHUNK_READ_LENGTH_BYTES, unit_scale=1, unit='B', dynamic_ncols=True, position=1) for item in items: encoded_part = self.download_part(item['id']) # Decode decoded_part = encoder.decode(encoded_part) progress_bar_chunks.update(1) progress_bar_speed.update(CHUNK_READ_LENGTH_BYTES) # Append decoded part to file f.write(decoded_part) print(" \r") file_hash = self.hash_file(f.name) f.close() original_hash = folder.get("md5Checksum") if file_hash != original_hash and original_hash is not None: print( "Failed to verify hash\nDownloaded file had hash {} compared to original {}" .format(file_hash, original_hash)) os.remove(f.name)
def decode(bq): if not bq.nextBit(): return decode_str(bq) if not bq.nextBit(): return numcoder.decode(bq) if not bq.nextBit(): return struct.unpack('!f',bq.popBytes(4))[0] return GILFprogram(encoder.decode(bq))
def decode(bq): if not bq.nextBit(): return decode_str(bq) if not bq.nextBit(): return numcoder.decode(bq) if not bq.nextBit(): return struct.unpack('!f', bq.popBytes(4))[0] return GILFprogram(encoder.decode(bq))
def incoming(self, src, msg): d = encoder.decode(msg)[0] if d["networkid"] != networkid: return self.addNode(src) { "ping": self.recv_ping, "pong": self.recv_pong, "found_node": self.recv_found_node, "found_value": self.recv_found_value, "find_node": self.recv_find_node, "find_value": self.recv_find_value, "store_value": self.recv_store, }[d["type"].lower()](src, d)
def post(self): # check if body is valid unicode try: text = request.get_data().decode("utf-8") except UnicodeDecodeError: response = make_response("Error: body is not valid unicode", 400) response.mimetype = "text/plain" return response # check if decode was successful try: response = make_response(decode(text), 200) except ValueError as e: response = make_response(str(e), 400) response.mimetype = "text/plain" return response
def route_decrypt(): # get ciphertext encoded_cipher = request.values.get("cipher", None) if encoded_cipher is None: raise ValueError( "Pass encoded chipher to decrypt using 'cipher' parameter in URL or POST data" ) # get encoding encoding = get_encoding(request) # decode cipher into bytes cipher = encoder.decode(data=encoded_cipher, encoding=Encoding[encoding]) # decrypt cipher into plaintext plain = crypto.decrypt(cipher, AES_key()) # answer return plain, 200
def test_multiple_files(self): stream = bytearray('') try: os.remove('a2.txt') os.remove('b2.txt') os.remove('c2.txt') except OSError: pass encode('a.txt', stream) encode('b.txt', stream) encode('c.txt', stream) decode(stream, 'a2.txt') decode(stream, 'b2.txt') decode(stream, 'c2.txt') self.assertEqual(filecmp.cmp('a.txt','a2.txt'), True) self.assertEqual(filecmp.cmp('b.txt','b2.txt'), True) self.assertEqual(filecmp.cmp('c.txt','c2.txt'), True)
parser.add_option("-d", "--decode", action="store_false", dest="encoder", help="Decode previously encoded file") parser.add_option("-p", "--password", type="string", dest="password", help="Password to encode / decode") parser.add_option("-t", "--test", action="store_true", dest="debug") (options, args) = parser.parse_args() if options.debug: # verify basic functionality teststring = "The quick brown fox jumped over the lazy dog" testpwd = "password" encoded = encode(teststring,testpwd) assert(teststring != encode(teststring,testpwd)) assert(testpwd != encode(teststring,testpwd)) assert(encode(teststring,"wrongpwd") != encode(teststring,testpwd)) assert(teststring == decode(encoded,testpwd)) # try increasingly difficult strings teststring = "Adding numbers 12345" assert(teststring == decode(encode(teststring,"password"),"password")) teststring = "Adding special chars @#$(*^,;'" assert(teststring == decode(encode(teststring,"password"),"password")) teststring = 'with double quotes"' assert(teststring == decode(encode(teststring,"password"),"password")) print "Success!" exit(0) if not options.password: parser.error("You must provide a password.") if not options.filename: parser.error("You must provide a file to encode or decode") if options.encoder == -1:
def test_decode_simple(): encoded = "random stuff" + encoder.SEPARATOR + "Tihs is Sartpa!!!" + encoder.SEPARATOR + "Sparta This" assert encoder.decode(encoded) == "This is Sparta!!!"
def test_encoding_decoding(value, encoding): encoded = encode(value, encoding) decoded = decode(encoded, encoding) assert decoded == value
def test_unknown_encoding(): with pytest.raises(RuntimeError): encode(b"", -1) with pytest.raises(RuntimeError): decode("", -1)
def test_decode_encode(): text = "Hello World! 😉 Lorem ipsum, random words, punctuation; even some unicode: 醤油" encoded = encoder.encode(text) assert encoder.decode(encoded) == text
if __name__ == '__main__': if not os.path.exists('target'): os.mkdir('target') print_table_header() print_separator() for file in os.listdir('data'): try: if file.endswith('.txt'): os.system('gzip -fk9 data/%s' % file) shutil.move('data/%s.gz' % file, 'target/%s.gz' % file) encoder.encode('data/'+file, 'target/'+file+'.encoded') os.system('gzip -fk9 target/%s.encoded' % file) print_table_entry(file, fsize('data/'+file), fsize('target/'+file + '.gz'), fsize('target/'+file + '.encoded'), fsize('target/'+file + '.encoded.gz')) encoder.decode('target/'+file + '.encoded') assert fsize('target/'+file + '.encoded.decoded') == fsize('data/'+file), file with open('target/'+file + '.encoded.decoded', 'r') as fp: with open('data/'+file, 'r') as fp2: assert fp.read() == fp2.read() except: print 'error in', file raise print_separator()
data.append(str(bit)) result = ''.join(data) return image, result def cut_data(data): for i in range(5, len(data) - 3): if data[i] == data[i + 1] == data[i + 2] == data[i + 3] == '3': data = data[:i + 4:] return data def scan(path): image = cv.imread(path) show_image(image) transformed_image = transform_for_contour_detection(image) main_hex = find_main_shape(transformed_image, 6) warped_image = warp(image, main_hex) show_image(warped_image) rotated_image, cell_width = rotate(warped_image) show_image(rotated_image) extracted_image, data = extract_data(rotated_image, cell_width) show_image(extracted_image) data = cut_data(data) return data if __name__ == '__main__': coded_data = scan('Resources/HexDetect-1.jpg') print(encoder.decode(coded_data))
def handle_decode_single(input_num): return encoder.decode(input_num)
def handle_decode(request): file = request.files["decoder_input"] lines = file.read() return [(l.decode(), encoder.decode(l)) for l in lines.split()]
def test_decode_validation(): bad_separator = encoder.SEPARATOR + "aaa a aaaaaa" + "\n-weird#-\n" + "aaa aaaaaa" with pytest.raises(ValueError): encoder.decode(bad_separator)
def privmsg(self, user, channel, msg): try: #I forgot why this exists but it causes bugs pass #user = user.split(key, 1)[0] except: user = user if user == self.nickname: return if not channel.startswith('#'): channel = user.split('!') auth = self.checkauth(user) owner = self.checkowner(user) # Start module execution command = msg.split(' ', 1)[0].lower() lower_channel = channel.lower() if lower_channel in sync_channels: # syncing u = user.split('!', 1)[0] self.msg(sync_channels[lower_channel], '<%s> %s' % (u, msg)) self.syncmsg(user, lower_channel, sync_channels[lower_channel], msg) iskey = False if command.startswith(key): command = command.split(key, 1)[1] iskey = True else: command = command if iskey or (channel == self.nickname and auth): setattr(self, 'isop', auth) setattr(self, 'isowner', owner) setattr(self, 'type', 'privmsg') setattr(self, 'command', command) setattr(self, 'message', msg) setattr(self, 'user', user) setattr(self, 'channel', channel) setattr(self, 'ver', VER) if command in mod_declare_privmsg: try: self.lockerbox[mod_declare_privmsg[command]] except: self.lockerbox[mod_declare_privmsg[ command]] = self.persist() # attributes setattr(self, 'store', self.save) setattr(self, 'locker', self.lockerbox[ mod_declare_privmsg[command]]) ##### Hijack config object functions to reduce scope def __config_get__(item, default=False): #stubs, basically return config_get(mod_declare_privmsg[command], item, default) def __config_set__(item, value): return config_set(mod_declare_privmsg[command], item, value) def __config_remove__(item): return config_remove(mod_declare_privmsg[command], item) setattr(self, 'config_get', __config_get__) setattr(self, 'config_set', __config_set__) setattr(self, 'config_remove', __config_remove__) log_data = "Command: %s, user: %s, channel: %s, data: %s" % ( command, user, channel, msg) log.msg(log_data) u = user.split('!', 1)[0] if channel == self.nickname: # private commands if irc_relay != "": self.msg(irc_relay, user + " said " + msg) if owner: if msg.startswith('op'): host = msg.split(' ', 1)[1] extname = host.split('!', 1)[0] c = conn.execute('insert into op(username) values (?)', (host.split('!', 1)[1],)) conn.commit() self.msg( user.split( '!', 1)[0], 'Added user %s to the op list' % (extname)) self.msg(extname, "You've been added to my op list") elif msg.startswith('deop'): host = msg.split(' ', 1)[1] extname = host.split('!', 1)[0] c = conn.execute('delete from op where username = ?', (host.split('!', 1)[1],)) conn.commit() self.msg( user.split( '!', 1)[0], 'Removed user %s from the op list' % (extname)) elif msg.startswith('prof_on'): pr.enable() self.msg(u, 'profiling on') elif msg.startswith('prof_off'): pr.disable() self.msg(u, 'profiling off') elif msg.startswith('prof_stat'): s = StringIO.StringIO() sortby = 'cumulative' ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps.print_stats() self.msg(u, s.getvalue()) elif msg.startswith('mod_inject'): mod = msg.split(' ')[1] url = msg.split(' ')[2] req = urllib2.Request( url, headers={'User-Agent': 'UNIX:KittyHawk http://github.com/KittyHawkIRC'}) fd = urllib2.urlopen(req) mod_src = open( config_dir + '/modules/' + mod + '.py', 'w') data = fd.read() mod_src.write(data) os.fsync(mod_src) fd.close() mod_src.close() elif msg.startswith('mod_load'): mod = msg.split(' ')[1] mod_src = open(config_dir + '/modules/' + mod + '.py') mod_bytecode = compile( mod_src.read(), '<string>', 'exec') mod_src.close() modlook[mod] = imp.new_module(mod) sys.modules[mod] = modlook[mod] exec mod_bytecode in modlook[mod].__dict__ declare_table = modlook[mod].declare() for i in declare_table: cmd_check = declare_table[i] if cmd_check == 'privmsg': mod_declare_privmsg[i] = mod elif cmd_check == 'userjoin': mod_declare_userjoin[i] = mod elif cmd_check == 'syncmsg': mod_declare_syncmsg[i] = mod elif msg.startswith('update_inject'): try: url = msg.split(' ')[1] except: url = 'https://raw.githubusercontent.com/KittyHawkIRC/core/master/arsenic.py' req = urllib2.Request( url, headers={'User-Agent': 'UNIX:KittyHawk http://github.com/KittyHawkIRC'}) fd = urllib2.urlopen(req) mod_src = open(sys.argv[0], 'w') data = fd.read() mod_src.write(data) os.fsync(mod_src) fd.close() mod_src.close() elif msg.startswith('update_restart'): try: mod_src = open(sys.argv[0]) compile(mod_src.read(), '<string>', 'exec') # syntax testing args = sys.argv[:] args.insert(0, sys.executable) # os.chdir(_startup_cwd) os.execv(sys.executable, args) except: self.msg(u, 'Syntax error!') elif msg.startswith('update_patch'): mod_src = open(sys.argv[0]) mod_bytecode = compile( mod_src.read(), '<string>', 'exec') mod_src.close() update = imp.new_module('update') exec mod_bytecode in update.__dict__ old = self self.__class__ = update.Arsenic self = update.Arsenic(old) self.msg(u, 'Attempted runtime patching (%s)' % (VER)) elif msg.startswith('inject'): self.lineReceived(msg.split(' ', 1)[1]) elif msg.startswith('raw'): self.sendLine(msg.split(' ', 1)[1]) elif msg.startswith('config_get'): opts = msg.split() module = opts[1] item = opts[2] self.msg(u, str(config_get(module, item, False))) elif msg.startswith('config_set'): opts = msg.split() module = opts[1] item = opts[2] value_list = opts[3:] value = '' for i in value_list: value += i + ' ' self.msg(u, str(config_set(module, item, value[:len(value) - 1]))) elif msg.startswith('config_remove'): opts = msg.split() module = opts[1] item = opts[2] self.msg(u, str(config_remove(module, item))) elif msg.startswith('config_list'): opts = msg.split() module = opts[1] if config.has_section(module): self.msg(u, str(config.options(module))) else: self.msg(u, 'no section for that module!') elif msg.startswith('help_config'): self.msg(u, 'KittyHawk Ver: %s' % (VER)) self.msg(u, 'Config commands: (note: owner only)') self.msg(u, 'config_set {module} {item} {value} (Sets a value for a module)') self.msg(u, 'config_get {module} {item} (Returns a value)') self.msg(u, 'config_remove {module} {item} (Removes a value)') self.msg(u, 'config_list {module} (Lists all items for a module)') elif msg.startswith('help_sysop'): self.msg(u, 'KittyHawk Ver: %s' % (VER)) self.msg( u, "DO NOT USE THESE UNLESS YOU KNOW WHAT YOU'RE DOING") self.msg(u, 'SysOP commands:') self.msg( u, 'op {hostmask}, deop {hostmask} (add or remove a user)') self.msg( u, 'prof_on, prof_off (enable or disable profiling, DO NOT USE)') self.msg( u, 'restart, prof_stat (Restart or display profiling stats (see ^))') self.msg( u, 'mod_load {module}, mod_reload {module} (Load or reload a loaded module)') self.msg( u, 'mod_inject {module} {url} (Download a module over the internet. (is not loaded))') self.msg( u, 'raw {line}, inject {line} (raw sends a raw line, inject assumes we recieved a line)') self.msg( u, 'update_restart, update_patch (Updates by restarting or patching the runtime)') self.msg( u, 'update_inject {optional:url} Downloads latest copy over the internet, not updated') if auth: if msg.startswith('add'): cmd = msg.split(' ', 2)[1].lower() data = msg.split(' ', 2)[2] conn.execute( ('insert or replace into command(name, response) ' 'values (?, ?)'), (cmd.decode('utf-8'), data.decode('utf-8'))) conn.commit() if data.startswith('!'): data = encoder.decode(data) self.msg( user.split( '!', 1)[0], 'Added the command %s with value %s' % (cmd, data)) elif msg.startswith('del'): cmd = msg.split(' ')[1].lower() conn.execute('delete from command where name = ?', (cmd.decode('utf-8'),)) conn.commit() self.msg( user.split( '!', 1)[0], 'Removed command %s' % (cmd)) elif msg == 'help': self.msg(u, 'KittyHawk Ver: %s' % (VER)) self.msg(u, 'Howdy, %s, you silly operator.' % (u)) self.msg( u, 'You have access to the following commands:') self.msg(u, 'add {command} {value}, del {command}') self.msg(u, 'join {channel}, leave {channel}') self.msg(u, 'nick {nickname}, topic {channel} {topic}') self.msg(u, 'kick {channel} {name} {optional reason}') self.msg(u, 'ban/unban {channel} {hostmask}') self.msg( u, 'sync {channel1} {channel2}, unsync {channel1}') self.msg(u, 'sync_list, msg {channel} {message}') elif msg == 'sync_list': for i in sync_channels: self.msg(u, '%s -> %s' % (i, sync_channels[i])) elif msg.startswith('sync'): ch1 = msg.split(' ')[1].lower() ch2 = msg.split(' ')[2].lower() if ch1 in sync_channels: self.msg(u, 'WARNING: %s already syncs to %s, overridden' % ( ch1, sync_channels[ch1])) sync_channels[ch1] = ch2 self.msg(u, '%s -> %s' % (ch1, ch2)) elif msg.startswith('unsync'): ch1 = msg.split(' ')[1].lower() if ch1 in sync_channels: del sync_channels[ch1] self.msg(u, '%s -> X' % (ch1)) else: self.msg(u, 'Channel not currently being synced') elif msg.startswith('cache_save'): self.cache_save() elif msg.startswith('mod_update'): mod = msg.split(' ')[1] if not mod in modlook: self.msg(u, 'Unknown module! (%s)' % (mod)) return try: url = modlook[mod].__url__ except: self.msg(u, 'Error, module lacks update schema') return try: # Impersonate the first owner, yolo op = 'fake!' + list(ownerlist)[0] except Exception, err: log.err(err) self.msg(u, 'Error, no owners are defined') return inject = 'mod_inject %s %s' % (mod, url) load = 'mod_load %s' % (mod) try: # It's dirty, but self.privmsg(op, channel, inject) self.privmsg(op, channel, load) # this shit works self.msg(u, 'Module updated') except: self.msg(u, 'an error occured updating the module') if command in mod_declare_privmsg: modlook[ mod_declare_privmsg[ command]].callback( self) elif msg.startswith(key): if channel == '#fatpeoplehate': return if channel == '#FatPeopleHate': return if command in mod_declare_privmsg: modlook[ mod_declare_privmsg[ command]].callback( self) elif msg.startswith(key + 'help'): self.msg( u, 'Howdy, %s, please visit https://commands.tox.im to view the commands.' % (u)) else: c = conn.execute( 'select response from command where name == ?', (command.decode('utf-8'),)) r = c.fetchone() if r is not None: rs = str(r[0]) if rs.startswith('!'): rs = encoder.decode(rs) if self.floodprotect: # adds a space every other command. ha rs = rs + ' ' self.floodprotect = False else: self.floodprotect = True try: u = msg.split(' ')[1] self.msg(channel, "%s: %s" % (u, rs)) except: self.msg(channel, rs)
try: hostname = config.get('network', 'hostname') except: raise conf('Unable to read hostname') try: port = config.getint('network', 'port') except: raise conf('Unable to read port') try: oplist = set(config.get('main', 'op').replace(' ', '').split(',')) for user in oplist: if user.startswith('!'): oplist.remove(user) oplist.add(encoder.decode(user)) except: print 'Unable to read ops, assuming none' oplist = [] ownerlist = oplist modlook = {} try: modules = config.get('main', 'mod').replace(' ', '').split(',') except: print 'Unable to read modules, assuming none' modules = [] # relays messages without a log
result = re.match(r'\((\d+),(\d+)\)', l) c = int(result.group(1)) d = int(result.group(2)) #Add extracted cipher ciphers.append(elgamal.CipherBlock(c,d)) keyFile = open("K2", 'r') p = int(keyFile.readline()) g = int(keyFile.readline()) x = int(keyFile.readline()) keyFile.close() key = elgamal.PrivateKey(p,g,x) blocks = [] #Generate a list of decrypted blocks for c in ciphers: blocks.append(elgamal.decrypt(c, key)) #Set the blocklength for the decoder encoder.BLOCKLENGTH = int(sys.argv[1]) #Decode the blocks to a string message = encoder.decode(blocks) #Write out textFile = open("Plaintext", 'w') textFile.write(message) textFile.close()