Example #1
0
def decypher(text, codec=DEFAULT, base=None):
    """Just a wrapper around do_decypher, with some checks."""
    if base and base not in N_DIGITS:
        raise ValueError("Invalid base value ({})!.".format(base))

    # Test length (*without* the spaces!).
    text = text.replace(' ', '')
    c_data = set(text)
    base_names = {2: "binary", 8: "octal", 10: "decimal", 16: "hexadecimal"}

    n_digits = {k: v for k, v in N_DIGITS.items()}
    if codec == ASCII7:
        n_digits[2] = 7

    if base is None:
        base = utils.base_autodetect(text, n_digits,
                                     sorted(n_digits.keys(), reverse=True))

    if len(text) % n_digits[base] != 0:
        raise ValueError("No integer number of bytes, please add some "
                         "digits, to get a total length multiple of {}."
                         "".format(n_digits[base]))
    # Get allowed digits.
    c_allowed = utils.get_allowed_digits(base)
    if not (c_data <= c_allowed):
        raise ValueError("Only {} digits and spaces are allowed, no '{}'!"
                         .format(base_names[base],
                                 "', '".join(sorted(c_data - c_allowed))))
    return do_decypher(text, codec, base)
Example #2
0
def decypher(text, codecs=DEFAULT, lengths=8):
    """Just a wrapper around do_decypher, with some checks."""
    # Test length (*without* the spaces!).
    text = text.replace(' ', '')
    c_data = set(text)

    # Get allowed digits.
    c_allowed = utils.get_allowed_digits(2)
    if not (c_data <= c_allowed):
        raise ValueError(
            "Only binary digits and spaces are allowed, no '{}'!".format(
                "', '".join(sorted(c_data - c_allowed))))

    if ((codecs is None or
         (not isinstance(codecs, str) and getattr(codecs, "__iter__", None)))
            and (lengths is None or getattr(lengths, "__iter__", None))):
        return do_hack(text, codecs, lengths)

    if len(text) % lengths != 0:
        raise ValueError("No integer number of bytes, please add some "
                         "bits, to get a total length multiple of {}."
                         "".format(lengths))
    try:
        mod_codecs.lookup(codecs)
    except:
        raise ValueError("Invalid {} codec.".format(codecs))
    return do_decypher(text, codecs, lengths)
Example #3
0
File: gray.py Project: 47-/Cyprium
def decypher(text, codecs=DEFAULT, lengths=8):
    """Just a wrapper around do_decypher, with some checks."""
    # Test length (*without* the spaces!).
    text = text.replace(' ', '')
    c_data = set(text)

    # Get allowed digits.
    c_allowed = utils.get_allowed_digits(2)
    if not (c_data <= c_allowed):
        raise ValueError("Only binary digits and spaces are allowed, no '{}'!"
                         .format("', '".join(sorted(c_data - c_allowed))))

    if ((codecs is None or (not isinstance(codecs, str) and
                            getattr(codecs, "__iter__", None))) and
        (lengths is None or getattr(lengths, "__iter__", None))):
        return do_hack(text, codecs, lengths)

    if len(text) % lengths != 0:
        raise ValueError("No integer number of bytes, please add some "
                         "bits, to get a total length multiple of {}."
                         "".format(lengths))
    try:
        mod_codecs.lookup(codecs)
    except:
        raise ValueError("Invalid {} codec.".format(codecs))
    return do_decypher(text, codecs, lengths)
Example #4
0
def decypher(text, base=None):
    """Just a wrapper around do_decypher, with some checks."""
    if base and base not in N_DIGITS:
        raise ValueError("Invalid base value ({})!.".format(base))

    if not text:
        raise ValueError("No text given!")
    # Test length (*without* the spaces!).
    text = text.replace(' ', '')
    c_data = set(text)
    base_names = {2: "binary", 8: "octal", 10: "decimal", 16: "hexadecimal"}

    if base is None:
        base = utils.base_autodetect(text, N_DIGITS,
                                     sorted(N_DIGITS.keys(), reverse=True))

    if (len(text) % N_DIGITS[base]) != 0:
        raise ValueError("No integer number of bytes, please add some "
                         "digits, to get a total length multiple of {}."
                         "".format(N_DIGITS[base]))
    # Get allowed digits.
    c_allowed = utils.get_allowed_digits(base)
    if not (c_data <= c_allowed):
        raise ValueError(
            "Only {} digits and spaces are allowed, no '{}'!".format(
                base_names[base], "', '".join(sorted(c_data - c_allowed))))
    return do_decypher(text, base)
Example #5
0
def decypher(text, codec=DEFAULT, length=8):
    """Just a wrapper around do_decypher, with some checks."""
    # Test length (*without* the spaces!).
    text = text.replace(' ', '')
    c_data = set(text)

    if len(text) % length != 0:
        raise ValueError("No integer number of bytes, please add some "
                         "bits, to get a total length multiple of {}."
                         "".format(length))
    # Get allowed digits.
    c_allowed = utils.get_allowed_digits(2)
    if not (c_data <= c_allowed):
        raise ValueError("Only binary digits and spaces are allowed, no '{}'!"
                         .format("', '".join(sorted(c_data - c_allowed))))
    return do_decypher(text, codec, length)