Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ic:laboratoare:01 [2020/10/03 13:17]
acosmin.maria
ic:laboratoare:01 [2020/10/18 15:55] (current)
acosmin.maria
Line 29: Line 29:
 <file python utils.py>​ <file python utils.py>​
 import base64 import base64
 + 
 # CONVERSION FUNCTIONS # CONVERSION FUNCTIONS
 + 
 def _chunks(string,​ chunk_size):​ def _chunks(string,​ chunk_size):​
     for i in range(0, len(string),​ chunk_size):​     for i in range(0, len(string),​ chunk_size):​
         yield string[i:​i+chunk_size]         yield string[i:​i+chunk_size]
 +  
 +def _hex(x): 
 +    return format(x, '​02x'​) 
 + 
 def hex_2_bin(data):​ def hex_2_bin(data):​
     return ''​.join(f'​{int(x,​ 16):​08b}'​ for x in _chunks(data,​ 2))     return ''​.join(f'​{int(x,​ 16):​08b}'​ for x in _chunks(data,​ 2))
 + 
 def str_2_bin(data):​ def str_2_bin(data):​
     return ''​.join(f'​{ord(c):​08b}'​ for c in data)     return ''​.join(f'​{ord(c):​08b}'​ for c in data)
 + 
 def bin_2_hex(data):​ def bin_2_hex(data):​
     return ''​.join(f'​{int(b,​ 2):​02x}'​ for b in _chunks(data,​ 8))     return ''​.join(f'​{int(b,​ 2):​02x}'​ for b in _chunks(data,​ 8))
 + 
 def str_2_hex(data):​ def str_2_hex(data):​
     return ''​.join(f'​{ord(c):​02x}'​ for c in data)     return ''​.join(f'​{ord(c):​02x}'​ for c in data)
 + 
 def bin_2_str(data):​ def bin_2_str(data):​
     return ''​.join(chr(int(b,​ 2)) for b in _chunks(data,​ 8))     return ''​.join(chr(int(b,​ 2)) for b in _chunks(data,​ 8))
 + 
 def hex_2_str(data):​ def hex_2_str(data):​
     return ''​.join(chr(int(x,​ 16)) for x in _chunks(data,​ 2))     return ''​.join(chr(int(x,​ 16)) for x in _chunks(data,​ 2))
 + 
 # XOR FUNCTIONS # XOR FUNCTIONS
 + 
 def strxor(a, b):  # xor two strings, trims the longer input def strxor(a, b):  # xor two strings, trims the longer input
     return ''​.join(chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b))     return ''​.join(chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b))
 + 
 def bitxor(a, b):  # xor two bit-strings,​ trims the longer input def bitxor(a, b):  # xor two bit-strings,​ trims the longer input
     return ''​.join(str(int(x) ^ int(y)) for (x, y) in zip(a, b))     return ''​.join(str(int(x) ^ int(y)) for (x, y) in zip(a, b))
 + 
 def hexxor(a, b):  # xor two hex-strings,​ trims the longer input def hexxor(a, b):  # xor two hex-strings,​ trims the longer input
-    return ''​.join(hex(int(x, 16) ^ int(y, 16))[2:] for (x, y) in zip(_chunks(a,​ 2), _chunks(b, 2))) +    return ''​.join(_hex(int(x, 16) ^ int(y, 16)) for (x, y) in zip(_chunks(a,​ 2), _chunks(b, 2))) 
 + 
 # BASE64 FUNCTIONS # BASE64 FUNCTIONS
 + 
 def b64decode(data):​ def b64decode(data):​
     return bytes_to_string(base64.b64decode(string_to_bytes(data)))     return bytes_to_string(base64.b64decode(string_to_bytes(data)))
 + 
 def b64encode(data):​ def b64encode(data):​
     return bytes_to_string(base64.b64encode(string_to_bytes(data)))     return bytes_to_string(base64.b64encode(string_to_bytes(data)))
 + 
 # PYTHON3 '​BYTES'​ FUNCTIONS # PYTHON3 '​BYTES'​ FUNCTIONS
 + 
 def bytes_to_string(bytes_data):​ def bytes_to_string(bytes_data):​
     return bytes_data.decode() ​ # default utf-8     return bytes_data.decode() ​ # default utf-8
 + 
 def string_to_bytes(string_data):​ def string_to_bytes(string_data):​
     return string_data.encode() ​ # default utf-8     return string_data.encode() ​ # default utf-8
Line 248: Line 251:
  
 == Hints == == Hints ==
 +<note tip>
   * What happens when you XOR a character from [a-z] with the character ' ' (spacebar). Check also for [A-Z].   * What happens when you XOR a character from [a-z] with the character ' ' (spacebar). Check also for [A-Z].
   * You can't write a perfect algorithm to solve the problem in one shot, you will mostly have to guess. Why?   * You can't write a perfect algorithm to solve the problem in one shot, you will mostly have to guess. Why?
   * The challenge is nice, but can get tedious. Luckily for us there is a cool open source implementation. Check it out!   * The challenge is nice, but can get tedious. Luckily for us there is a cool open source implementation. Check it out!
 +</​note>​
 === How to run on Windows === === How to run on Windows ===
  
ic/laboratoare/01.1601720274.txt.gz · Last modified: 2020/10/03 13:17 by acosmin.maria
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0