<commit_before>import base64
import collections
import string
import six
import codecs
import pwnypack.main


__all__ = [
    'xor',
    'rot13',
    'caesar',
    'enhex',
    'dehex',
    'enb64',
    'deb64',
    'frequency',
]


def xor(key, data):
    if type(key) is int:
        key = six.int2byte(key)
    key_len = len(key)

    return b''.join(
        six.int2byte(c ^ six.indexbytes(key, i % key_len))
        for i, c in enumerate(six.iterbytes(data))
    )


def caesar(shift, data, shift_ranges=('az', 'AZ')):
    alphabet = {
        chr(c): chr((c - s + shift) % (e - s + 1) + s)
        for s, e in map(lambda r: (ord(r[0]), ord(r[-1])), shift_ranges)
        for c in range(s, e + 1)
    }
    return ''.join(alphabet.get(c, c) for c in data)


rot13_encode = codecs.getencoder('rot-13')
rot13 = lambda d: rot13_encode(d)[0]

hex_encode = codecs.getencoder('hex')
hex_decode = codecs.getdecoder('hex')
enhex = lambda d: hex_encode(d)[0].decode('ascii')
dehex = lambda d: hex_decode(''.join(d.replace(':', '').split()))[0]


enb64 = lambda d: base64.b64encode(d).decode('ascii')
deb64 = lambda d: base64.b64decode(d.encode('ascii'))


frequency = collections.Counter


@pwnypack.main.register('xor')
def xor_app(parser, cmd, args):  # pragma: no cover
    """
    Xor a value with a key.
    """

    parser.add_argument(
        '-d', '--dec',
        help='interpret the key as a decimal integer',
        dest='type',
        action='store_const',
        const=int<commit_msg>Switch to binascii.{,un}hexlify for hex codec.<commit_after>import base64
import collections
import string
import six
import codecs
import pwnypack.main
import binascii


__all__ = [
    'xor',
    'rot13',
    'caesar',
    'enhex',
    'dehex',
    'enb64',
    'deb64',
    'frequency',
]


def xor(key, data):
    if type(key) is int:
        key = six.int2byte(key)
    key_len = len(key)

    return b''.join(
        six.int2byte(c ^ six.indexbytes(key, i % key_len))
        for i, c in enumerate(six.iterbytes(data))
    )


def caesar(shift, data, shift_ranges=('az', 'AZ')):
    alphabet = {
        chr(c): chr((c - s + shift) % (e - s + 1) + s)
        for s, e in map(lambda r: (ord(r[0]), ord(r[-1])), shift_ranges)
        for c in range(s, e + 1)
    }
    return ''.join(alphabet.get(c, c) for c in data)


rot13_encode = codecs.getencoder('rot-13')
rot13 = lambda d: rot13_encode(d)[0]

enhex = lambda d: binascii.hexlify(d).decode('ascii')
dehex = lambda d: binascii.unhexlify(''.join(d.replace(':', '').split()).encode('ascii'))


enb64 = lambda d: base64.b64encode(d).decode('ascii')
deb64 = lambda d: base64.b64decode(d.encode('ascii'))


frequency = collections.Counter


@pwnypack.main.register('xor')
def xor_app(parser, cmd, args):  # pragma: no cover
    """
    Xor a value with a key.
    """

    parser.add_argument(
        '-d', '--dec',
        help='interpret the key as a decimal integer',
        dest='type',
        action='store_const',
        const=int
{'fraction_non_alphanumeric': 0.1670564650851988, 'fraction_numerical': 0.024056130972268625, 'mean_word_length': 2.9446640316205532, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '760914', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1106, 'n_words': 272}