Esempio n. 1
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009
"""
Ascii85 / Base85
base coder
Paul E. Rutter
"""

from kbp._misc import ASCII, DIGITS, ALPHABET, ALPHABET_LOWERCASE, char_range
from kbp._subst import substitute

ASCII85_1924 = DIGITS + ALPHABET + ALPHABET_LOWERCASE + "!#$%&()*+-;<=>?@^_`{|}~"  # for reference only, for RFC 1924

ASCII85 = char_range(chr(33), chr(33 + 85 - 1))


def merge(number_list, base):
    result = 0
    for i in number_list:
        result = result * base + i

    return result


def split(number, base, max_digits=0):
    result = list()
    digits = 0
    remainder = number
    while remainder != 0:
        number = remainder % base
        remainder /= base
        digits += 1
Esempio n. 2
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009
"""
Uuencode format
"""
import kbp.coder.base as base
from kbp._misc import char_range

LENGTHS = "`" + char_range(
    chr(33),
    chr(33 + 45 - 1))  # 0 is encoded as `, the other char starting from 33"
UUENCODE = char_range(" ", "_").replace(" ", "`")


def encode(filename):
    with open(filename, "rt") as f:
        r = f.read()
        mode = "644"
        print "begin " + mode + " " + filename
        encoded = base.encode(r, base.base256, UUENCODE)
        blocks = (encoded[i:i + 60] for i in range(0, len(encoded), 60))
        for block in blocks:
            print LENGTHS[len(block) * 3 / 4] + block

        print LENGTHS[0] + ""
        print "end"


def decode(filename):
    with open(filename, "rt") as f:
        r = f.read()
        lines = r.splitlines()
Esempio n. 3
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009
"""
Base2, Base8, Base16, Base32, Base32hex, Base64

coder
The Base16, Base32, and Base64 Data Encodings, rfc 3548
S. Josefsson, 2003
"""

from kbp._misc import (DIGITS, ALPHABET, ALPHABET_LOWERCASE, ASCII, char_range,
                       getbinlen, getpadbinstr, getvaluefrombinarystring, lcm)

base2 = "01"
base8 = char_range("0", "7")
base16 = DIGITS + char_range("A", "F")
base32 = ALPHABET + char_range("2", "7")
base32_hex = DIGITS + char_range("A", "V")
base64 = ALPHABET + ALPHABET_LOWERCASE + DIGITS + "+/"
base64_safe = ALPHABET + ALPHABET_LOWERCASE + DIGITS + "-_"
base256 = ASCII


def quotient_ceiling(dividend, divisor):
    quotient, remainder = divmod(dividend, divisor)
    if remainder > 0:
        quotient += 1
    return quotient


def change_base(block, block_length, source_base, source_size, target_base,
                target_size):
Esempio n. 4
0
# Kabopan - Readable Algorithms. Public Domain, 2007-2009
"""
Ascii85 / Base85
base coder
Paul E. Rutter
"""

from kbp._misc import ASCII, DIGITS, ALPHABET, ALPHABET_LOWERCASE, char_range
from kbp._subst import substitute


ASCII85_1924 = DIGITS + ALPHABET + ALPHABET_LOWERCASE + "!#$%&()*+-;<=>?@^_`{|}~"  # for reference only, for RFC 1924

ASCII85 = char_range(chr(33), chr(33 + 85 - 1))


def merge(number_list, base):
    result = 0
    for i in number_list:
        result = result * base + i

    return result


def split(number, base, max_digits=0):
    result = list()
    digits = 0
    remainder = number
    while remainder != 0:
        number = remainder % base
        remainder /= base
Esempio n. 5
0
#
# Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp.coder.ascii85 import ASCII85, merge, split, encode, decode
from kbp._misc import DIGITS, ALPHABET, char_range

assert ASCII85 == """!"#$%&'()*+,-./""" + DIGITS + ":;<=>?@" + ALPHABET + "[\]^_`" + char_range("a", "u")

assert merge([77, 97, 110, 32], 256) == 1298230816
assert split(1298230816, 85) == [24, 73, 80, 78, 61]
assert split(1298230816, 85, 7) == [0, 0, 24, 73, 80, 78, 61]

leviathan = (
    "Man is distinguished, not only by his reason, "
    "but by this singular passion from other animals, which is a lust of the mind,"
    " that by a perseverance of delight in the continued and indefatigable generation of knowledge,"
    " exceeds the short vehemence of any carnal pleasure."
)
ascii85_leviathan = (
    "<~9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>[email protected]$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,"
    'O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY'
    "i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa"
    "l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G"
    ">uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c~>"
)

for s in (DIGITS[:i] for i in range(8)):
    assert s == decode(encode(s))

assert encode(leviathan) == ascii85_leviathan
assert decode(ascii85_leviathan) == leviathan
Esempio n. 6
0
#
#Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp.coder.ascii85 import ASCII85, merge, split, encode, decode
from kbp._misc import DIGITS, ALPHABET, char_range
assert ASCII85 == """!"#$%&'()*+,-./""" + DIGITS + ":;<=>?@" + ALPHABET + "[\]^_`" + char_range(
    "a", "u")

assert merge([77, 97, 110, 32], 256) == 1298230816
assert split(1298230816, 85) == [24, 73, 80, 78, 61]
assert split(1298230816, 85, 7) == [0, 0, 24, 73, 80, 78, 61]

leviathan = "Man is distinguished, not only by his reason, " \
"but by this singular passion from other animals, which is a lust of the mind," \
" that by a perseverance of delight in the continued and indefatigable generation of knowledge," \
" exceeds the short vehemence of any carnal pleasure."
ascii85_leviathan = \
"<~9jqo^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>[email protected]$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!," \
"O<DJ+*.@<*K0@<6L(Df-\\0Ec5e;DffZ(EZee.Bl.9pF\"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKY" \
"i(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIa" \
"l(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^F!,R<AKZ&-DfTqBG%G" \
">uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c~>"

for s in (DIGITS[:i] for i in range(8)):
    assert s == decode(encode(s))

assert encode(leviathan) == ascii85_leviathan
assert decode(ascii85_leviathan) == leviathan
Esempio n. 7
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009
"""
ROT5 / ROT13 / ROT18 / ROT47 / Caesar
substitution cipher
"""

from kbp._subst import substitute
from kbp._misc import char_range, DIGITS, ALPHABET, ALPHABET_LOWERCASE

ASCII33_126 = char_range("!", "~")

def rot5(data):
    return substitute(data, DIGITS)

def rot13(data):
    return substitute(substitute(data, ALPHABET), ALPHABET_LOWERCASE)

def rot18(data):
    return rot13(rot5(data))

def rot47(data):
    return substitute(data, ASCII33_126)

def caesar_encode(plaintext):
    return substitute(plaintext, ALPHABET, lambda x: x + 3)

def caesar_decode(ciphertext):
    return substitute(ciphertext, ALPHABET, lambda x: x - 3)

if __name__ == "__main__":
    import kbp.test.rot_test
Esempio n. 8
0
from kbp._misc import (
    DIGITS,
    ALPHABET,
    ALPHABET_LOWERCASE,
    ASCII,
    char_range,
    getbinlen,
    getpadbinstr,
    getvaluefrombinarystring,
    lcm,
)


base2 = "01"
base8 = char_range("0", "7")
base16 = DIGITS + char_range("A", "F")
base32 = ALPHABET + char_range("2", "7")
base32_hex = DIGITS + char_range("A", "V")
base64 = ALPHABET + ALPHABET_LOWERCASE + DIGITS + "+/"
base64_safe = ALPHABET + ALPHABET_LOWERCASE + DIGITS + "-_"
base256 = ASCII


def quotient_ceiling(dividend, divisor):
    quotient, remainder = divmod(dividend, divisor)
    if remainder > 0:
        quotient += 1
    return quotient

Esempio n. 9
0
#
#Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp._misc import (
 byteswap, char_range, countmissing, gcd, getbinlen, getbinstr, gethexstr,
 gethyphenstr, getlongestcommon, getpadbinstr, getunkbinstr,
 getvaluefrombinarystring, bin2hex, ASCII, as_words,
 hex2bin, insert_string, int2bebin, int2lebin, lcm, md5, modifystring,
 nibbleswap, rol, ror, rorstr, setstr, zip_extend, lcm, slice_and_pad)

import struct

assert char_range("A", "Z") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

#gcd
#returns the greatest common divisor of both parameters
assert gcd(6, 8) == 2

#lcm
#returns the least common multiplier of both parameters
assert lcm(6, 9) == 18
assert lcm(6, 8) == 24

assert getbinlen(0) == 1
assert getbinlen(1) == 1
assert getbinlen(3) == 2

assert getvaluefrombinarystring("1000") == 8
assert getvaluefrombinarystring("001000") == 8

assert getpadbinstr(8, 8) == "00001000"
Esempio n. 10
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009
"""
ROT5 / ROT13 / ROT18 / ROT47 / Caesar
substitution cipher
"""

from kbp._subst import substitute
from kbp._misc import char_range, DIGITS, ALPHABET, ALPHABET_LOWERCASE

ASCII33_126 = char_range("!", "~")


def rot5(data):
    return substitute(data, DIGITS)


def rot13(data):
    return substitute(substitute(data, ALPHABET), ALPHABET_LOWERCASE)


def rot18(data):
    return rot13(rot5(data))


def rot47(data):
    return substitute(data, ASCII33_126)


def caesar_encode(plaintext):
    return substitute(plaintext, ALPHABET, lambda x: x + 3)
Esempio n. 11
0
#Kabopan - Readable Algorithms. Public Domain, 2007-2009
"""
Uuencode format
"""
import kbp.coder.base as base
from kbp._misc import char_range

LENGTHS = "`" + char_range(chr(33), chr(33 + 45 - 1)) # 0 is encoded as `, the other char starting from 33"
UUENCODE = char_range(" ", "_").replace(" ", "`")


def encode(filename):
    with open(filename,"rt") as f:
        r = f.read()
        mode = "644"
        print "begin " + mode + " " + filename
        encoded = base.encode(r, base.base256, UUENCODE)
        blocks = (encoded[i: i + 60] for i in range(0,  len(encoded), 60))
        for block in blocks:
            print LENGTHS[len(block) * 3 / 4] + block

        print LENGTHS[0] + ""
        print "end"


def decode(filename):
    with open(filename,"rt") as f:
        r = f.read()
        lines = r.splitlines()
        filename = lines[0][10:]
        print filename
Esempio n. 12
0
#
#Kabopan - Readable Algorithms. Public Domain, 2007-2009

from kbp._misc import (byteswap, char_range, countmissing, gcd, getbinlen,
                       getbinstr, gethexstr, gethyphenstr, getlongestcommon,
                       getpadbinstr, getunkbinstr, getvaluefrombinarystring,
                       bin2hex, ASCII, as_words, hex2bin, insert_string,
                       int2bebin, int2lebin, lcm, md5, modifystring,
                       nibbleswap, rol, ror, rorstr, setstr, zip_extend, lcm,
                       slice_and_pad)

import struct

assert char_range("A", "Z") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

#gcd
#returns the greatest common divisor of both parameters
assert gcd(6, 8) == 2

#lcm
#returns the least common multiplier of both parameters
assert lcm(6, 9) == 18
assert lcm(6, 8) == 24

assert getbinlen(0) == 1
assert getbinlen(1) == 1
assert getbinlen(3) == 2

assert getvaluefrombinarystring("1000") == 8
assert getvaluefrombinarystring("001000") == 8