Skip to content

Instantly share code, notes, and snippets.

@marcusmonteiro
Last active March 31, 2018 09:42
Show Gist options
  • Save marcusmonteiro/85998922496026b489ecddd810628007 to your computer and use it in GitHub Desktop.
Save marcusmonteiro/85998922496026b489ecddd810628007 to your computer and use it in GitHub Desktop.
Useful functions to test ciphering functions
import binascii
import codecs
def to_hex(s: str, pad_until_length: int = 0) -> str:
"""
Args:
s: the string to be converted to it's hex representation.
pad_until_length: if the length of the `s` argument is less than this argument, the string to be converted will be padded
with binary zeros to the right until it's length is equal to this argument.
Returns:
The hex representation of the `s` argument, padded to the right with binary zeros depending on the `pad_until_length` argument.
"""
return binascii.hexlify(codecs.encode(s)).ljust(pad_until_length, b'0')
def xor(s1: str, s2: str) -> str:
"""
Args:
s1: a string made of only valid hexadecimal characters.
s2: a string made of only valid hexadecimal characters.
Returns:
The result of xor'ing the arguments.
"""
return hex(int(s1, 16) ^ int(s2, 16))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment