def test_a85encode(self):
     eq = self.assertEqual
     tests = {b'': b'', b'www.python.org': b'GB\\6`E-ZP=Df.1GEb>', bytes
         (range(255)):
         b'!!*-\'"9eu7#RLhG$k3[W&.oNg\'GVB"(`=52*$$(B+<_pR,UFcb-n-Vr/1iJ-0JP==1c70M3&s#]4?Ykm5X@_(6q\'R884cEH9MJ8X:f1+h<)lt#=BSg3>[:ZC?t!MSA7]@cBPD3sCi+\'.E,fo>FEMbNG^4U^I!pHnJ:W<)KS>/9Ll%"IN/`jYOHG]iPa.Q$R$jD4S=Q7DTV8*TUnsrdW2ZetXKAY/Yd(L?[\'d?O\\@K2_]Y2%o^qmn*`5Ta:aN;TJbg"GZd*^:jeCE.%f\\,!5gtgiEi8N\\UjQ5OekiqBum-X60nF?)@o_%qPq"ad`r;HT'
         ,
         b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#0^&*();:<>,. []{}'
         :
         b'@:E_WAS,RgBkhF"D/O92EH6,BF`qtRH$VbC6UX@47n?3D92&&T:Jand;cHat=\'/U/0JP==1c70M3&r-I,;<FN.OZ`-3]oSW/g+A(H[P'
         , b'no padding..': b'DJpY:@:Wn_DJ(RS',
         b'zero compression\x00\x00\x00\x00': b'H=_,8+Cf>,E,oN2F(oQ1z',
         b'zero compression\x00\x00\x00': b'H=_,8+Cf>,E,oN2F(oQ1!!!!',
         b'Boundary:\x00\x00\x00\x00': b'6>q!aA79M(3WK-[!!',
         b'Space compr:    ': b';fH/TAKYK$D/aMV+<VdL', b'\xff': b'rr', (
         b'\xff' * 2): b's8N', (b'\xff' * 3): b's8W*', (b'\xff' * 4):
         b's8W-!'}
     for data, res in tests.items():
         eq(base64.a85encode(data), res, data)
         eq(base64.a85encode(data, adobe=False), res, data)
         eq(base64.a85encode(data, adobe=True), b'<~' + res + b'~>', data)
     self.check_other_types(base64.a85encode, b'www.python.org',
         b'GB\\6`E-ZP=Df.1GEb>')
     self.assertRaises(TypeError, base64.a85encode, '')
     eq(base64.a85encode(b'www.python.org', wrapcol=7, adobe=False),
         b'GB\\6`E-\nZP=Df.1\nGEb>')
     eq(base64.a85encode(b'\x00\x00\x00\x00www.python.org', wrapcol=7,
         adobe=False), b'zGB\\6`E\n-ZP=Df.\n1GEb>')
     eq(base64.a85encode(b'www.python.org', wrapcol=7, adobe=True),
         b'<~GB\\6`\nE-ZP=Df\n.1GEb>\n~>')
     eq(base64.a85encode(b' ' * 8, foldspaces=True, adobe=False), b'yy')
     eq(base64.a85encode(b' ' * 7, foldspaces=True, adobe=False), b'y+<Vd')
     eq(base64.a85encode(b' ' * 6, foldspaces=True, adobe=False), b'y+<U')
     eq(base64.a85encode(b' ' * 5, foldspaces=True, adobe=False), b'y+9')
 def test_a85_padding(self):
     eq = self.assertEqual
     eq(base64.a85encode(b'x', pad=True), b'GQ7^D')
     eq(base64.a85encode(b'xx', pad=True), b"G^'2g")
     eq(base64.a85encode(b'xxx', pad=True), b'G^+H5')
     eq(base64.a85encode(b'xxxx', pad=True), b'G^+IX')
     eq(base64.a85encode(b'xxxxx', pad=True), b'G^+IXGQ7^D')
     eq(base64.a85decode(b'GQ7^D'), b'x\x00\x00\x00')
     eq(base64.a85decode(b"G^'2g"), b'xx\x00\x00')
     eq(base64.a85decode(b'G^+H5'), b'xxx\x00')
     eq(base64.a85decode(b'G^+IX'), b'xxxx')
     eq(base64.a85decode(b'G^+IXGQ7^D'), b'xxxxx\x00\x00\x00')
    def test_a85_padding(self):
        eq = self.assertEqual

        eq(base64.a85encode(b"x", pad=True), b'GQ7^D')
        eq(base64.a85encode(b"xx", pad=True), b"G^'2g")
        eq(base64.a85encode(b"xxx", pad=True), b'G^+H5')
        eq(base64.a85encode(b"xxxx", pad=True), b'G^+IX')
        eq(base64.a85encode(b"xxxxx", pad=True), b'G^+IXGQ7^D')

        eq(base64.a85decode(b'GQ7^D'), b"x\x00\x00\x00")
        eq(base64.a85decode(b"G^'2g"), b"xx\x00\x00")
        eq(base64.a85decode(b'G^+H5'), b"xxx\x00")
        eq(base64.a85decode(b'G^+IX'), b"xxxx")
        eq(base64.a85decode(b'G^+IXGQ7^D'), b"xxxxx\x00\x00\x00")
    def test_a85encode(self):
        eq = self.assertEqual

        tests = {
            b'': b'',
            b"www.python.org": b'GB\\6`E-ZP=Df.1GEb>',
            bytes(range(255)): b"""!!*-'"9eu7#RLhG$k3[W&.oNg'GVB"(`=52*$$"""
               b"""(B+<_pR,UFcb-n-Vr/1iJ-0JP==1c70M3&s#]4?Ykm5X@_(6q'R884cE"""
               b"""H9MJ8X:f1+h<)lt#=BSg3>[:ZC?t!MSA7]@cBPD3sCi+'.E,fo>FEMbN"""
               b"""G^4U^I!pHnJ:W<)KS>/9Ll%"IN/`jYOHG]iPa.Q$R$jD4S=Q7DTV8*TU"""
               b"""nsrdW2ZetXKAY/Yd(L?['d?O\\@K2_]Y2%o^qmn*`5Ta:aN;TJbg"GZd"""
               b"""*^:jeCE.%f\\,!5gtgiEi8N\\UjQ5OekiqBum-X60nF?)@o_%qPq"ad`"""
               b"""r;HT""",
            b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                b"0123456789!@#0^&*();:<>,. []{}":
                b'@:E_WAS,RgBkhF"D/O92EH6,BF`qtRH$VbC6UX@47n?3D92&&T'
                b":Jand;cHat='/U/0JP==1c70M3&r-I,;<FN.OZ`-3]oSW/g+A(H[P",
            b"no padding..": b'DJpY:@:Wn_DJ(RS',
            b"zero compression\0\0\0\0": b'H=_,8+Cf>,E,oN2F(oQ1z',
            b"zero compression\0\0\0": b'H=_,8+Cf>,E,oN2F(oQ1!!!!',
            b"Boundary:\0\0\0\0": b'6>q!aA79M(3WK-[!!',
            b"Space compr:    ": b';fH/TAKYK$D/aMV+<VdL',
            b'\xff': b'rr',
            b'\xff'*2: b's8N',
            b'\xff'*3: b's8W*',
            b'\xff'*4: b's8W-!',
            }

        for data, res in tests.items():
            eq(base64.a85encode(data), res, data)
            eq(base64.a85encode(data, adobe=False), res, data)
            eq(base64.a85encode(data, adobe=True), b'<~' + res + b'~>', data)

        self.check_other_types(base64.a85encode, b"www.python.org",
                               b'GB\\6`E-ZP=Df.1GEb>')

        self.assertRaises(TypeError, base64.a85encode, "")

        eq(base64.a85encode(b"www.python.org", wrapcol=7, adobe=False),
           b'GB\\6`E-\nZP=Df.1\nGEb>')
        eq(base64.a85encode(b"\0\0\0\0www.python.org", wrapcol=7, adobe=False),
           b'zGB\\6`E\n-ZP=Df.\n1GEb>')
        eq(base64.a85encode(b"www.python.org", wrapcol=7, adobe=True),
           b'<~GB\\6`\nE-ZP=Df\n.1GEb>\n~>')

        eq(base64.a85encode(b' '*8, foldspaces=True, adobe=False), b'yy')
        eq(base64.a85encode(b' '*7, foldspaces=True, adobe=False), b'y+<Vd')
        eq(base64.a85encode(b' '*6, foldspaces=True, adobe=False), b'y+<U')
        eq(base64.a85encode(b' '*5, foldspaces=True, adobe=False), b'y+9')
Example #5
0
 def ascii85_encoder(destination):
     rest = b''
     while True:
         try:
             in_bytes = rest + (yield)
         except GeneratorExit:
             break
         rest_len = len(in_bytes) % 4
         if rest_len:
             input, rest = in_bytes[:-rest_len], in_bytes[-rest_len:]
         else:
             input, rest = in_bytes, b''
         destination.write(a85encode(input))
     destination.write(a85encode(rest))
     destination.write(b'~>')  # EOD marker
Example #6
0
 def ascii85_encoder(destination):
     rest = b''
     while True:
         try:
             in_bytes = rest + (yield)
         except GeneratorExit:
             break
         rest_len = len(in_bytes) % 4
         if rest_len:
             input, rest = in_bytes[:-rest_len], in_bytes[-rest_len:]
         else:
             input, rest = in_bytes, b''
         destination.write(a85encode(input))
     destination.write(a85encode(rest))
     destination.write(b'~>')  # EOD marker
Example #7
0
def fuzz(filename):
    with open(filename, "rb") as fp:
        data = fp.read()
    try:
        base64.b64decode(data)
    except base64.binascii.Error:
        pass
    try:
        base64.standard_b64decode(data)
    except base64.binascii.Error:
        pass
    try:
        base64.urlsafe_b64decode(data)
    except base64.binascii.Error:
        pass
    try:
        base64.b32decode(data)
    except base64.binascii.Error:
        pass
    try:
        base64.b16decode(data)
    except base64.binascii.Error:
        pass
    try:
        base64.a85decode(data)
    # TODO undocumented exception.
    except ValueError:
        pass
    try:
        base64.a85decode(data, adobe=True)
    # TODO undocumented exception.
    except ValueError:
        pass
    try:
        base64.b85decode(data)
    # TODO undocumented exception.
    except ValueError:
        pass

    assert data == base64.b64decode(base64.b64encode(data))
    assert data == base64.standard_b64decode(base64.standard_b64encode(data))
    assert data == base64.urlsafe_b64decode(base64.urlsafe_b64encode(data))
    assert data == base64.b32decode(base64.b32encode(data))
    assert data == base64.b16decode(base64.b16encode(data))
    assert data == base64.a85decode(base64.a85encode(data))
    assert data == base64.a85decode(
        base64.a85encode(data, adobe=True), adobe=True)
    assert data == base64.b85decode(base64.b85encode(data))
Example #8
0
def printem(n_bytes):
    msg = "Generating {} bit random ascii85 strings".format(n_bytes * 8)
    print(msg)
    print("-" * len(msg))
    for _ in range(3):
        print(str(a85encode(os.urandom(n_bytes)), 'ascii'))
    return
Example #9
0
 def save(self):
     return base64.a85encode(
         pickle.dumps({
             'polynomial_features': self.polynomial_features,
             'linear_regression_x': self.linear_regression_x,
             'linear_regression_y': self.linear_regression_y
         })).decode('ascii')
Example #10
0
def main():
    inp = input('->')
    encoded = inp.encode(
        'utf-8')  # encoded the input (we need a bytes like object)
    a85encoded = base64.a85encode(encoded)  # a85encoded the encoded string
    print(a85encoded)
    print(base64.a85decode(a85encoded).decode('utf-8'))  # decoded it
Example #11
0
def main() -> None:
    inp = input("->")
    encoded = inp.encode(
        "utf-8")  # encoded the input (we need a bytes like object)
    a85encoded = base64.a85encode(encoded)  # a85encoded the encoded string
    print(a85encoded)
    print(base64.a85decode(a85encoded).decode("utf-8"))  # decoded it
Example #12
0
def hash_dict(d, str_encoding='base64'):
    """Returns the hash string for an input dict
    
    :param d: dict to hash

    :param str_encoding: how to encode the hash. The undelying hash
      function is always sha256, but the standard hexdigest tends to
      be quite large. To get shorter hashes, you can provide an
      alternative encoding. Supported values are: , (url safe)
      'base64' (defualt), 'ascii85' (default) or just 'hexdigest'

    :returns: hash string for dict
    :rtype: str
    """
    s = json.dumps(d, sort_keys=True)
    def str_as_bytesiter(s):
        yield s.encode()
    assert str_encoding in ['hexdigest', 'base64', 'ascii85']
    hfn, byter = hashlib.sha256(), str_as_bytesiter(s)
    if str_encoding == 'hexdigest':
        return hash_bytesiter(byter, hasher=hfn, ashexstr=True)
    elif str_encoding == 'ascii85':
        return base64.a85encode(
            hash_bytesiter(byter, hasher=hfn)).decode('utf-8')
    else:  # assume str_encoding == 'base64'
        return clean_b64(base64.urlsafe_b64encode(
            hash_bytesiter(byter, hasher=hfn)).decode('utf-8'))
Example #13
0
    async def send(self, message):
        """
        Send a message
        """
        await self.wait_until_ready()
        ## Format message(if message is string, convert to
        ## bytes, then add message start text and length)
        if type(message) is str:
            message = message.encode()

        formattedMessage = self.startHeader.encode() + b":\n"
        formattedMessage += self.lengthHeader.encode() + b":" + str(
            len(message)).encode() + b"\n"
        formattedMessage += message

        formattedMessage = a85encode(formattedMessage).decode()

        ## Get communication channel
        commChannel = await self.fetch_channel(self.commChannelId)

        ## Send message
        # Message sent in base64 encoded chunks
        remaining = formattedMessage
        while len(remaining) > self.messageBlockSize:
            await commChannel.send(remaining[:self.messageBlockSize])
            remaining = remaining[self.messageBlockSize:]

        await commChannel.send(remaining)
Example #14
0
def _postscript_objdef(
    alias: str,
    dictionary: Dict[str, str],
    *,
    stream_name: str = None,
    stream_data: bytes = None,
) -> Iterator[str]:
    assert (stream_name is None) == (stream_data is None)

    objtype = '/stream' if stream_name else '/dict'

    if stream_name:
        assert stream_data is not None
        a85_data = base64.a85encode(stream_data, adobe=True).decode('ascii')
        yield f'{stream_name} ' + a85_data
        yield 'def'

    if alias != '{Catalog}':  # Catalog needs no definition
        yield f'[/_objdef {alias} /type {objtype} /OBJ pdfmark'

    yield f'[{alias} <<'
    for key, val in dictionary.items():
        yield f'  {key} {val}'
    yield '>> /PUT pdfmark'

    if stream_name:
        yield f'[{alias} {stream_name[1:]} /PUT pdfmark'
Example #15
0
def encrypt(arguments):
    """Encrypt the input file,
    and write the result either to the output file,
    or Ascii85-emcode to stdout
    """
    if arguments.input_file:
        source_data = arguments.input_file.read_bytes()
    else:
        source_data = sys.stdin.buffer.read()
    #
    encryption_password = getpass.getpass(
        'Enter encryption password: '******'utf-8')
    # Encrypt using the password
    temp_file = io.BytesIO()
    scrypt_file = pyscrypt.ScryptFile(temp_file, encryption_password, 1024, 1,
                                      1)
    scrypt_file.write(source_data)
    scrypt_file.finalize()
    if arguments.output_file:
        arguments.output_file.write_bytes(temp_file.getvalue())
    else:
        sys.stdout.buffer.write(
            base64.a85encode(temp_file.getvalue(), wrapcol=76))
        sys.stdout.write('\n')
    #
    return True
 def close(self):
   super().write(base64.a85encode(self.trailer))
   # Avoid breaking the end-of-data marker (but still try to wrap).
   if self.wrapcol and self.column > self.wrapcol - 2:
     self.buffer.write(b'\n')
   self.buffer.write(b'~>')
   self.buffer.close()
Example #17
0
def test(v: bytes):
    ''' test '''
    def show(m, n):
        ''' show '''
        print(f'{m:<14s}: {n}')

    #hx = binascii.hexlify(v)    # bytes: b'([0-9a-f][0-9a-f])+'
    #show('input', hx)
    hxx = v.hex()  # str: ([0-9a-f][0-9a-f])+
    show('input hex', hxx)
    #b16 = base64.b16encode(v)   # bytes: b'([0-9A-F][0-9A-F])+'
    #show('b16encode', b16)
    print('-' * 60)

    r0 = base64.standard_b64encode(v)
    r1 = base64.b64encode(v)
    r2 = base64.urlsafe_b64encode(v)

    if r0 != r1:
        show('std base64', r0)
    show('base64', r1)

    if r1 != r2:
        show('urlsafe base64', r2)

    print('-' * 60)
    r = base64.a85encode(v)
    show('base85a', r)
    r = base64.b85encode(v)
    show('base85b', r)

    print('-' * 60)
    r = base58.b58encode(v)
    show('base58', r)
Example #18
0
    async def encode_ascii85(self, ctx, *, input: commands.clean_content = None):
        if not input:
            input = await self.detect_file(ctx)

        await self.encryptout(
            ctx, "Text -> ASCII85", base64.a85encode(input.encode('UTF-8'))
        )
Example #19
0
def encrypt(arguments):
    """Encrypt the compressed input file (minus the XZ header),
    and write the result either to the output file,
    or Ascii85-emcode to stdout
    """
    if arguments.input_file:
        source_data = arguments.input_file.read_bytes()
    else:
        source_data = sys.stdin.buffer.read()
    #
    compressed_data = lzma.compress(source_data)
    if compressed_data[:8] != XZ_HEADER:
        logging.error('LZMA compression failed')
        return False
    #
    encryption_password = getpass.getpass(
        'Enter encryption password: '******'utf-8')
    # Encrypt using the password
    temp_file = io.BytesIO()
    scrypt_file = pyscrypt.ScryptFile(temp_file, encryption_password, 1024, 1,
                                      1)
    scrypt_file.write(compressed_data[8:])
    scrypt_file.finalize()
    if arguments.output_file:
        arguments.output_file.write_bytes(temp_file.getvalue())
    else:
        sys.stdout.buffer.write(
            base64.a85encode(temp_file.getvalue(), wrapcol=76))
        sys.stdout.write('\n')
    #
    return True
def output_pdf(fig):
    """
    Generates an PDF from the Plotly figure. Errors in the process are logged
    and an PDF with error text is returned.

    Parameters
    ----------
    fig : plotly.graph_objects.Figure

    Returns
    -------
    pdf : str
       An ASCII-85 encoded PDF

    Notes
    -----
    Requires `Orca <https://plot.ly/python/orca-management/>`_ for generating
    pdfs. If orca is not installed, an pdf with an error message will be
    returned.
    """
    try:
        pdf = base64.a85encode(fig.to_image(format='pdf')).decode('utf-8')
    except Exception:
        try:
            name = fig.layout.title['text'][3:-4]
        except Exception:
            name = 'unnamed'
        logger.error('Could not generate PDF for figure %s', name)
        # should have same text as fail SVG
        pdf = fail_pdf
    return pdf
Example #21
0
 def encode(data):
     from base64 import a85encode
     from zlib import compress
     if hasattr(data, "encode"):
         data = data.encode()
     return a85encode(compress(data), adobe=True,
                      wrapcol=250)[2:].decode()
Example #22
0
def generate_pdfa_ps(target_filename, icc='sRGB'):
    """Create a Postscript pdfmark file for Ghostscript PDF/A conversion

    A pdfmark file is a small Postscript program that provides some information
    Ghostscript needs to perform PDF/A conversion. The only information we put
    in specifies that we want the file to be a PDF/A, and we want to Ghostscript
    to convert objects to the sRGB colorspace if it runs into any object that
    it decides must be converted.

    See the Adobe pdfmark Reference for details:
    https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/pdfmark_reference.pdf

    :param target_filename: filename to save
    :param icc: ICC identifier such as 'sRGB'

    :returns: a string containing the entire pdfmark
    """
    if icc == 'sRGB':
        icc_profile = SRGB_ICC_PROFILE
    else:
        raise NotImplementedError("Only supporting sRGB")

    # Read the ICC profile, encode as ASCII85 and convert to a string which we
    # will insert in the .ps file
    bytes_icc_profile = Path(icc_profile).read_bytes()
    icc_profile = base64.a85encode(bytes_icc_profile,
                                   adobe=True).decode('ascii')

    t = Template(pdfa_def_template)
    ps = t.substitute(icc_profile=icc_profile, icc_identifier=icc)

    # We should have encoded everything to pure ASCII by this point, and
    # to be safe, only allow ASCII in PostScript
    Path(target_filename).write_text(ps, encoding='ascii')
Example #23
0
def save_to_files(out_data, dir_path, name):
    out_data_encoded = out_data.encode()
    with open(os.path.join(dir_path, name + ".dat"), "wb") as f:
        f.write(out_data_encoded)
    save_to_archives(out_data_encoded, dir_path, name + ".dat")

    base16_data = binascii.hexlify(out_data_encoded)
    with open(os.path.join(dir_path, name + ".base16"), "wb") as g:
        g.write(base16_data)
    # save_to_archives(base32_data, dir_path, name + ".base32")

    base32_data = base64.b32encode(out_data_encoded)
    with open(os.path.join(dir_path, name + ".base32"), "wb") as g:
        g.write(base32_data)
    # save_to_archives(base32_data, dir_path, name + ".base32")

    # base58_data = base58.b58encode(out_data_encoded)
    # with open(os.path.join(dir_path, name + ".base58"), "wb") as g:
    #     g.write(base58_data)
    # save_to_archives(base58_data, dir_path, name + ".base58")

    base64_data = base64.b64encode(out_data_encoded)
    with open(os.path.join(dir_path, name + ".base64"), "wb") as g:
        g.write(base64_data)
    # save_to_archives(base64_data, dir_path, name + ".base64")

    base85_data = base64.b85encode(out_data_encoded)
    with open(os.path.join(dir_path, name + ".base85"), "wb") as g:
        g.write(base85_data)
    # save_to_archives(base85_data, dir_path, name + ".base85")

    ascii85_data = base64.a85encode(out_data_encoded)
    with open(os.path.join(dir_path, name + ".ascii85"), "wb") as g:
        g.write(ascii85_data)
    # save_to_archives(ascii85_data, dir_path, name + ".ascii85")

    res = out_data_encoded + b"=" * ((4 - len(out_data_encoded) & 3) & 3)
    z85_data = z85.encode(res)
    with open(os.path.join(dir_path, name + ".z85"), "wb") as g:
        g.write(z85_data)
    # save_to_archives(z85_data, dir_path, name + ".z85")

    base91_data = base91.encode(out_data_encoded).encode()
    with open(os.path.join(dir_path, name + ".base91"), "wb") as g:
        g.write(base91_data)
    # save_to_archives(base91_data, dir_path, name + ".base91")

    uu_data = codecs.encode(out_data_encoded, encoding="uu")
    with open(os.path.join(dir_path, name + ".uu"), "wb") as g:
        g.write(uu_data)
    # save_to_archives(uu_data, dir_path, name + ".uu")

    quopri_data = quopri.encodestring(out_data_encoded)
    with open(os.path.join(dir_path, name + ".quopri"), "wb") as g:
        g.write(quopri_data)
    # save_to_archives(quopri_data, dir_path, name + ".quopri")

    binhex.binhex(os.path.join(dir_path, name + ".dat"),
                  os.path.join(dir_path, name + ".binhex"))
Example #24
0
def step_encode(text_string: str):
    text_bytes: bytes = bytes(text_string, encoding="ascii")
    a85_bytes: bytearray = a85encode(text_bytes)
    logging.debug(f"Encoded : {a85_bytes}")
    encoded_bytes_array: List(int) = [_steps_up(byte) for byte in a85_bytes]
    encoded_bytes = bytes(encoded_bytes_array)
    logging.debug(f"Encoded + shifted : {encoded_bytes}")
    return str(encoded_bytes, encoding="ascii")
Example #25
0
	def encodebase(encoder): #Function for encoder for all base's
		encoder =encoder.encode("utf-8")
		Encoders = {}
		Encoders["Base64"]    = str(base64.b64encode(encoder), "utf-8")
		Encoders["Base32"]    = str(base64.b32encode(encoder), "utf-8")
		Encoders["a85encode"] = str(base64.a85encode(encoder), "utf-8")
		Encoders["b16encode"] = str(base64.b16encode(encoder), "utf-8")
		return Encoders
Example #26
0
    def test_encode(self):
        for phrase in self.phrases_to_encode:
            phrase = phrase.encode("utf-8")

            actual = ascii85.encode(phrase)
            expected = base64.a85encode(phrase, adobe=True)

            self.assertEqual(actual, expected)
Example #27
0
def analyse_images_in_blocks():
    # Get the next N images that need processing, and process them
    # Use multiprocessing because it's faster
    db = get_db()
    c = db.cursor()
    pool = Pool()  # don't use all processors, or the machine hangs
    chunk_size = 24  # higher is better, but we might run out of memory
    c.execute("select count(*) from images where md5 is null")
    cnt = c.fetchone()[0]
    if cnt == 0: return (0, 0)  # none left to process
    c.execute("select id, full_path from images where md5 is null limit ?",
              (chunk_size, ))
    images = c.fetchall()
    results = pool.map(
        load_single, images
    )  # map can take a chunksize, but we don't want all results in one huge object
    pool.close()
    pool.join()

    image_updates = []
    face_inserts = []
    for image_id, locations, encodings, md5, thumbnail, width, height in results:
        image_updates.append({
            "id": image_id,
            "md5": md5,
            "thumbnail": thumbnail,
            "facecount": len(encodings),
            "width": width,
            "height": height
        })
        if encodings:
            for loc, enc in zip(locations, encodings):
                face_inserts.append({
                    "id":
                    None,
                    "image":
                    image_id,
                    "encoding":
                    base64.a85encode(enc.tostring()),
                    "x":
                    loc[3],
                    "y":
                    loc[0],
                    "w":
                    loc[1] - loc[3],
                    "h":
                    loc[2] - loc[0]
                })
    c.executemany(
        """update images set md5=:md5, thumbnail=:thumbnail, facecount=:facecount,
                    width=:width, height=:height where id=:id""",
        image_updates)
    c.executemany(
        """INSERT OR IGNORE INTO faces 
        (id, image, x, y, w, h, encoding)
        VALUES (:id, :image, :x, :y, :w, :h, :encoding)""", face_inserts)
    db.commit()
    return len(image_updates), cnt - len(image_updates)
Example #28
0
        def genPasswd(widget):
            website = w_input.value.encode()
            masterpass = m_input.value.encode()

            salted = website + masterpass
            hash = hashlib.sha256(salted).digest()
            password = a85encode(hash).decode()[:15]

            f_input.value = password
Example #29
0
def b85(text, encode=False):
    import base64
    text = bytes(text, "utf-8")
    if encode:
        return SUCCESS % base64.a85encode(text).decode("utf-8")
    try:
        return SUCCESS % str(base64.a85decode(text))[2:-1]
    except:
        return FAIL % "Invalid base85 input"
def encrypt_to_base64(data):
    """
    :param data: str or bytes
    """
    try:
        return base64.a85encode(encrypt(data))
    except:
        traceback.print_exc()
    return b''
Example #31
0
 def _get_make_blank_entityid_unique(self) -> Optional[str]:
     ''' enable storing broken <EntityDescriptor> documents where an entityID cannot be parsed '''
     if self.entityID:
         return None
     else:
         hashobj = hashlib.md5()
         hashobj.update(self.ed_uploaded.encode('utf-8'))
         hash_str = base64.a85encode(hashobj.digest()).decode('ascii')[0:16]
         return hash_str
Example #32
0
def binary_decode_encode():
    print("\n# binary_decode_encode")
    tb = TAINTED_BYTES
    import base64

    ensure_tainted(
        # New in Python 3.4
        base64.a85encode(tb),  # $ tainted
        base64.a85decode(base64.a85encode(tb)),  # $ tainted

        # New in Python 3.4
        base64.b85encode(tb),  # $ tainted
        base64.b85decode(base64.b85encode(tb)),  # $ tainted

        # New in Python 3.1
        base64.encodebytes(tb),  # $ tainted
        base64.decodebytes(base64.encodebytes(tb)),  # $ tainted
    )
Example #33
0
def hash_object(path_or_bytes, isFile=False):
    if isFile and type(path_or_bytes) == str:
        with open(path_or_bytes, 'rb') as f:
            obj_bytes = f.read()
    elif type(path_or_bytes) == str:
        obj_bytes = path_or_bytes.encode()
    else:
        obj_bytes = path_or_bytes
    return base64.a85encode(sha512(obj_bytes).digest()).decode()
Example #34
0
    async def encode_ascii85(self, ctx, *, _input: clean_content = None):
        """ Encode in ASCII85 """
        if not _input:
            _input = await detect_file(ctx)

        await encrypt_out(
            ctx, "Text -> ASCII85",
            base64.a85encode(_input.encode('UTF-8'))
        )
Example #35
0
 def send(self, key, recipients, message):
     if not isinstance(recipients, list):
         recipients = [recipients]
     recipients = [ x.get_addr()
         if isinstance(x, ChumpServer)
         else x
         for x in recipients ]
     smtp = self._get_smtp()
     full_message = {
         'key': key,
         'sender': self._config['email']['address'],
         'body': message,
         'timestamp': int(time()),
         'protocol': 'email'
     }
     message_tcp = base64.a85encode(msgpack.packb(
          {**full_message, 'protocol': 'tcp'},
     use_bin_type=True)).decode()
     new_recipients = []
     with self.lock:
         for r in recipients:
             if self.tcpdict[r].try_send(message_tcp):
                 continue
             offer = self.tcpdict[r].make_offer()
             new_recipients.append(r)
             if offer is None:
                 continue
             self.log('Providing offer...')
             offer_msg = {
                 'key': '__offer',
                 'sender': self._config['email']['address'],
                 'body': '',
                 'timestamp': int(time()),
                 'protocol': 'email',
                 'offer': offer
             }
             offer_mail = base64.a85encode(msgpack.packb(
                 offer_msg,
             use_bin_type=True),wrapcol=80).decode()
             self._send_email([r], offer_mail)
     message_email = base64.a85encode(msgpack.packb(
          full_message,
     use_bin_type=True),wrapcol=80).decode()
     self._send_email(new_recipients, message_email)
def encode_and_save(username, game_state):
    data = ["auto", game_state.autoclick2, "print", game_state.printmoney2, "counter", game_state.counterfeit2,
            "shares", game_state.sharecrash2, "bank", game_state.bankheist2, "upg1h1", game_state.upgcheck1h1,
            "upg1h2", game_state.upgcheck1h2, "upg2h1", game_state.upgcheck2h1, "upg2h2", game_state.upgcheck2h2,
            "upg3", game_state.upgcheck3, "upg4", game_state.upgcheck4, "upg5", game_state.upgcheck5,
            "cupg1", game_state.clickupgcheck1, "cupg2", game_state.clickupgcheck2, "money",
            game_state.money, "time", game_state.timeplay, "clicks",
            game_state.totalclicks, "lotto", game_state.lottoprice]

    print('saving data:', data)

    utf8_encoded_data = str("_").join(str(y) for y in data).encode("utf-8")
    print('utf8_encoded_data:', utf8_encoded_data)

    a85_encoded_data = a85encode(utf8_encoded_data) + b";"
    print('a85_encoded_data:', a85_encoded_data)

    filename = "savefile_" + username + ".txt"
    with open(filename, "wb") as f:
        f.write(a85_encoded_data)

    print('data saved:', data)
    def generate_password(self) -> None:
        self.text['state'] = 'normal'
        self.text.delete('1.0', 'end')
        raw_input = self.key_value.get() + self.salt_value.get()  # type: str
        hashers = (hashlib.md5, hashlib.sha1, hashlib.sha224, hashlib.sha256,
                   hashlib.sha384, hashlib.sha512)
        output_strings = []  # type: tg.List[str]
        for hasher_init in hashers:
            hasher = hasher_init()
            name = hasher.name  # type: str
            hasher.update(raw_input.encode())
            digest = hasher.digest()
            hexdigest = hasher.hexdigest()
            b64_output = base64.standard_b64encode(digest).decode()
            a85_output = base64.a85encode(digest).decode()
            b85_output = base64.b85encode(digest).decode()
            output_strings.append(
                "hash: %s\nresult: %s\nbase64: %s\nascii85: %s\nbase85: %s\n" %
                (name, hexdigest, b64_output, a85_output, b85_output))

        for output_string in output_strings:
            self.text.insert('end', output_string + '\n')

        self.text['state'] = 'disabled'
Example #38
0
#!/usr/bin/env python3
# encoding: utf-8
#
# Copyright (c) 2008 Doug Hellmann All rights reserved.
#
"""Demonstrates base85 and ascii85 encodings.

http://bugs.python.org/17618
"""
#end_pymotw_header

import base64

original_data = b'This is the data, in the clear.'
print('Original    : {} bytes {!r}'.format(
    len(original_data), original_data))

b64_data = base64.b64encode(original_data)
print('b64 Encoded : {} bytes {!r}'.format(
    len(b64_data), b64_data))

b85_data = base64.b85encode(original_data)
print('b85 Encoded : {} bytes {!r}'.format(
    len(b85_data), b85_data))

a85_data = base64.a85encode(original_data)
print('a85 Encoded : {} bytes {!r}'.format(
    len(a85_data), a85_data))
Example #39
0
def main():
    inp = input('->')
    encoded = inp.encode('utf-8') #encoded the input (we need a bytes like object)
    a85encoded = base64.a85encode(encoded) #a85encoded the encoded string
    print(a85encoded)
    print(base64.a85decode(a85encoded).decode('utf-8'))#decoded it