Exemplo n.º 1
0
def do_decode(text, codec):
    """Function to convert “binary” text into text."""
    # XXX Their might be a better way to create a bytes from ints, but
    #     for now it will do the trick!
    hex_s = "".join(["{:0>2x}".format(int(''.join(p), 2))
                     for p in utils.grouper(8, text, '')])
    return bytes.fromhex(hex_s).decode(codec)
Exemplo n.º 2
0
def do_decypher(text, codec="utf-8"):
    """Function to convert “binary” text into text."""
    # XXX Their might be a better way to create a bytes from ints, but
    #     for now it will do the trick!
    hex_s = "".join([
        "{:0>2x}".format(int(''.join(p), 2))
        for p in utils.grouper(text, 8, '')
    ])
    return bytes.fromhex(hex_s).decypher(codec)
Exemplo n.º 3
0
def _process_square(text, key, _reverse=False):
    """
    (De)cypher message using caesar's square.
    key = 0 -> square.
    key > 0 -> fixed-width rectangle.
    key < 0 -> fixed-height rectangle.
    """
    base = ord('A')
    modulo = ord('Z') - base + 1

    square = []
    text = text.replace(' ', '')
    ln_txt = len(text)

    if key == 0:
        # Squarish square...
        t = ln_txt ** 0.5
        key = int(t)
        # e.g. length 577 will be 25*24, not 25*25...
        # More generally, x² - (x-1)² = 2x - 1
        # Hence we can have either x*x, or x*(x-1) grids...
        if (key < t and (not _reverse or ((key + 1) * key) < ln_txt)):
            key += 1
    elif key < 0:
        # Variable width, let's just recalculate the width we'll need
        # for this text, given the constant height!
        key = (len(text) - key - 1) // -key

    if _reverse and len(text) % key:
        # We need a "square" where empty places are on the rightest column,
        # not the lowest row!
        dlt = key - (len(text) % key)
        high = (len(text) + key - 1) // key
        f_ln = key * (high - dlt)
        square = tuple(utils.grouper(text[:f_ln], key, '')) + \
                 tuple(g + ('',)
                       for g in utils.grouper(text[f_ln:], key - 1, ''))
    else:
        square = tuple(utils.grouper(text, key, ''))
    return "".join("".join(p) for p in zip(*square))
Exemplo n.º 4
0
def _process_square(text, key, _reverse=False):
    """
    (De)cypher message using caesar's square.
    key = 0 -> square.
    key > 0 -> fixed-width rectangle.
    key < 0 -> fixed-height rectangle.
    """
    base = ord('A')
    modulo = ord('Z') - base + 1

    square = []
    text = text.replace(' ', '')
    ln_txt = len(text)

    if key == 0:
        # Squarish square...
        t = ln_txt**0.5
        key = int(t)
        # e.g. length 577 will be 25*24, not 25*25...
        # More generally, x² - (x-1)² = 2x - 1
        # Hence we can have either x*x, or x*(x-1) grids...
        if (key < t and (not _reverse or ((key + 1) * key) < ln_txt)):
            key += 1
    elif key < 0:
        # Variable width, let's just recalculate the width we'll need
        # for this text, given the constant height!
        key = (len(text) - key - 1) // -key

    if _reverse and len(text) % key:
        # We need a "square" where empty places are on the rightest column,
        # not the lowest row!
        dlt = key - (len(text) % key)
        high = (len(text) + key - 1) // key
        f_ln = key * (high - dlt)
        square = tuple(utils.grouper(text[:f_ln], key, '')) + \
                 tuple(g + ('',)
                       for g in utils.grouper(text[f_ln:], key - 1, ''))
    else:
        square = tuple(utils.grouper(text, key, ''))
    return "".join("".join(p) for p in zip(*square))
Exemplo n.º 5
0
Arquivo: gray.py Projeto: 47-/Cyprium
def do_cypher(text, codec=DEFAULT, lengths=(8,), sep=""):
    """
    Function to convert some text to Gray code.
    """
    mapp = {n: bin2gray_n(n) for n in lengths}
    # Simpler to pass by a a textual representation of binary data... :/
    bytes = "".join("{:0>8b}".format(c) for c in text.encode(codec))

    ret = []
    for n in lengths:
        ret.append(sep.join(mapp[n][int("".join(c), 2)]
                            for c in utils.grouper(bytes, n, '0')))
    return ret
Exemplo n.º 6
0
def do_cypher(text, codec=DEFAULT, lengths=(8, ), sep=""):
    """
    Function to convert some text to Gray code.
    """
    mapp = {n: bin2gray_n(n) for n in lengths}
    # Simpler to pass by a a textual representation of binary data... :/
    bytes = "".join("{:0>8b}".format(c) for c in text.encode(codec))

    ret = []
    for n in lengths:
        ret.append(
            sep.join(mapp[n][int("".join(c), 2)]
                     for c in utils.grouper(bytes, n, '0')))
    return ret
Exemplo n.º 7
0
def do_decypher(text, codec=DEFAULT, base=2):
    """
    Function to convert binary/octal/decimal/hexadecimal text into text.
    Note: expect "unspaced" text as input!
    """
    n_digits = {k: v for k, v in N_DIGITS.items()}
    if codec == ASCII7:
        codec = ASCII
        n_digits[2] = 7

    if base != 16:
        ints = (int(''.join(p), base)
                for p in utils.grouper(text, n_digits[base], ''))
        byts = utils.int8_to_bytes(ints)
    else:
        byts = bytes.fromhex(text)
    return byts.decode(codec)
Exemplo n.º 8
0
def do_decypher(text, codec=DEFAULT, base=2):
    """
    Function to convert binary/octal/decimal/hexadecimal text into text.
    Note: expect "unspaced" text as input!
    """
    n_digits = {k: v for k, v in N_DIGITS.items()}
    if codec == ASCII7:
        codec = ASCII
        n_digits[2] = 7

    if base != 16:
        ints = (int(''.join(p), base)
                for p in utils.grouper(text, n_digits[base], ''))
        byts = utils.int8_to_bytes(ints)
    else:
        byts = bytes.fromhex(text)
    return byts.decode(codec)
Exemplo n.º 9
0
def do_decypher(text, base=2):
    """
    Function to convert binary/octal/decimal/hexadecimal baudot text into text.
    Note: expect "unspaced" text as input!
    """
    ret = []
    maps = MAPS[base]
    # This makes mandatory the first byte is a mode switch
    # XXX Use rather a default (letters?) one?
    mode = None
    for c in utils.grouper(text, N_DIGITS[base], ''):
        c = "".join(c)
        if c == maps[L_MODE]:
            mode = L_MODE
        elif c == maps[S_MODE]:
            mode = S_MODE
        else:
            ret.append(maps["RMAP"][mode][c])
    return "".join(ret)
Exemplo n.º 10
0
def do_decypher(text, base=2):
    """
    Function to convert binary/octal/decimal/hexadecimal baudot text into text.
    Note: expect "unspaced" text as input!
    """
    ret = []
    maps = MAPS[base]
    # This makes mandatory the first byte is a mode switch
    # XXX Use rather a default (letters?) one?
    mode = None
    for c in utils.grouper(text, N_DIGITS[base], ''):
        c = "".join(c)
        if c == maps[L_MODE]:
            mode = L_MODE
        elif c == maps[S_MODE]:
            mode = S_MODE
        else:
            ret.append(maps["RMAP"][mode][c])
    return "".join(ret)